Changed ReadNonBlocking to Finalize for forms needing to do an Update prior to Read

This commit is contained in:
MikeTheWatchGuy 2018-09-14 12:17:19 -04:00
parent 4f43f7bcaf
commit 21bbb7fca8
12 changed files with 99 additions and 34 deletions

27
Demo_Button_Click.py Normal file
View File

@ -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)

View File

@ -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)

View File

@ -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()

View File

@ -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))]]

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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('<KeyPress-Up>', self.up)
self.canvas.bind_all('<KeyPress-Down>', 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__':

24
Demo_Popups.py Normal file
View File

@ -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'))

View File

@ -209,6 +209,7 @@ def OneLineGUI():
def main():
# button, (filename,) = OneLineGUI()
# DebugTe`st()
sg.MsgBox('Hello')
ChatBot()
Everything()
SourceDestFolders()