From 21bbb7fca856d1b8696ad5527a273fa9148b0611 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Fri, 14 Sep 2018 12:17:19 -0400 Subject: [PATCH] Changed ReadNonBlocking to Finalize for forms needing to do an Update prior to Read --- Demo_Button_Click.py | 27 +++++++++++++ Demo_Canvas.py | 2 +- Demo_Columns.py | 6 +-- Demo_Cookbook_Browser.py | 2 +- Demo_Disable_Elements.py | 2 +- Demo_Matplotlib_Animated.py | 2 +- Demo_Matplotlib_Animated_Scatter.py | 2 +- Demo_Matplotlib_Ping_Graph.py | 2 +- Demo_Matplotlib_Ping_Graph_Large.py | 2 +- Demo_Pong.py | 61 +++++++++++++++++------------ Demo_Popups.py | 24 ++++++++++++ Demo_Recipes.py | 1 + 12 files changed, 99 insertions(+), 34 deletions(-) create mode 100644 Demo_Button_Click.py create mode 100644 Demo_Popups.py diff --git a/Demo_Button_Click.py b/Demo_Button_Click.py new file mode 100644 index 00000000..2a6b89ec --- /dev/null +++ b/Demo_Button_Click.py @@ -0,0 +1,27 @@ +import PySimpleGUI as sg +import winsound + + +sg.ChangeLookAndFeel('Dark') +sg.SetOptions(element_padding=(0,0)) + +layout = [ + [sg.ReadFormButton('Start', button_color=('white', 'black'), key='start'), + sg.ReadFormButton('Stop', button_color=('white', 'black'), key='stop'), + sg.ReadFormButton('Reset', button_color=('white', 'firebrick3'), key='reset'), + sg.ReadFormButton('Submit', button_color=('white', 'springgreen4'), key='submit')] + ] + +form = sg.FlexForm("Button Click", default_element_size=(12,1), text_justification='r', auto_size_text=False, auto_size_buttons=False, + default_button_element_size=(12,1)) +form.Layout(layout) +form.Finalize() + +form.FindElement('submit').Update(disabled=True) + +recording = have_data = False +while True: + button, values = form.Read() + if button is None: + exit(69) + winsound.PlaySound("ButtonClick.wav", 1) diff --git a/Demo_Canvas.py b/Demo_Canvas.py index 893c9d4f..a717c4a9 100644 --- a/Demo_Canvas.py +++ b/Demo_Canvas.py @@ -7,7 +7,7 @@ layout = [ form = gui.FlexForm('Canvas test') form.Layout(layout) -form.ReadNonBlocking() +form.Finalize() cir = form.FindElement('canvas').TKCanvas.create_oval(50, 50, 100, 100) diff --git a/Demo_Columns.py b/Demo_Columns.py index d58a8fe2..9ca240a2 100644 --- a/Demo_Columns.py +++ b/Demo_Columns.py @@ -35,7 +35,7 @@ def ScrollableColumns(): layout = [[sg.Column(column2, scrollable=True), sg.Column(column1, scrollable=True, size=(200,150))], [sg.OK()]] - form = sg.FlexForm('Form Fill Demonstration', default_element_size=(40, 1)) + form = sg.FlexForm('Form Fill Demonstration', grab_anywhere=False, default_element_size=(40, 1)) b, v = form.LayoutAndRead(layout) sg.Popup(v) @@ -53,9 +53,9 @@ def NormalColumns(): # Display the form and get values # If you're willing to not use the "context manager" design pattern, then it's possible # to collapse the form display and read down to a single line of code. - button, values = sg.FlexForm('Compact 1-line form with column').LayoutAndRead(layout) + button, values = sg.FlexForm('Compact 1-line form with column', grab_anywhere=False).LayoutAndRead(layout) sg.Popup(button, values, line_width=200) -NormalColumns() +# NormalColumns() ScrollableColumns() \ No newline at end of file diff --git a/Demo_Cookbook_Browser.py b/Demo_Cookbook_Browser.py index f188253d..2eeb06d1 100644 --- a/Demo_Cookbook_Browser.py +++ b/Demo_Cookbook_Browser.py @@ -754,7 +754,7 @@ listbox_values = [key for key in fig_dict.keys()] while True: sg.ChangeLookAndFeel('Dark') - sg.SetOptions(element_padding=(0,0)) + # sg.SetOptions(element_padding=(0,0)) col_listbox = [[sg.Listbox(values=listbox_values, size=(max(len(x) for x in listbox_values),min(len(listbox_values), 20)), change_submits=False, key='func')], [sg.ReadFormButton('Run', pad=(0,0)), sg.ReadFormButton('Show Code', button_color=('white', 'gray25'), pad=(0,0)), sg.Exit(button_color=('white', 'firebrick4'), pad=(0,0))]] diff --git a/Demo_Disable_Elements.py b/Demo_Disable_Elements.py index 4741c4b0..cf351a8f 100644 --- a/Demo_Disable_Elements.py +++ b/Demo_Disable_Elements.py @@ -26,7 +26,7 @@ form = sg.FlexForm("Time Tracker", default_element_size=(12, 1), text_justificat form.Layout(layout) -form.ReadNonBlocking() +form.Finalize() form.FindElement('cbox').Update(disabled=True) form.FindElement('listbox').Update(disabled=True) diff --git a/Demo_Matplotlib_Animated.py b/Demo_Matplotlib_Animated.py index 60be75cc..da813c47 100644 --- a/Demo_Matplotlib_Animated.py +++ b/Demo_Matplotlib_Animated.py @@ -25,7 +25,7 @@ def main(): # create the form and show it without the plot form = sg.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') form.Layout(layout) - form.ReadNonBlocking() + form.Finalize() graph = FigureCanvasTkAgg(fig, master=canvas_elem.TKCanvas) canvas = canvas_elem.TKCanvas diff --git a/Demo_Matplotlib_Animated_Scatter.py b/Demo_Matplotlib_Animated_Scatter.py index 81902298..0005d681 100644 --- a/Demo_Matplotlib_Animated_Scatter.py +++ b/Demo_Matplotlib_Animated_Scatter.py @@ -16,7 +16,7 @@ def main(): # create the form and show it without the plot form = sg.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') form.Layout(layout) - form.ReadNonBlocking() + form.Finalize() canvas = canvas_elem.TKCanvas diff --git a/Demo_Matplotlib_Ping_Graph.py b/Demo_Matplotlib_Ping_Graph.py index c58c2874..39111a98 100644 --- a/Demo_Matplotlib_Ping_Graph.py +++ b/Demo_Matplotlib_Ping_Graph.py @@ -647,7 +647,7 @@ def main(): # create the form and show it without the plot form = sg.FlexForm('Ping Graph', background_color='white', grab_anywhere=True) form.Layout(layout) - form.ReadNonBlocking() + form.Finalize() canvas = canvas_elem.TKCanvas diff --git a/Demo_Matplotlib_Ping_Graph_Large.py b/Demo_Matplotlib_Ping_Graph_Large.py index e24d86a0..23bb678f 100644 --- a/Demo_Matplotlib_Ping_Graph_Large.py +++ b/Demo_Matplotlib_Ping_Graph_Large.py @@ -83,7 +83,7 @@ def main(): # create the form and show it without the plot form = sg.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') form.Layout(layout) - form.ReadNonBlocking() + form.Finalize() canvas = canvas_elem.TKCanvas diff --git a/Demo_Pong.py b/Demo_Pong.py index a56580cd..0c03d69b 100644 --- a/Demo_Pong.py +++ b/Demo_Pong.py @@ -1,5 +1,7 @@ import random -import PySimpleGUI as gui +import PySimpleGUI as sg +import time + """ Pong code supplied by Daniel Young (Neonzz) Modified. Original code: https://www.pygame.org/project/3649/5739 @@ -23,20 +25,12 @@ class Ball: def checkwin(self): winner = None - if self.playerScore == 10: + if self.playerScore >= 10: winner = 'Player left wins' - # gameOver = True - if self.player1Score == 10: + if self.player1Score >= 10: winner = 'Player Right' - # gameOver = True return winner - def checkForgameOver(self): - gameOver = False - if self.playerScore == 10 or self.player1Score == 10: - gameOver = True - return gameOver - return gameOver def updatep(self, val): self.canvas.delete(self.drawP) @@ -91,9 +85,6 @@ class pongbat(): self.canvas_width = self.canvas.winfo_width() self.y = 0 - self.canvas.bind_all('w', self.up) - self.canvas.bind_all('s', self.down) - def up(self, evt): self.y = -5 @@ -106,7 +97,7 @@ class pongbat(): if pos[1] <= 0: self.y = 0 if pos[3] >= 400: - self.y = -0 + self.y = 0 class pongbat2(): @@ -116,8 +107,6 @@ class pongbat2(): self.canvas_height = self.canvas.winfo_height() self.canvas_width = self.canvas.winfo_width() self.y = 0 - self.canvas.bind_all('', self.up) - self.canvas.bind_all('', self.down) def up(self, evt): self.y = -5 @@ -131,33 +120,57 @@ class pongbat2(): if pos[1] <= 0: self.y = 0 if pos[3] >= 400: - self.y = -0 + self.y = 0 def pong(): - layout = [ [gui.Canvas(size=(700, 400), background_color='black', key='canvas')], - [gui.T(''), gui.ReadFormButton('Quit')]] - - form = gui.FlexForm('Canvas test') + # ------------- Define GUI layout ------------- + layout = [[sg.Canvas(size=(700, 400), background_color='black', key='canvas')], + [sg.T(''), sg.ReadFormButton('Quit')]] + # ------------- Create window ------------- + form = sg.FlexForm('Canvas test', return_keyboard_events=True) form.Layout(layout) - form.ReadNonBlocking() # TODO Replace with call to Finalize once code released + form.ReadNonBlocking() # TODO Replace with call to form.Finalize once code released + # ------------- Get the tkinter Canvas we're drawing on ------------- canvas = form.FindElement('canvas').TKCanvas + # ------------- Create line down center, the bats and ball ------------- canvas.create_line(350, 0, 350, 400, fill='white') bat1 = pongbat(canvas, 'white') bat2 = pongbat2(canvas, 'white') ball1 = Ball(canvas, bat1, bat2, 'green') + # ------------- Event Loop ------------- while True: + # ------------- Draw ball and bats ------------- ball1.draw() bat1.draw() bat2.draw() + + # ------------- Read the form, get keypresses ------------- button, values = form.ReadNonBlocking() + + # ------------- If quit ------------- if button is None and values is None or button == 'Quit': exit(69) + # ------------- Keypresses ------------- + if button is not None: + if button.startswith('Up'): + bat2.up(2) + elif button.startswith('Down'): + bat2.down(2) + elif button == 'w': + bat1.up(1) + elif button == 's': + bat1.down(1) + if ball1.checkwin(): - gui.Popup('Game End', ball1.checkwin() + ' won!!') + sg.Popup('Game Over', ball1.checkwin() + ' won!!') + + + # ------------- Bottom of loop, delay between animations ------------- + # time.sleep(.01) canvas.after(10) if __name__ == '__main__': diff --git a/Demo_Popups.py b/Demo_Popups.py new file mode 100644 index 00000000..f5692b9f --- /dev/null +++ b/Demo_Popups.py @@ -0,0 +1,24 @@ +import PySimpleGUI as sg + + + +print(sg.PopupGetFolder('Get text', background_color='blue', text_color='white')) +print(sg.PopupGetFile('Get text', background_color='blue', text_color='white')) +print(sg.PopupGetFolder('Get text', background_color='blue', text_color='white')) + + +sg.Popup('Simple popup') + +sg.PopupNonBlocking('Non Blocking') +sg.PopupError('Error') +sg.PopupYesNo('Yes No') +sg.PopupNoTitlebar('No titlebar') +sg.PopupNoBorder('No border') +sg.PopupNoFrame('No frame') +sg.PopupNoButtons('No Buttons') +sg.PopupCancel('Cancel') +sg.PopupOKCancel('OK Cancel') +sg.PopupAutoClose('Autoclose') +print(sg.PopupGetText('Get text')) +print(sg.PopupGetFile('Get File')) +print(sg.PopupGetFolder('Get folder')) diff --git a/Demo_Recipes.py b/Demo_Recipes.py index fe7c403a..972a199a 100644 --- a/Demo_Recipes.py +++ b/Demo_Recipes.py @@ -209,6 +209,7 @@ def OneLineGUI(): def main(): # button, (filename,) = OneLineGUI() # DebugTe`st() + sg.MsgBox('Hello') ChatBot() Everything() SourceDestFolders()