0.12.0, restructure folders, multi-window design patterns
This commit is contained in:
parent
9b26e63f44
commit
0763fcf254
12 changed files with 342 additions and 214 deletions
|
@ -0,0 +1,59 @@
|
|||
import sys
|
||||
if sys.version_info[0] >= 3:
|
||||
import PySimpleGUIQt as sg
|
||||
else:
|
||||
import PySimpleGUI27 as sg
|
||||
|
||||
"""
|
||||
Demo - Running 2 windows with both being active at the same time
|
||||
Three important things to note about this design patter:
|
||||
1. The layout for window 2 is inside of the while loop, just before the call to window2=sg.Window
|
||||
2. The read calls have timeout values of 100 and 0. You can change the 100 to whatever interval you wish
|
||||
but must keep the second window's timeout at 0
|
||||
3. There is a safeguard to stop from launching multiple copies of window2. Only 1 window2 is visible at a time
|
||||
"""
|
||||
|
||||
# Window 1 layout
|
||||
layout = [
|
||||
[sg.Text('This is the FIRST WINDOW'), sg.Text(' ', key='_OUTPUT_')],
|
||||
[sg.Text('')],
|
||||
[sg.Button('Launch 2nd Window'),sg.Button('Popup'), sg.Button('Exit')]
|
||||
]
|
||||
|
||||
window = sg.Window('Window Title', location=(800,600)).Layout(layout)
|
||||
win2_active = False
|
||||
i=0
|
||||
while True: # Event Loop
|
||||
event, values = window.Read(timeout=100)
|
||||
if event != sg.TIMEOUT_KEY:
|
||||
print(i, event, values)
|
||||
|
||||
if event is None or event == 'Exit':
|
||||
break
|
||||
elif event == 'Popup':
|
||||
sg.Popup('This is a BLOCKING popup','all windows remain inactive while popup active')
|
||||
i+=1
|
||||
if event == 'Launch 2nd Window' and not win2_active: # only run if not already showing a window2
|
||||
win2_active = True
|
||||
# window 2 layout - note - must be "new" every time a window is created
|
||||
layout2 = [
|
||||
[sg.Text('The second window'), sg.Text('', key='_OUTPUT_')],
|
||||
[sg.Input(do_not_clear=True, key='_IN_')],
|
||||
[sg.Button('Show'), sg.Button('Exit')]
|
||||
]
|
||||
window2 = sg.Window('Second Window').Layout(layout2)
|
||||
# Read window 2's events. Must use timeout of 0
|
||||
if win2_active:
|
||||
# print("reading 2")
|
||||
event, values = window2.Read(timeout=100)
|
||||
# print("win2 ", event)
|
||||
if event != sg.TIMEOUT_KEY:
|
||||
print("win2 ", event)
|
||||
if event == 'Exit' or event is None:
|
||||
# print("Closing window 2", event)
|
||||
win2_active = False
|
||||
window2.Close()
|
||||
if event == 'Show':
|
||||
sg.Popup('You entered ', values['_IN_'])
|
||||
|
||||
window.Close()
|
|
@ -0,0 +1,35 @@
|
|||
"""
|
||||
PySimpleGUI The Complete Course
|
||||
Lesson 7 - Multiple Windows
|
||||
"""
|
||||
import PySimpleGUIQt as sg
|
||||
|
||||
# Design pattern 1 - First window does not remain active
|
||||
|
||||
layout = [[ sg.Text('Window 1'),],
|
||||
[sg.Input(do_not_clear=True)],
|
||||
[sg.Text('', key='_OUTPUT_')],
|
||||
[sg.Button('Launch 2')]]
|
||||
|
||||
win1 = sg.Window('Window 1').Layout(layout)
|
||||
win2_active=False
|
||||
while True:
|
||||
ev1, vals1 = win1.Read(timeout=100)
|
||||
if ev1 is None:
|
||||
break
|
||||
win1.FindElement('_OUTPUT_').Update(vals1[0])
|
||||
|
||||
if ev1 == 'Launch 2' and not win2_active:
|
||||
win2_active = True
|
||||
win1.Disappear()
|
||||
layout2 = [[sg.Text('Window 2')],
|
||||
[sg.Button('Exit')]]
|
||||
|
||||
win2 = sg.Window('Window 2').Layout(layout2)
|
||||
while True:
|
||||
ev2, vals2 = win2.Read()
|
||||
if ev2 is None or ev2 == 'Exit':
|
||||
win2.Close()
|
||||
win2_active = False
|
||||
win1.Reappear()
|
||||
break
|
|
@ -0,0 +1,34 @@
|
|||
"""
|
||||
PySimpleGUI The Complete Course
|
||||
Lesson 7 - Multiple Windows
|
||||
"""
|
||||
import PySimpleGUI as sg
|
||||
|
||||
# Design pattern 2 - First window remains active
|
||||
|
||||
layout = [[ sg.Text('Window 1'),],
|
||||
[sg.Input(do_not_clear=True)],
|
||||
[sg.Text('', key='_OUTPUT_')],
|
||||
[sg.Button('Launch 2'), sg.Button('Exit')]]
|
||||
|
||||
win1 = sg.Window('Window 1').Layout(layout)
|
||||
|
||||
win2_active = False
|
||||
while True:
|
||||
ev1, vals1 = win1.Read(timeout=100)
|
||||
win1.FindElement('_OUTPUT_').Update(vals1[0])
|
||||
if ev1 is None or ev1 == 'Exit':
|
||||
break
|
||||
|
||||
if not win2_active and ev1 == 'Launch 2':
|
||||
win2_active = True
|
||||
layout2 = [[sg.Text('Window 2')],
|
||||
[sg.Button('Exit')]]
|
||||
|
||||
win2 = sg.Window('Window 2').Layout(layout2)
|
||||
|
||||
if win2_active:
|
||||
ev2, vals2 = win2.Read(timeout=100)
|
||||
if ev2 is None or ev2 == 'Exit':
|
||||
win2_active = False
|
||||
win2.Close()
|
93
PySimpleGUIQt/Demo Programs/Demo_HowDoI.py
Normal file
93
PySimpleGUIQt/Demo Programs/Demo_HowDoI.py
Normal file
|
@ -0,0 +1,93 @@
|
|||
#!/usr/bin/env python
|
||||
import sys
|
||||
import PySimpleGUIQt as sg
|
||||
import subprocess
|
||||
|
||||
|
||||
# Test this command in a dos window if you are having trouble.
|
||||
HOW_DO_I_COMMAND = 'python -m howdoi.howdoi'
|
||||
|
||||
# if you want an icon on your taskbar for this gui, then change this line of code to point to the ICO file
|
||||
DEFAULT_ICON = 'question.ico'
|
||||
|
||||
def HowDoI():
|
||||
'''
|
||||
Make and show a window (PySimpleGUI form) that takes user input and sends to the HowDoI web oracle
|
||||
Excellent example of 2 GUI concepts
|
||||
1. Output Element that will show text in a scrolled window
|
||||
2. Non-Window-Closing Buttons - These buttons will cause the form to return with the form's values, but doesn't close the form
|
||||
:return: never returns
|
||||
'''
|
||||
# ------- Make a new Window ------- #
|
||||
sg.ChangeLookAndFeel('GreenTan') # give our form a spiffy set of colors
|
||||
|
||||
layout = [
|
||||
[sg.Text('Ask and your answer will appear here....')],
|
||||
[sg.Output(size=(900, 500), font=('Courier', 10))],
|
||||
[ sg.Spin(values=(1, 4), initial_value=1, size=(50, 25), key='Num Answers', font=('Helvetica', 15)),
|
||||
sg.Text('Num Answers',font=('Helvetica', 15), size=(170,22)), sg.Checkbox('Display Full Text', key='full text', font=('Helvetica', 15), size=(200,22)),
|
||||
sg.T('Command History', font=('Helvetica', 15)), sg.T('', size=(100,25), text_color=sg.BLUES[0], key='history'), sg.Stretch()],
|
||||
[sg.Multiline(size=(600, 100), enter_submits=True, focus=True, key='query', do_not_clear=False), sg.Stretch(),
|
||||
sg.ReadButton('SEND', button_color=(sg.YELLOWS[0], sg.BLUES[0]), bind_return_key=True),
|
||||
sg.Button('EXIT', button_color=(sg.YELLOWS[0], sg.GREENS[0])), sg.Stretch()]
|
||||
]
|
||||
|
||||
window = sg.Window('How Do I ??',
|
||||
default_element_size=(100, 25),
|
||||
# element_padding=(10,10),
|
||||
icon=DEFAULT_ICON,
|
||||
font=('Helvetica',14),
|
||||
default_button_element_size=(70,50),
|
||||
return_keyboard_events=True,
|
||||
no_titlebar=False,
|
||||
grab_anywhere=True,)
|
||||
|
||||
window.Layout(layout)
|
||||
# ---===--- Loop taking in user input and using it to query HowDoI --- #
|
||||
command_history = []
|
||||
history_offset = 0
|
||||
while True:
|
||||
event, values = window.Read()
|
||||
if event == 'SEND' or event == 'query':
|
||||
# window.FindElement('+OUTPUT+').Update('test of output') # manually clear input because keyboard events blocks clear
|
||||
|
||||
query = values['query'].rstrip()
|
||||
# print(query)
|
||||
QueryHowDoI(query, values['Num Answers'], values['full text']) # send the string to HowDoI
|
||||
command_history.append(query)
|
||||
history_offset = len(command_history)-1
|
||||
window.FindElement('query').Update('') # manually clear input because keyboard events blocks clear
|
||||
window.FindElement('history').Update('\n'.join(command_history[-3:]))
|
||||
elif event == None or event == 'EXIT': # if exit button or closed using X
|
||||
break
|
||||
elif 'Up' in event and len(command_history): # scroll back in history
|
||||
command = command_history[history_offset]
|
||||
history_offset -= 1 * (history_offset > 0) # decrement is not zero
|
||||
window.FindElement('query').Update(command)
|
||||
elif 'Down' in event 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]
|
||||
window.FindElement('query').Update(command)
|
||||
elif 'Escape' in event: # clear currently line
|
||||
window.FindElement('query').Update('')
|
||||
|
||||
|
||||
def QueryHowDoI(Query, num_answers, full_text):
|
||||
'''
|
||||
Kicks off a subprocess to send the 'Query' to HowDoI
|
||||
Prints the result, which in this program will route to a gooeyGUI window
|
||||
:param Query: text english question to ask the HowDoI web engine
|
||||
:return: nothing
|
||||
'''
|
||||
howdoi_command = HOW_DO_I_COMMAND
|
||||
full_text_option = ' -a' if full_text else ''
|
||||
t = subprocess.Popen(howdoi_command + ' \"'+ Query + '\" -n ' + str(num_answers)+full_text_option, stdout=subprocess.PIPE)
|
||||
(output, err) = t.communicate()
|
||||
print('{:^88}'.format(Query.rstrip()))
|
||||
print('_'*60)
|
||||
print(output.decode("utf-8") )
|
||||
exit_code = t.wait()
|
||||
|
||||
if __name__ == '__main__':
|
||||
HowDoI()
|
||||
|
57
PySimpleGUIQt/Demo Programs/Qt_All_Widgets.py
Normal file
57
PySimpleGUIQt/Demo Programs/Qt_All_Widgets.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
#!/usr/bin/env python
|
||||
import PySimpleGUIQt as sg
|
||||
|
||||
sg.ChangeLookAndFeel('Topanga')
|
||||
# ------ Column Definition ------ #
|
||||
column1 = [[sg.Text('Column 1', background_color='lightblue',text_color='black', justification='center', size=(100,22))],
|
||||
[sg.Spin((1,10), size=(100,22))],
|
||||
[sg.Spin((1,10), size=(100,22))],
|
||||
[sg.Spin((1,10), size=(100,22))],]
|
||||
|
||||
layout = [
|
||||
[sg.Text('(Almost) All widgets in one Window!', justification='c', font=("Helvetica", 25), relief=sg.RELIEF_RIDGE)],
|
||||
[sg.Text('Here is some text.... and a place to enter text')],
|
||||
[sg.InputText('This is my text', size=(400,22))],
|
||||
[sg.Frame(layout=[
|
||||
[sg.Checkbox('Checkbox', size=(185,22)), sg.Checkbox('My second checkbox!', default=True)],
|
||||
[sg.Radio('My first Radio!', "RADIO1", default=True, size=(180,22), ),sg.Radio('My second Radio!', "RADIO1")],
|
||||
[sg.Radio('Third Radio!', "RADIO2", default=True, size=(180,22), ),
|
||||
sg.Radio('Fourth Radio!', "RADIO2")]], title='Options',title_color='red', relief=sg.RELIEF_SUNKEN,
|
||||
tooltip='Use these to set flags', ), sg.Stretch()],
|
||||
[sg.Multiline(default_text='This is the default Text should you decide not to type anything', size=(220, 80)),
|
||||
sg.Multiline(default_text='A second multi-line', size=(220, 80))],
|
||||
[sg.InputCombo(('Combobox 1', 'Combobox 2'), size=(150, 22)), sg.Stretch(),
|
||||
sg.Slider(range=(1, 100), orientation='h', size=(300, 22), default_value=85)],
|
||||
[sg.InputOptionMenu(('Menu Option 1', 'Menu Option 2', 'Menu Option 3'))],
|
||||
[sg.Listbox(values=('Listbox 1', 'Listbox 2', 'Listbox 3'), size=(200,100), select_mode=sg.LISTBOX_SELECT_MODE_EXTENDED), sg.Stretch(),
|
||||
sg.Frame('Labelled Group',[[
|
||||
sg.Slider(range=(1, 100), orientation='v', default_value=25, tick_interval=25),
|
||||
sg.Slider(range=(1, 100), orientation='v', default_value=75),
|
||||
sg.Slider(range=(1, 100), orientation='v', default_value=10),
|
||||
sg.Column(column1, background_color='lightblue')]], background_color='black'), sg.Stretch()],
|
||||
[sg.Text('_' * 50, justification='c')],
|
||||
[sg.Text('Choose A Folder')],
|
||||
[sg.Text('Your Folder'),
|
||||
sg.InputText('Default Folder', size=(300,22)), sg.FolderBrowse(), sg.Stretch()],
|
||||
[sg.Submit(tooltip='Click to submit this form',), sg.Cancel()]]
|
||||
|
||||
window = sg.Window('Everything bagel',
|
||||
grab_anywhere=False,
|
||||
font=('Helvetica', 12),
|
||||
no_titlebar=False,
|
||||
alpha_channel=1,
|
||||
keep_on_top=False,
|
||||
element_padding=(2,3),
|
||||
default_element_size=(100, 23),
|
||||
default_button_element_size=(120,30)
|
||||
).Layout(layout)
|
||||
event, values = window.Read()
|
||||
print(event, values)
|
||||
window.Close()
|
||||
|
||||
sg.Popup('Title',
|
||||
'The results of the window.',
|
||||
'The button clicked was "{}"'.format(event),
|
||||
'The values are', values)
|
||||
|
||||
|
59
PySimpleGUIQt/Demo Programs/Qt_Demo_OpenCV_Webcam.py
Normal file
59
PySimpleGUIQt/Demo Programs/Qt_Demo_OpenCV_Webcam.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
#!/usr/bin/env python
|
||||
import sys
|
||||
if sys.version_info[0] >= 3:
|
||||
import PySimpleGUIQt as sg
|
||||
else:
|
||||
import PySimpleGUI27 as sg
|
||||
import cv2
|
||||
import numpy as np
|
||||
from sys import exit as exit
|
||||
|
||||
"""
|
||||
Demo program that displays a webcam using OpenCV
|
||||
"""
|
||||
def main():
|
||||
|
||||
sg.ChangeLookAndFeel('LightGreen')
|
||||
|
||||
# define the window layout
|
||||
layout = [[sg.Text('OpenCV Demo', size=(40, 1), justification='center', font='Helvetica 20')],
|
||||
[sg.Image(filename='', key='image')],
|
||||
[sg.Button('Record', size=(10, 1), font='Helvetica 14'),
|
||||
sg.Button('Stop', size=(10, 1), font='Any 14'),
|
||||
sg.Button('Exit', size=(10, 1), font='Helvetica 14'),
|
||||
sg.Button('About', size=(10,1), font='Any 14')]]
|
||||
|
||||
# create the window and show it without the plot
|
||||
window = sg.Window('Demo Application - OpenCV Integration',
|
||||
location=(800,400))
|
||||
window.Layout(layout)
|
||||
|
||||
# ---===--- Event LOOP Read and display frames, operate the GUI --- #
|
||||
cap = cv2.VideoCapture(0)
|
||||
recording = False
|
||||
while True:
|
||||
event, values = window.Read(timeout=0, timeout_key='timeout')
|
||||
if event == 'Exit' or event is None:
|
||||
sys.exit(0)
|
||||
pass
|
||||
elif event == 'Record':
|
||||
recording = True
|
||||
elif event == 'Stop':
|
||||
recording = False
|
||||
# img = np.full((480, 640),255)
|
||||
# imgbytes=cv2.imencode('.png', img)[1].tobytes() #this is faster, shorter and needs less includes
|
||||
# window.FindElement('image').Update(data=imgbytes)
|
||||
elif event == 'About':
|
||||
sg.PopupNoWait('Made with PySimpleGUI',
|
||||
'www.PySimpleGUI.org',
|
||||
'Check out how the video keeps playing behind this window.',
|
||||
'I finally figured out how to display frames from a webcam.',
|
||||
'ENJOY! Go make something really cool with this... please!',
|
||||
keep_on_top=True)
|
||||
if recording:
|
||||
ret, frame = cap.read()
|
||||
imgbytes=cv2.imencode('.png', frame)[1].tobytes() #ditto
|
||||
window.FindElement('image').Update(data=imgbytes)
|
||||
|
||||
main()
|
||||
exit()
|
22
PySimpleGUIQt/Demo Programs/Qt_Dial.py
Normal file
22
PySimpleGUIQt/Demo Programs/Qt_Dial.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
import PySimpleGUIQt as sg
|
||||
|
||||
layout = [
|
||||
[sg.Text('This is the new Dial Element!')],
|
||||
[sg.T(' ', size=(70,10)), sg.T('0', key='+DIAL_VALUE+', font=('Helvetica', 15))],
|
||||
[sg.Dial(range=(1,100), key='_DIAL_', change_submits=True)],
|
||||
[sg.Slider((1,100), orientation='h', key='_SLIDER_', change_submits=True),
|
||||
sg.T(' 1', key='+SLIDER_VALUE+', font=('Helvetica', 15))],
|
||||
[sg.T('1' + 30*' ' + '100')],
|
||||
[sg.Button('Show'), sg.Button('Exit')]
|
||||
]
|
||||
|
||||
window = sg.Window('Window Title').Layout(layout)
|
||||
|
||||
while True: # Event Loop
|
||||
event, values = window.Read()
|
||||
if event is None or event == 'Exit':
|
||||
break
|
||||
window.FindElement('+DIAL_VALUE+').Update(values['_DIAL_'])
|
||||
window.FindElement('+SLIDER_VALUE+').Update(values['_SLIDER_'])
|
||||
|
||||
window.Close()
|
30
PySimpleGUIQt/Demo Programs/Qt_Test.py
Normal file
30
PySimpleGUIQt/Demo Programs/Qt_Test.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
import PySimpleGUIQt as sg
|
||||
# sg.Popup('test 1')
|
||||
# sg.Popup('test 2')
|
||||
sg.ChangeLookAndFeel('GreenTan')
|
||||
layout = [
|
||||
[sg.Text('Hello From PySimpleGUIQt!', text_color='red', tooltip='This is my tooltip', justification='c', font=('Courier', 22), key='_TEXT_')],
|
||||
[sg.Text('Input something here'),sg.Stretch(), sg.Input('This is an InputText Element', key='_INPUT_', font=('Any', 14))],
|
||||
[sg.Text('This is the new Dial Element'), sg.Dial(background_color='red'), sg.Stretch()],
|
||||
[sg.Combo(['Combo 1', 'Combo 2', 'Combo 3'], key='+COMBO+', size=(150,30), text_color='green')],
|
||||
[sg.Listbox(['Listbox Item 1', 'Listbox Item 2', 'Listbox Item 3'], key='+LIST+', size=(200,150), text_color='blue'),sg.Slider((1,100), orientation='v', key='+SLIDER 1+')],
|
||||
[sg.Slider((1,10), size=(200,30), orientation='h', key='+SLIDER 2+'), sg.Stretch()],
|
||||
[sg.Checkbox('Checkbox 1', key='+CB1+'), sg.Checkbox('Checkbox 2', key='+CB2')],
|
||||
[sg.Checkbox('Checkbox 3'), sg.Checkbox('Checkbox 4')],
|
||||
[sg.Radio('Radio1', group_id=1),sg.Radio('Radio2', group_id=1)],
|
||||
[sg.Spin((5,8), size=(100,30))],
|
||||
[sg.Multiline('This is a Multiline Element', size=(300,300), key='+MULTI+')],
|
||||
[sg.Button('My Button', size=(120,30)), sg.Exit(), sg.Button('Change', key='_CHANGE_')],
|
||||
]
|
||||
|
||||
window = sg.Window('My first QT Window', auto_size_text=True, auto_size_buttons=False, font=('Helvetica', 16)).Layout(layout)
|
||||
|
||||
while True:
|
||||
event, values = window.Read()
|
||||
print(event, values)
|
||||
if event is None or event == 'Exit':
|
||||
break
|
||||
|
||||
window.FindElement('_TEXT_').Update(values['_INPUT_'], font=('Helvetica', 30))
|
||||
if event == '_CHANGE_':
|
||||
window.FindElement('_CHANGE_').Update('Disabled', disabled=True, button_color=('gray', 'gray20'),)
|
101
PySimpleGUIQt/Demo Programs/Qt_Widget_Summary.py
Normal file
101
PySimpleGUIQt/Demo Programs/Qt_Widget_Summary.py
Normal file
File diff suppressed because one or more lines are too long
BIN
PySimpleGUIQt/Demo Programs/question.ico
Normal file
BIN
PySimpleGUIQt/Demo Programs/question.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 43 KiB |
Loading…
Add table
Add a link
Reference in a new issue