From b471b5dd05365384c4648daf312d27282a27a499 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Thu, 6 Sep 2018 16:20:37 -0400 Subject: [PATCH] Fixed import as g instead of sg bug, made some demos borderless, used form.FindElement where possible, fixed PopupGetFile and PopupGetFolder no_window option --- Demo_Borderless_Window.py | 50 ++++++++++++++--------------- Demo_Chat.py | 16 ++++----- Demo_Chatterbot.py | 22 ++++++------- Demo_Color.py | 42 ++++++++++++------------ Demo_DOC_Viewer_PIL.py | 6 ++-- Demo_HowDoI.py | 17 ++++------ Demo_Keypad.py | 24 +++++++------- Demo_Matplotlib.py | 10 +++--- Demo_Matplotlib_Animated.py | 12 +++---- Demo_Matplotlib_Animated_Scatter.py | 10 +++--- Demo_Matplotlib_Browser.py | 18 +++++------ Demo_Matplotlib_Ping_Graph.py | 8 ++--- Demo_Matplotlib_Ping_Graph_Large.py | 10 +++--- Demo_Media_Player.py | 7 ++-- Demo_NonBlocking_Form.py | 5 ++- Demo_Pi_LEDs.py | 20 ++++++------ PySimpleGUI.py | 23 +++++++++++-- 17 files changed, 153 insertions(+), 147 deletions(-) diff --git a/Demo_Borderless_Window.py b/Demo_Borderless_Window.py index fe243b5f..51e1663f 100644 --- a/Demo_Borderless_Window.py +++ b/Demo_Borderless_Window.py @@ -1,29 +1,27 @@ -def TightLayout(): - """ - Turn off padding in order to get a really tight looking layout. - """ - import PySimpleGUI as sg - sg.ChangeLookAndFeel('Dark') - sg.SetOptions(element_padding=(0, 0)) - layout = [[sg.T('User:', pad=((3, 0), 0)), sg.OptionMenu(values=('User 1', 'User 2'), size=(20, 1)), - sg.T('0', size=(8, 1))], - [sg.T('Customer:', pad=((3, 0), 0)), sg.OptionMenu(values=('Customer 1', 'Customer 2'), size=(20, 1)), - sg.T('1', size=(8, 1))], - [sg.T('Notes:', pad=((3, 0), 0)), sg.In(size=(44, 1), background_color='white', text_color='black')], - [sg.ReadFormButton('Start', button_color=('white', 'black')), - sg.ReadFormButton('Stop', button_color=('gray50', 'black')), - sg.ReadFormButton('Reset', button_color=('white', '#9B0023')), - sg.ReadFormButton('Submit', button_color=('gray60', 'springgreen4')), - sg.SimpleButton('Exit', button_color=('white', '#00406B'))]] - form = sg.FlexForm("Time Tracker", default_element_size=(12, 1), text_justification='r', auto_size_text=False, - auto_size_buttons=False, no_titlebar=True, - default_button_element_size=(12, 1)) - form.Layout(layout) - while True: - button, values = form.Read() - if button is None or button == 'Exit': - return +import PySimpleGUI as sg +""" +Turn off padding in order to get a really tight looking layout. +""" +sg.ChangeLookAndFeel('Dark') +sg.SetOptions(element_padding=(0, 0)) +layout = [[sg.T('User:', pad=((3, 0), 0)), sg.OptionMenu(values=('User 1', 'User 2'), size=(20, 1)), + sg.T('0', size=(8, 1))], + [sg.T('Customer:', pad=((3, 0), 0)), sg.OptionMenu(values=('Customer 1', 'Customer 2'), size=(20, 1)), + sg.T('1', size=(8, 1))], + [sg.T('Notes:', pad=((3, 0), 0)), sg.In(size=(44, 1), background_color='white', text_color='black')], + [sg.ReadFormButton('Start', button_color=('white', 'black')), + sg.ReadFormButton('Stop', button_color=('gray50', 'black')), + sg.ReadFormButton('Reset', button_color=('white', '#9B0023')), + sg.ReadFormButton('Submit', button_color=('gray60', 'springgreen4')), + sg.SimpleButton('Exit', button_color=('white', '#00406B'))]] +form = sg.FlexForm("Time Tracker", default_element_size=(12, 1), text_justification='r', auto_size_text=False, + auto_size_buttons=False, no_titlebar=True, + default_button_element_size=(12, 1)) +form.Layout(layout) +while True: + button, values = form.Read() + if button is None or button == 'Exit': + break -TightLayout() \ No newline at end of file diff --git a/Demo_Chat.py b/Demo_Chat.py index 55bba560..3b80ea7c 100644 --- a/Demo_Chat.py +++ b/Demo_Chat.py @@ -16,13 +16,11 @@ def ChatBotWithHistory(): form = sg.FlexForm('Chat window with history', default_element_size=(30, 2), font=('Helvetica',' 13'), default_button_element_size=(8,2), return_keyboard_events=True) - multiline_elem = sg.Multiline(size=(85, 5), enter_submits=True, key='query', do_not_clear=False) - history_elem = sg.T('', size=(20,3)) layout = [ [sg.Text('Your output will go here', size=(40, 1))], [sg.Output(size=(127, 30), font=('Helvetica 10'))], - [sg.T('Command History'), history_elem], - [multiline_elem, + [sg.T('Command History'), sg.T('', size=(20,3), key='history')], + [sg.Multiline(size=(85, 5), enter_submits=True, key='query', do_not_clear=False), sg.ReadFormButton('SEND', button_color=(sg.YELLOWS[0], sg.BLUES[0]), bind_return_key=True), sg.SimpleButton('EXIT', button_color=(sg.YELLOWS[0], sg.GREENS[0]))] ] @@ -39,20 +37,20 @@ def ChatBotWithHistory(): print('The command you entered was {}'.format(query)) command_history.append(query) history_offset = len(command_history)-1 - multiline_elem.Update('') # manually clear input because keyboard events blocks clear - history_elem.Update('\n'.join(command_history[-3:])) + form.FindElement('query').Update('') # manually clear input because keyboard events blocks clear + form.FindElement('history').Update('\n'.join(command_history[-3:])) elif button is None or button is 'EXIT': # quit if exit button or X break elif 'Up' in button and len(command_history): command = command_history[history_offset] history_offset -= 1 * (history_offset > 0) # decrement is not zero - multiline_elem.Update(command) + form.FindElement('query').Update(command) elif 'Down' in button and len(command_history): history_offset += 1 * (history_offset < len(command_history)-1) # increment up to end of list command = command_history[history_offset] - multiline_elem.Update(command) + form.FindElement('query').Update(command) elif 'Escape' in button: - multiline_elem.Update('') + form.FindElement('query').Update('') exit(69) diff --git a/Demo_Chatterbot.py b/Demo_Chatterbot.py index 25ac0a30..f7936482 100644 --- a/Demo_Chatterbot.py +++ b/Demo_Chatterbot.py @@ -1,4 +1,4 @@ -import PySimpleGUI as g +import PySimpleGUI as sg from chatterbot import ChatBot import chatterbot.utils @@ -11,17 +11,17 @@ to collect user input that is sent to the chatbot. The reply is displayed in th # Create the 'Trainer GUI' # The Trainer GUI consists of a lot of progress bars stacked on top of each other -g.ChangeLookAndFeel('GreenTan') +sg.ChangeLookAndFeel('GreenTan') MAX_PROG_BARS = 20 # number of training sessions bars = [] texts = [] -training_layout = [[g.T('TRAINING PROGRESS', size=(20,1), font=('Helvetica', 17))],] +training_layout = [[sg.T('TRAINING PROGRESS', size=(20, 1), font=('Helvetica', 17))], ] for i in range(MAX_PROG_BARS): - bars.append(g.ProgressBar(100, size=(30, 4))) - texts.append(g.T(' '*20, size=(20,1), justification='right')) + bars.append(sg.ProgressBar(100, size=(30, 4))) + texts.append(sg.T(' ' * 20, size=(20, 1), justification='right')) training_layout += [[texts[i], bars[i]],] # add a single row -training_form = g.FlexForm('Training') +training_form = sg.FlexForm('Training') training_form.Layout(training_layout) current_bar = 0 @@ -50,10 +50,10 @@ chatbot = ChatBot('Ron Obvious', trainer='chatterbot.trainers.ChatterBotCorpusTr chatbot.train("chatterbot.corpus.english") ################# GUI ################# -with g.FlexForm('Chat Window', auto_size_text=True, default_element_size=(30, 2)) as form: - layout = [[g.Output(size=(80, 20))], - [g.Multiline(size=(70, 5), enter_submits=True), - g.ReadFormButton('SEND', bind_return_key=True), g.ReadFormButton('EXIT')]] +with sg.FlexForm('Chat Window', auto_size_text=True, default_element_size=(30, 2)) as form: + layout = [[sg.Output(size=(80, 20))], + [sg.Multiline(size=(70, 5), enter_submits=True), + sg.ReadFormButton('SEND', bind_return_key=True), sg.ReadFormButton('EXIT')]] form.Layout(layout) # ---===--- Loop taking in user input and using it to query HowDoI web oracle --- # @@ -62,7 +62,7 @@ with g.FlexForm('Chat Window', auto_size_text=True, default_element_size=(30, 2) if button is not 'SEND': break string = value.rstrip() - print(string.rjust(120)) + print(' '+string) # send the user input to chatbot to get a response response = chatbot.get_response(value.rstrip()) print(response) \ No newline at end of file diff --git a/Demo_Color.py b/Demo_Color.py index 14c64ab8..e27365a6 100644 --- a/Demo_Color.py +++ b/Demo_Color.py @@ -1,4 +1,4 @@ -import PySimpleGUI as g +import PySimpleGUI as sg MY_WINDOW_ICON = 'E:\\TheRealMyDocs\\Icons\\The Planets\\jupiter.ico' reverse = {} @@ -1639,13 +1639,13 @@ def show_all_colors_on_buttons(): global reverse global colorhex global colors - form = g.FlexForm('Colors on Buttons Demo', default_element_size=(3,1), location=(0,0), icon=MY_WINDOW_ICON, font=("Helvetica", 7)) + form = sg.FlexForm('Colors on Buttons Demo', default_element_size=(3, 1), location=(0, 0), icon=MY_WINDOW_ICON, font=("Helvetica", 7)) row = [] row_len = 20 for i, c in enumerate(colors): hex = get_hex_from_name(c) - button1 = g.Button(button_text=c, button_color=(get_complementary_hex(hex), hex), size=(8,1)) - button2 = g.Button(button_text=c, button_color=(hex,get_complementary_hex(hex)), size=(8,1)) + button1 = sg.Button(button_text=c, button_color=(get_complementary_hex(hex), hex), size=(8, 1)) + button2 = sg.Button(button_text=c, button_color=(hex, get_complementary_hex(hex)), size=(8, 1)) row.append(button1) row.append(button2) if (i+1) % row_len == 0: @@ -1656,12 +1656,12 @@ def show_all_colors_on_buttons(): form.Show() -GoodColors = [('#0e6251',g.RGB(255,246,122) ), - ('white', g.RGB(0,74,60)), - (g.RGB(0,210,124),g.RGB(0,74,60) ), - (g.RGB(0,210,87),g.RGB(0,74,60) ), - (g.RGB(0,164,73),g.RGB(0,74,60) ), - (g.RGB(0,74,60),g.RGB(0,74,60) ), +GoodColors = [('#0e6251', sg.RGB(255, 246, 122)), + ('white', sg.RGB(0, 74, 60)), + (sg.RGB(0, 210, 124), sg.RGB(0, 74, 60)), + (sg.RGB(0, 210, 87), sg.RGB(0, 74, 60)), + (sg.RGB(0, 164, 73), sg.RGB(0, 74, 60)), + (sg.RGB(0, 74, 60), sg.RGB(0, 74, 60)), ] @@ -1676,15 +1676,15 @@ def main(): # show_all_colors_on_buttons() while True: # ------- Form show ------- # - layout = [[g.Text('Find color')], - [g.Text('Demonstration of colors')], - [g.Text('Enter a color name in text or hex #RRGGBB format')], - [g.InputText()], - [g.Listbox(list_of_colors, size=(20,30)), g.T('Or choose from list')], - [g.Submit(), g.Quit(), g.SimpleButton('Show me lots of colors!', button_color=('white','#0e6251'))], + layout = [[sg.Text('Find color')], + [sg.Text('Demonstration of colors')], + [sg.Text('Enter a color name in text or hex #RRGGBB format')], + [sg.InputText()], + [sg.Listbox(list_of_colors, size=(20, 30)), sg.T('Or choose from list')], + [sg.Submit(), sg.Quit(), sg.SimpleButton('Show me lots of colors!', button_color=('white', '#0e6251'))], ] # [g.Multiline(DefaultText=str(printable), Size=(30,20))]] - (button, (hex_input, drop_down_value)) = g.FlexForm('Color Demo', auto_size_text=True, icon=MY_WINDOW_ICON).LayoutAndRead(layout) + (button, (hex_input, drop_down_value)) = sg.FlexForm('Color Demo', auto_size_text=True, icon=MY_WINDOW_ICON).LayoutAndRead(layout) # ------- OUTPUT results portion ------- # if button == '' or button == 'Quit' or button is None: @@ -1705,11 +1705,11 @@ def main(): complementary_color = get_name_from_hex(complementary_hex) # g.MsgBox('Colors', 'The RBG value is', rgb, 'color and comp are', color_string, compl) - layout = [[g.Text('That color and it\'s compliment are shown on these buttons. This form auto-closes')], - [g.Button(button_text=color_name, button_color=(color_hex, complementary_hex))], - [g.Button(button_text=complementary_hex + ' ' + complementary_color, button_color=(complementary_hex , color_hex), size=(30,1))], + layout = [[sg.Text('That color and it\'s compliment are shown on these buttons. This form auto-closes')], + [sg.Button(button_text=color_name, button_color=(color_hex, complementary_hex))], + [sg.Button(button_text=complementary_hex + ' ' + complementary_color, button_color=(complementary_hex , color_hex), size=(30, 1))], ] - g.FlexForm('Color demo', default_element_size=(100,1), auto_size_text=True, auto_close=True, auto_close_duration=5, icon=MY_WINDOW_ICON).LayoutAndRead(layout) + sg.FlexForm('Color demo', default_element_size=(100, 1), auto_size_text=True, auto_close=True, auto_close_duration=5, icon=MY_WINDOW_ICON).LayoutAndRead(layout) diff --git a/Demo_DOC_Viewer_PIL.py b/Demo_DOC_Viewer_PIL.py index ac2c8232..3c170f3c 100644 --- a/Demo_DOC_Viewer_PIL.py +++ b/Demo_DOC_Viewer_PIL.py @@ -37,14 +37,14 @@ from PIL import Image, ImageTk import time if len(sys.argv) == 1: - rc, fname = sg.GetFileBox('Document Browser', 'Document file to open', + fname = sg.PopupGetFile('Document Browser', 'Document file to open', no_window=True, file_types = ( ("PDF Files", "*.pdf"), ("XPS Files", "*.*xps"), ("Epub Files", "*.epub"), ("Fiction Books", "*.fb2"), ("Comic Books", "*.cbz"), - ("HTML", "*.htm*"), + ("HTML", "*.htm*") # add more document types here ) ) @@ -122,7 +122,7 @@ root.destroy() del root form = sg.FlexForm(title, return_keyboard_events = True, - location = (0,0), use_default_focus = False) + location = (0,0), use_default_focus = False, no_titlebar=True) cur_page = 0 data, clip_pos = get_page(cur_page, diff --git a/Demo_HowDoI.py b/Demo_HowDoI.py index b05cc266..ae1b251c 100644 --- a/Demo_HowDoI.py +++ b/Demo_HowDoI.py @@ -30,15 +30,12 @@ def HowDoI(): # ------- Make a new FlexForm ------- # sg.ChangeLookAndFeel('GreenTan') # give our form a spiffy set of colors - - multiline_elem = sg.Multiline(size=(85, 5), enter_submits=True, key='query', do_not_clear=False) - history_elem = sg.T('', size=(40,3), text_color=sg.BLUES[0]) layout = [ [sg.Text('Ask and your answer will appear here....', size=(40, 1))], [sg.Output(size=(127, 30), font=('Helvetica 10'))], [ sg.Spin(values=(1, 2, 3, 4), initial_value=1, size=(2, 1), key='Num Answers', font='Helvetica 15'), sg.T('Num Answers',font='Helvetica 15'), sg.Checkbox('Display Full Text', key='full text', font='Helvetica 15'), - sg.T('Command History'), history_elem], - [multiline_elem, + sg.T('Command History'), sg.T('', size=(40,3), text_color=sg.BLUES[0], key='history')], + [sg.Multiline(size=(85, 5), enter_submits=True, key='query', do_not_clear=False), sg.ReadFormButton('SEND', button_color=(sg.YELLOWS[0], sg.BLUES[0]), bind_return_key=True), sg.SimpleButton('EXIT', button_color=(sg.YELLOWS[0], sg.GREENS[0]))] ] @@ -56,20 +53,20 @@ def HowDoI(): QueryHowDoI(query, value['Num Answers'], value['full text']) # send the string to HowDoI command_history.append(query) history_offset = len(command_history)-1 - multiline_elem.Update('') # manually clear input because keyboard events blocks clear - history_elem.Update('\n'.join(command_history[-3:])) + form.FindElement('query').Update('') # manually clear input because keyboard events blocks clear + form.FindElement('history').Update('\n'.join(command_history[-3:])) elif button is None or button is 'EXIT': # if exit button or closed using X break elif 'Up' in button and len(command_history): # scroll back in history command = command_history[history_offset] history_offset -= 1 * (history_offset > 0) # decrement is not zero - multiline_elem.Update(command) + form.FindElement('query').Update(command) elif 'Down' in button and len(command_history): # scroll forward in history history_offset += 1 * (history_offset < len(command_history)-1) # increment up to end of list command = command_history[history_offset] - multiline_elem.Update(command) + form.FindElement('query').Update(command) elif 'Escape' in button: # clear currently line - multiline_elem.Update('') + form.FindElement('query').Update('') exit(69) diff --git a/Demo_Keypad.py b/Demo_Keypad.py index 0c1fbd84..9efa683a 100644 --- a/Demo_Keypad.py +++ b/Demo_Keypad.py @@ -1,4 +1,4 @@ -import PySimpleGUI as g +import PySimpleGUI as sg # g.SetOptions(button_color=g.COLOR_SYSTEM_DEFAULT) # because some people like gray buttons @@ -12,19 +12,17 @@ import PySimpleGUI as g # create the 2 Elements we want to control outside the form -out_elem = g.Text('', size=(15, 1), font=('Helvetica', 18), text_color='red') -in_elem = g.Input(size=(10, 1), do_not_clear=True, key='input') -layout = [[g.Text('Enter Your Passcode')], - [in_elem], - [g.ReadFormButton('1'), g.ReadFormButton('2'), g.ReadFormButton('3')], - [g.ReadFormButton('4'), g.ReadFormButton('5'), g.ReadFormButton('6')], - [g.ReadFormButton('7'), g.ReadFormButton('8'), g.ReadFormButton('9')], - [g.ReadFormButton('Submit'), g.ReadFormButton('0'), g.ReadFormButton('Clear')], - [out_elem], +layout = [[sg.Text('Enter Your Passcode')], + [sg.Input(size=(10, 1), do_not_clear=True, key='input')], + [sg.ReadFormButton('1'), sg.ReadFormButton('2'), sg.ReadFormButton('3')], + [sg.ReadFormButton('4'), sg.ReadFormButton('5'), sg.ReadFormButton('6')], + [sg.ReadFormButton('7'), sg.ReadFormButton('8'), sg.ReadFormButton('9')], + [sg.ReadFormButton('Submit'), sg.ReadFormButton('0'), sg.ReadFormButton('Clear')], + [sg.Text('', size=(15, 1), font=('Helvetica', 18), text_color='red', key='out')], ] -form = g.FlexForm('Keypad', default_button_element_size=(5, 2), auto_size_buttons=False) +form = sg.FlexForm('Keypad', default_button_element_size=(5, 2), auto_size_buttons=False) form.Layout(layout) # Loop forever reading the form's values, updating the Input field @@ -40,6 +38,6 @@ while True: keys_entered += button # add the new digit elif button is 'Submit': keys_entered = values['input'] - out_elem.Update(keys_entered) # output the final string + form.FindElement('out').Update(keys_entered) # output the final string - in_elem.Update(keys_entered) # change the form to reflect current key string \ No newline at end of file + form.FindElement('input').Update(keys_entered) # change the form to reflect current key string \ No newline at end of file diff --git a/Demo_Matplotlib.py b/Demo_Matplotlib.py index 9dc26dad..8b5d334d 100644 --- a/Demo_Matplotlib.py +++ b/Demo_Matplotlib.py @@ -1,4 +1,4 @@ -import PySimpleGUI as g +import PySimpleGUI as sg import matplotlib matplotlib.use('TkAgg') from matplotlib.backends.backend_tkagg import FigureCanvasAgg @@ -107,14 +107,14 @@ fig = plt.gcf() # if using Pyplot then get the figure from the plot # information to display. # # --------------------------------------------------------------------------------# figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds -canvas_elem = g.Canvas(size=(figure_w, figure_h)) # get the canvas we'll be drawing on +canvas_elem = sg.Canvas(size=(figure_w, figure_h)) # get the canvas we'll be drawing on # define the form layout -layout = [[g.Text('Plot test')], +layout = [[sg.Text('Plot test')], [canvas_elem], - [g.OK(pad=((figure_w/2,0), 3), size=(4,2))]] + [sg.OK(pad=((figure_w / 2, 0), 3), size=(4, 2))]] # create the form and show it without the plot -form = g.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') +form = sg.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') form.Layout(layout) form.ReadNonBlocking() diff --git a/Demo_Matplotlib_Animated.py b/Demo_Matplotlib_Animated.py index 5f640fea..60be75cc 100644 --- a/Demo_Matplotlib_Animated.py +++ b/Demo_Matplotlib_Animated.py @@ -1,5 +1,5 @@ from random import randint -import PySimpleGUI as g +import PySimpleGUI as sg from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, FigureCanvasAgg from matplotlib.figure import Figure import matplotlib.backends.tkagg as tkagg @@ -14,16 +14,16 @@ def main(): ax.set_ylabel("Y axis") ax.grid() - canvas_elem = g.Canvas(size=(640, 480)) # get the canvas we'll be drawing on - slider_elem = g.Slider(range=(0,10000), size=(60,10), orientation='h') + canvas_elem = sg.Canvas(size=(640, 480)) # get the canvas we'll be drawing on + slider_elem = sg.Slider(range=(0, 10000), size=(60, 10), orientation='h') # define the form layout - layout = [[g.Text('Animated Matplotlib', size=(40,1), justification='center', font='Helvetica 20')], + layout = [[sg.Text('Animated Matplotlib', size=(40, 1), justification='center', font='Helvetica 20')], [canvas_elem], [slider_elem], - [g.ReadFormButton('Exit', size=(10,2), pad=((280, 0), 3), font='Helvetica 14')]] + [sg.ReadFormButton('Exit', size=(10, 2), pad=((280, 0), 3), font='Helvetica 14')]] # create the form and show it without the plot - form = g.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') + form = sg.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') form.Layout(layout) form.ReadNonBlocking() diff --git a/Demo_Matplotlib_Animated_Scatter.py b/Demo_Matplotlib_Animated_Scatter.py index 53f0e580..81902298 100644 --- a/Demo_Matplotlib_Animated_Scatter.py +++ b/Demo_Matplotlib_Animated_Scatter.py @@ -1,5 +1,5 @@ from random import randint -import PySimpleGUI as g +import PySimpleGUI as sg from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, FigureCanvasAgg from matplotlib.figure import Figure import matplotlib.backends.tkagg as tkagg @@ -7,14 +7,14 @@ import tkinter as tk def main(): - canvas_elem = g.Canvas(size=(640, 480)) # get the canvas we'll be drawing on + canvas_elem = sg.Canvas(size=(640, 480)) # get the canvas we'll be drawing on # define the form layout - layout = [[g.Text('Animated Matplotlib', size=(40,1), justification='center', font='Helvetica 20')], + layout = [[sg.Text('Animated Matplotlib', size=(40, 1), justification='center', font='Helvetica 20')], [canvas_elem], - [g.ReadFormButton('Exit', size=(10,2), pad=((280, 0), 3), font='Helvetica 14')]] + [sg.ReadFormButton('Exit', size=(10, 2), pad=((280, 0), 3), font='Helvetica 14')]] # create the form and show it without the plot - form = g.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') + form = sg.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') form.Layout(layout) form.ReadNonBlocking() diff --git a/Demo_Matplotlib_Browser.py b/Demo_Matplotlib_Browser.py index 7301b5fd..17ecb0e5 100644 --- a/Demo_Matplotlib_Browser.py +++ b/Demo_Matplotlib_Browser.py @@ -1,4 +1,4 @@ -import PySimpleGUI as g +import PySimpleGUI as sg import matplotlib matplotlib.use('TkAgg') from matplotlib.backends.backend_tkagg import FigureCanvasAgg @@ -862,21 +862,21 @@ fig_dict = {'Pyplot Simple':PyplotSimple, 'Pyplot Formatstr':PyplotFormatstr,'Py 'Artist Customized Box Plots 2' : ArtistBoxplot2, 'Pyplot Histogram' : PyplotHistogram} -g.ChangeLookAndFeel('LightGreen') +sg.ChangeLookAndFeel('LightGreen') figure_w, figure_h = 650, 650 -canvas_elem = g.Canvas(size=(figure_w, figure_h)) # get the canvas we'll be drawing on -multiline_elem = g.Multiline(size=(70,35),pad=(5,(3,90))) +canvas_elem = sg.Canvas(size=(figure_w, figure_h)) # get the canvas we'll be drawing on +multiline_elem = sg.Multiline(size=(70, 35), pad=(5, (3, 90))) # define the form layout listbox_values = [key for key in fig_dict.keys()] -col_listbox = [[g.Listbox(values=listbox_values, change_submits=True, size=(28, len(listbox_values)), key='func')], - [g.T(' '*12), g.Exit(size=(5,2))]] +col_listbox = [[sg.Listbox(values=listbox_values, change_submits=True, size=(28, len(listbox_values)), key='func')], + [sg.T(' ' * 12), sg.Exit(size=(5, 2))]] -layout = [[g.Text('Matplotlib Plot Test', font=('current 18'))], - [g.Column(col_listbox, pad=(5,(3,330))), canvas_elem, multiline_elem], +layout = [[sg.Text('Matplotlib Plot Test', font=('current 18'))], + [sg.Column(col_listbox, pad=(5, (3, 330))), canvas_elem, multiline_elem], ] # create the form and show it without the plot -form = g.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') +form = sg.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') form.Layout(layout) while True: diff --git a/Demo_Matplotlib_Ping_Graph.py b/Demo_Matplotlib_Ping_Graph.py index d20755a8..fc8f7cdd 100644 --- a/Demo_Matplotlib_Ping_Graph.py +++ b/Demo_Matplotlib_Ping_Graph.py @@ -1,4 +1,4 @@ -import PySimpleGUI as g +import PySimpleGUI as sg import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasAgg import matplotlib.backends.tkagg as tkagg @@ -640,12 +640,12 @@ def draw(fig, canvas): def main(): global g_my_globals - canvas_elem = g.Canvas(size=SIZE, background_color='white') # get the canvas we'll be drawing on + canvas_elem = sg.Canvas(size=SIZE, background_color='white') # get the canvas we'll be drawing on # define the form layout - layout = [[ canvas_elem, g.ReadFormButton('Exit', pad=(0,(210,0)))] ] + layout = [[canvas_elem, sg.ReadFormButton('Exit', pad=(0, (210, 0)))]] # create the form and show it without the plot - form = g.FlexForm('Ping Graph', background_color='white') + form = sg.FlexForm('Ping Graph', background_color='white') form.Layout(layout) form.ReadNonBlocking() diff --git a/Demo_Matplotlib_Ping_Graph_Large.py b/Demo_Matplotlib_Ping_Graph_Large.py index 20e9b7f8..e24d86a0 100644 --- a/Demo_Matplotlib_Ping_Graph_Large.py +++ b/Demo_Matplotlib_Ping_Graph_Large.py @@ -1,4 +1,4 @@ -import PySimpleGUI as g +import PySimpleGUI as sg import matplotlib.pyplot as plt import ping from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, FigureCanvasAgg @@ -74,14 +74,14 @@ def draw(fig, canvas): def main(): global g_my_globals - canvas_elem = g.Canvas(size=(640, 480)) # get the canvas we'll be drawing on + canvas_elem = sg.Canvas(size=(640, 480)) # get the canvas we'll be drawing on # define the form layout - layout = [[g.Text('Animated Ping', size=(40,1), justification='center', font='Helvetica 20')], + layout = [[sg.Text('Animated Ping', size=(40, 1), justification='center', font='Helvetica 20')], [canvas_elem], - [g.ReadFormButton('Exit', size=(10,2), pad=((280, 0), 3), font='Helvetica 14')]] + [sg.ReadFormButton('Exit', size=(10, 2), pad=((280, 0), 3), font='Helvetica 14')]] # create the form and show it without the plot - form = g.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') + form = sg.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') form.Layout(layout) form.ReadNonBlocking() diff --git a/Demo_Media_Player.py b/Demo_Media_Player.py index 3a78113c..b1128f3e 100644 --- a/Demo_Media_Player.py +++ b/Demo_Media_Player.py @@ -17,14 +17,13 @@ def MediaPlayerGUI(): image_exit = './ButtonGraphics/Exit.png' # A text element that will be changed to display messages in the GUI - TextElem = sg.Text('', size=(15, 2), font=("Helvetica", 14)) # Open a form, note that context manager can't be used generally speaking for async forms form = sg.FlexForm('Media File Player', auto_size_text=True, default_element_size=(20, 1), - font=("Helvetica", 25)) + font=("Helvetica", 25), no_titlebar=True) # define layout of the rows layout= [[sg.Text('Media File Player',size=(17,1), font=("Helvetica", 25))], - [TextElem], + [sg.Text('', size=(15, 2), font=("Helvetica", 14), key='output')], [sg.ReadFormButton('Restart Song', button_color=(background,background), image_filename=image_restart, image_size=(50, 50), image_subsample=2, border_width=0), sg.Text(' ' * 2), @@ -60,7 +59,7 @@ def MediaPlayerGUI(): break # If a button was pressed, display it on the GUI by updating the text element if button: - TextElem.Update(button) + form.FindElement('output').Update(button) MediaPlayerGUI() diff --git a/Demo_NonBlocking_Form.py b/Demo_NonBlocking_Form.py index d4df658b..05006d16 100644 --- a/Demo_NonBlocking_Form.py +++ b/Demo_NonBlocking_Form.py @@ -8,10 +8,9 @@ def StatusOutputExample(): # Make a form, but don't use context manager form = sg.FlexForm('Running Timer', auto_size_text=True) # Create a text element that will be updated with status information on the GUI itself - output_element = sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center') # Create the rows form_rows = [[sg.Text('Non-blocking GUI with updates')], - [output_element], + [sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='output')], [sg.ReadFormButton('LED On'), sg.ReadFormButton('LED Off'), sg.ReadFormButton('Quit')]] # Layout the rows of the form and perform a read. Indicate the form is non-blocking! form.LayoutAndRead(form_rows, non_blocking=True) @@ -25,7 +24,7 @@ def StatusOutputExample(): i=0 while (True): # This is the code that reads and updates your window - output_element.Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100)) + form.FindElement('output').Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100)) button, values = form.ReadNonBlocking() if button == 'Quit' or values is None: break diff --git a/Demo_Pi_LEDs.py b/Demo_Pi_LEDs.py index 1c08b9fe..6fb60ca0 100644 --- a/Demo_Pi_LEDs.py +++ b/Demo_Pi_LEDs.py @@ -1,4 +1,4 @@ -import PySimpleGUI as g +import PySimpleGUI as rg # GUI for switching an LED on and off to GPIO14 @@ -28,18 +28,18 @@ def FlashLED(): GPIO.output(14, GPIO.LOW) time.sleep(0.5) -results_elem = g.T('', size=(30,1)) +results_elem = rg.T('', size=(30, 1)) -layout = [[g.T('Raspberry Pi LEDs')], +layout = [[rg.T('Raspberry Pi LEDs')], [results_elem], - [g.ReadFormButton('Switch LED')], - [g.ReadFormButton('Flash LED')], - [g.ReadFormButton('Show slider value')], - [g.Slider(range=(0, 100), default_value=0, orientation='h', size=(40,20), key='slider')], - [g.Exit()] + [rg.ReadFormButton('Switch LED')], + [rg.ReadFormButton('Flash LED')], + [rg.ReadFormButton('Show slider value')], + [rg.Slider(range=(0, 100), default_value=0, orientation='h', size=(40, 20), key='slider')], + [rg.Exit()] ] -form = g.FlexForm('Raspberry Pi GUI') +form = rg.FlexForm('Raspberry Pi GUI') form.Layout(layout) while True: @@ -56,4 +56,4 @@ while True: elif button is 'Show slider value': results_elem.Update('Slider = %s'%values['slider']) -g.MsgBox('Done... exiting') +rg.MsgBox('Done... exiting') diff --git a/PySimpleGUI.py b/PySimpleGUI.py index e0764e65..765658c1 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -3043,7 +3043,17 @@ def GetPathBox(title, message, default_path='', button_color=None, size=(None, N return True, path -def PopupGetFolder(message, default_path='', button_color=None, size=(None, None)): +def PopupGetFolder(message, default_path='', no_window=False, button_color=None, size=(None, None)): + if no_window: + root = tk.Tk() + try: + root.attributes('-alpha', 0) # hide window while building it. makes for smoother 'paint' + except: + pass + folder_name = tk.filedialog.askdirectory() # show the 'get folder' dialog box + root.destroy() + return folder_name + with FlexForm(title=message, auto_size_text=True, button_color=button_color) as form: layout = [[Text(message, auto_size_text=True)], [InputText(default_text=default_path, size=size), FolderBrowse()], @@ -3078,10 +3088,17 @@ AskForFile = GetFileBox def PopupGetFile(message, default_path='',save_as=False, no_window=False, file_types=(("ALL Files", "*.*"),), button_color=None, size=(None, None)): if no_window: + root = tk.Tk() + try: + root.attributes('-alpha', 0) # hide window while building it. makes for smoother 'paint' + except: + pass if save_as: - return tk.filedialog.asksaveasfilename(filetypes=file_types) # show the 'get file' dialog box + filename = tk.filedialog.asksaveasfilename(filetypes=file_types) # show the 'get file' dialog box else: - return tk.filedialog.askopenfilename(filetypes=file_types) # show the 'get file' dialog box + filename = tk.filedialog.askopenfilename(filetypes=file_types) # show the 'get file' dialog box + root.destroy() + return filename if save_as: browse_button = SaveAs(file_types=file_types)