Here's what I think you are looking for your code to do:
import PySimpleGUI as sg import shutil, errno src = "" dest = "" def copy(src, dest): try: shutil.copytree(src, dest) except OSError as e: # If the error was caused because the source wasn 't a directory if e.errno == errno.ENOTDIR: shutil.copy(src, dest) else: print('Directory not copied. Error: %s' % e) #Me testing out commands in PSG layout = [ [sg.Text("Select path from source to destination")], [sg.Text("Source Folder", size = (15, 1)), sg.InputText(src), sg.FolderBrowse() ], [sg.Text("Destination Folder", size = (15, 1)), sg.InputText(dest), sg.FolderBrowse() ], [sg.Button("Transfer", button_color = ("white", "blue"), size = (6, 1)), sg.Button("Copy", button_color = ("white", "green"), size = (6, 1)), sg.Exit(button_color = ("white", "red"), size = (6, 1))] ] window = sg.Window("Mass File Transfer").Layout(layout) while True: event, values = window.Read() print(event, values) if event in (None, 'Exit'): break if event == 'Copy': copy(values[0], values[1])
PySimpleGUI.Text() , PySimpleGUI.Window()
def DownloadSubtitlesGUI():
sg.ChangeLookAndFeel('Dark')
combobox = sg.InputCombo(values = ['', ], size = (10, 1), key = 'lang')
layout = [
[sg.Text('Subtitle Grabber', size = (40, 1), font = ('Any 15'))],
[sg.T('YouTube Link'), sg.In(default_text = '', size = (60, 1), key = 'link', do_not_clear = True)],
[sg.Output(size = (90, 20), font = 'Courier 12')],
[sg.Button('Get List')],
[sg.T('Language Code'), combobox, sg.Button('Download')],
[sg.Button('Exit', button_color = ('white', 'firebrick3'))]
]
window = sg.Window('Subtitle Grabber launcher', text_justification = 'r', default_element_size = (15, 1), font = ('Any 14')).Layout(layout)
#-- -= == -- - Loop taking in user input and using it to query HowDoI-- - #
while True:
event, values = window.Read()
if event in ('Exit', None):
break # exit button clicked
link = values['link']
if event == 'Get List':
print('Getting list of subtitles....')
window.Refresh()
command = [f 'C:\\Python\\Anaconda3\\Scripts\\youtube-dl.exe --list-subs {link}', ]
output = ExecuteCommandSubprocess(command, wait = True, quiet = True)
lang_list = [o[: 5].rstrip() for o in output.split('\n') if 'vtt' in o]
lang_list = sorted(lang_list)
combobox.Update(values = lang_list)
print('Done')
elif event == 'Download':
lang = values['lang'] or 'en'
print(f 'Downloading subtitle for {lang}...')
window.Refresh()
command = [f 'C:\\Python\\Anaconda3\\Scripts\\youtube-dl.exe --sub-lang {lang} --write-sub {link}', ]
print(ExecuteCommandSubprocess(command, wait = True, quiet = False))
print('Done')
def main():
layout = [
[sg.Text('Enter the command you wish to run')],
[sg.Input(key = '_IN_')],
[sg.Output(size = (60, 15))],
[sg.Button('Run'), sg.Button('Exit')]
]
window = sg.Window('Realtime Shell Command Output', layout)
while True: # Event Loop
event, values = window.Read()
# print(event, values)
if event in (None, 'Exit'):
break
elif event == 'Run':
runCommand(cmd = values['_IN_'], window = window)
window.Close()
def main():
MLINE_KEY = '-ML-' + sg.WRITE_ONLY_KEY # multiline element 's key. Indicate it'
s an output only element
MLINE_KEY2 = '-ML2-' + sg.WRITE_ONLY_KEY # multiline element 's key. Indicate it'
s an output only element
MLINE_KEY3 = '-ML3-' + sg.WRITE_ONLY_KEY # multiline element 's key. Indicate it'
s an output only element
output_key = MLINE_KEY
layout = [
[sg.Text('Multiline Color Print Demo', font = 'Any 18')],
[sg.Multiline('Multiline\n', size = (80, 20), key = MLINE_KEY)],
[sg.Multiline('Multiline2\n', size = (80, 20), key = MLINE_KEY2)],
[sg.Text('Text color:'), sg.Combo(list(color_map.keys()), size = (12, 20), key = '-TEXT COLOR-'),
sg.Text('on Background color:'), sg.Combo(list(color_map.keys()), size = (12, 20), key = '-BG COLOR-')
],
[sg.Input('Type text to output here', size = (80, 1), key = '-IN-')],
[sg.Button('Print', bind_return_key = True), sg.Button('Print short'),
sg.Button('Force 1'), sg.Button('Force 2'),
sg.Button('Use Input for colors'), sg.Button('Toggle Output Location'), sg.Button('Exit')
]
]
window = sg.Window('Window Title', layout)
sg.cprint_set_output_destination(window, output_key)
while True: # Event Loop
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == 'Print':
sg.cprint(values['-IN-'], text_color = values['-TEXT COLOR-'], background_color = values['-BG COLOR-'])
elif event == 'Print short':
sg.cprint(values['-IN-'], c = (values['-TEXT COLOR-'], values['-BG COLOR-']))
elif event.startswith('Use Input'):
sg.cprint(values['-IN-'], colors = values['-IN-'])
elif event.startswith('Toggle'):
output_key = MLINE_KEY
if output_key == MLINE_KEY2
else MLINE_KEY2
sg.cprint_set_output_destination(window, output_key)
sg.cprint('Switched to this output element', c = 'white on red')
elif event == 'Force 1':
sg.cprint(values['-IN-'], c = (values['-TEXT COLOR-'], values['-BG COLOR-']), key = MLINE_KEY)
elif event == 'Force 2':
sg.cprint(values['-IN-'], c = (values['-TEXT COLOR-'], values['-BG COLOR-']), key = MLINE_KEY2)
window.close()
def __init__(self, location = (None, None), font = ('Arial', 16)):
self.font = font
numberRow = '1234567890'
topRow = 'QWERTYUIOP'
midRow = 'ASDFGHJKL'
bottomRow = 'ZXCVBNM'
keyboard_layout = [
[sg.Button(c, key = c, size = (4, 2), font = self.font) for c in numberRow] + [
sg.Button('⌫', key = 'back', size = (4, 2), font = self.font),
sg.Button('Esc', key = 'close', size = (4, 2), font = self.font)
],
[sg.T(' ' * 4)] + [sg.Button(c, key = c, size = (4, 2), font = self.font) for c in
topRow
] + [sg.Stretch()],
[sg.T(' ' * 11)] + [sg.Button(c, key = c, size = (4, 2), font = self.font) for c in
midRow
] + [sg.Stretch()],
[sg.T(' ' * 18)] + [sg.Button(c, key = c, size = (4, 2), font = self.font) for c in
bottomRow
] + [sg.Stretch()]
]
self.window = sg.Window('keyboard',
grab_anywhere = True,
keep_on_top = True,
alpha_channel = 0,
no_titlebar = True,
element_padding = (0, 0),
location = location
).Layout(keyboard_layout).Finalize()
self.hide()
def show_win():
sg.SetOptions(border_width = 0, margins = (0, 0), element_padding = (5, 3))
frame_layout = [
[sg.Button('', image_data = mac_red, button_color = ('white', sg.COLOR_SYSTEM_DEFAULT), key = '_exit_'),
sg.Button('', image_data = mac_orange, button_color = ('white', sg.COLOR_SYSTEM_DEFAULT)),
sg.Button('', image_data = mac_green, button_color = ('white', sg.COLOR_SYSTEM_DEFAULT), key = '_minimize_'),
sg.Text(' ' * 40)
],
]
layout = [
[sg.Frame('', frame_layout)],
[sg.T('')],
[sg.Text(' My Mac-alike window', size = (25, 2))],
]
window = sg.Window('My new window',
no_titlebar = True,
grab_anywhere = True,
alpha_channel = 0,
).Layout(layout).Finalize()
for i in range(100):
window.SetAlpha(i / 100)
time.sleep(.01)
while True: # Event Loop
event, values = window.Read()
if event is None or event == '_exit_':
break
if event == '_minimize_':
# window.Minimize() # cannot minimize a window with no titlebar
pass
print(event, values)
def main():
layout = [
[sg.Text('A toggle button example')],
[sg.T('A graphical version'), sg.B('', image_data = toggle_btn_off, key = '_TOGGLE_GRAPHIC_', button_color = sg.COLOR_SYSTEM_DEFAULT, border_width = 0), ],
[sg.Button('On', size = (3, 1), button_color = ('white', 'green'), key = '_B_'), sg.Button('Exit')]
]
window = sg.Window('Toggle Button Example', layout)
down = True
graphic_off = True
while True: # Event Loop
event, values = window.Read()
print(event, values)
if event in (None, 'Exit'):
break
elif event == '_B_': #
if the normal button that changes color and text
down = not down
window.Element('_B_').Update(('Off', 'On')[down], button_color = (('white', ('red', 'green')[down])))
elif event == '_TOGGLE_GRAPHIC_': #
if the graphical button that changes images
graphic_off = not graphic_off
window.Element('_TOGGLE_GRAPHIC_').Update(image_data = (toggle_btn_on, toggle_btn_off)[graphic_off])
window.Close()
This is the code that displays the form, collects the information and returns the data collected. In this example we have a button return code and only 1 input field. The result of the form is stored directly into the variable we wish to work with.,The last line of the form_rows variable assignment contains a Submit and a Cancel Button. These are buttons that will cause a form to return its value to the caller.,The next few rows of code lay out the rows of elements in the window to be displayed. The variable form_rows holds our entire GUI window. The first row of this form has a Text element. These simply display text on the form.,In the statement that shows and reads the form, the two input fields are directly assigned to the caller's variables folder_path and file_path, ready to use. No parsing no callbacks.
Looking to take your Python code from the world of command lines and into the convenience of a GUI? Have a Raspberry Pi with a touchscreen that's going to waste because you don't have the time to learn a GUI SDK? Into Machine Learning and are sick of the command line? Look no further, you've found your GUI package.
import PySimpleGUI as sg
sg.Popup('Hello From PySimpleGUI!', 'This is the shortest GUI program ever!')
Or how about a custom GUI in 1 line of code?
import PySimpleGUI as sg
button, (filename, ) = sg.FlexForm('Get filename example').LayoutAndRead([
[sg.Text('Filename')],
[sg.Input(), sg.FileBrowse()],
[sg.OK(), sg.Cancel()]
])
In addition to a primary GUI, you can add a Progress Meter to your code with ONE LINE of code. Slide this into any of your for
loops and get a nice meter like this:
EasyProgressMeter('My meter title', current_value, max value)
Here is the code that produced the above screenshot.
import PySimpleGUI as sg
with sg.FlexForm('Everything bagel', auto_size_text = True, default_element_size = (40, 1)) as form:
layout = [
[sg.Text('All graphic widgets in one form!', size = (30, 1), font = ("Helvetica", 25), text_color = 'blue')],
[sg.Text('Here is some text.... and a place to enter text')],
[sg.InputText()],
[sg.Checkbox('My first checkbox!'), sg.Checkbox('My second checkbox!',
default = True)],
[sg.Radio('My first Radio! ', "RADIO1",
default = True), sg.Radio('My second Radio!', "RADIO1")],
[sg.Multiline(default_text = 'This is the default Text shoulsd you decide not to type anything',
scale = (2, 10))],
[sg.InputCombo(['Combobox 1', 'Combobox 2'], size = (20, 3)),
sg.Slider(range = (1, 100), orientation = 'h', size = (35, 20), default_value = 85)
],
[sg.Listbox(values = ['Listbox 1', 'Listbox 2', 'Listbox 3'], size = (30, 6)),
sg.Slider(range = (1, 100), orientation = 'v', size = (10, 20), default_value = 25),
sg.Slider(range = (1, 100), orientation = 'v', size = (10, 20), default_value = 75),
sg.Slider(range = (1, 100), orientation = 'v', size = (10, 20), default_value = 10)
],
[sg.Text('_' * 100, size = (70, 1))],
[sg.Text('Choose Source and Destination Folders', size = (35, 1))],
[sg.Text('Source Folder', size = (15, 1), auto_size_text = False, justification = 'right'), sg.InputText('Source'),
sg.FolderBrowse()
],
[sg.Text('Destination Folder', size = (15, 1), auto_size_text = False, justification = 'right'), sg.InputText('Dest'),
sg.FolderBrowse()
],
[sg.Submit(), sg.Cancel(), sg.SimpleButton('Customized', button_color = ('white', 'green'))]
]
button, values = form.LayoutAndRead(layout)
Installing
pip install PySimpleGUI
Please note this part is not only about creating checkboxes or radio buttons but also about making them work. Passing key argument to the items will be crucial in this objective as you will see in the examples.,Also, a couple points can seem slightly confusing with the lack of examples and demonstrations but don’t worry we created a tutorial that makes everything clear at first sight and you’ll be able to hit the ground running.,In this continuation tutorial let’s discover ways to create and read inputs from various very useful GUI items that were briefly mentioned in Part I. These are:,We have demonstrated checkboxes and radio buttons in PySimpleGUI which is a great Python GUI library that really packs a punch especially given its minimalist structure.
[sg.Checkbox('My Checkbox',
default = True)]
layout = [
[sg.Button('Hello World', size = (20, 4))],
[sg.Checkbox('Print On:',
default = True)]
]
import PySimpleGUI as sg layout = [ [sg.T("")], [sg.T(" "), sg.Button('Hello World', size = (20, 4))], [sg.T("")], [sg.T(" "), sg.Checkbox('Print On:', default = True)] ] # # #Setting Window window = sg.Window('Push my Buttons', layout, size = (300, 200)) # # #Showing the Application, also GUI functions can be placed here. while True: event, values = window.read() if event == sg.WIN_CLOSED or event == "Exit": break window.close()
layout = [
[sg.Button('Hello World', size = (20, 4))],
[sg.Checkbox('Print On:',
default = False, key = "-IN-")]
]
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == "Exit":
break
elif values["-IN-"] == True:
print("Hello World")
import PySimpleGUI as sg layout = [ [sg.T("")], [sg.T(" "), sg.Button('Hello World', size = (20, 4))], [sg.T("")], [sg.T(" "), sg.Checkbox('Print On:', default = True, key = "-IN-")] ] # # #Setting Window window = sg.Window('Push my Buttons', layout, size = (300, 200)) # # #Showing the Application, also GUI functions can be placed here. while True: event, values = window.read() if event == sg.WIN_CLOSED or event == "Exit": break elif values["-IN-"] == True: print("Hello World") window.close()