Switch window in PySimpleGUI with keyboard input (left/right arrow) - python

I have 3 windows in terms of sg.Frame and would like to switch the windows via keyboard input. However the window does not respones while the buttons work well.
layout = [
[
sg.Column(layout1, key='-COL1-'), # layoutX is a page interms of sg.Frame
sg.Column(layout2, visible=False, key='-COL2-'),
sg.Column(layout3, visible=False, key='-COL3-')
],
[
# sg.Button('Cycle Layout'),
sg.Button('-PREV-'),
sg.Button('1'),
sg.Button('2'),
sg.Button('3'),
sg.Button('-NEXT-'),
# sg.Button('Exit')
]
]
window = sg.Window('Swapping the contents of a window', layout) #, size = (1024, 800)
layout = 1 # The currently visible layout
while True:
event, values = window.read()
print(event, values)
if event in (None, 'Exit'):
break
window.bind('<Right>', '-NEXT-')
window.bind('<Left>', '-PREV-')
window.bind('<Down>', 'Exit')
if event == '-NEXT-' and layout < 3:
window[f'-COL{layout}-'].update(visible=False)
layout = layout + 1
window[f'-COL{layout}-'].update(visible=True)
elif event == '-PREV-' and layout > 1:
window[f'-COL{layout}-'].update(visible=False)
layout -= 1
window[f'-COL{layout}-'].update(visible=True)
elif event in '123':
window[f'-COL{layout}-'].update(visible=False)
layout = int(event)
window[f'-COL{layout}-'].update(visible=True)
window.close()
How can I modify? Thanks very much!

The statements for binding the keyboards should be placed after window finalized and before your event loop, then it will work before your event loop start.
window = sg.Window('Swapping the contents of a window', layout, finalize=True) #, size = (1024, 800)
window.bind('<Right>', '-NEXT-')
window.bind('<Left>', '-PREV-')
window.bind('<Down>', 'Exit')

Related

How to control Progress bar size PySimpleGUI

I want to create a progress bar using PySimpleGUI but I want the user to put the maximum of the progress bar.
this is my code:
import PySimpleGUI as sg
import random, time
sg.theme("LightBlue")
progress_value=input()
layout = [[sg.Text("Enter a number out of 50", font='Lucida'),
sg.InputText(key='-PROGRESS_VALUE-', font='Lucida, 20', size=(20, 40))],
[sg.ProgressBar(progress_value, orientation='h', size=(100, 20), border_width=4, key='-PROGRESS_BAR-',
bar_color=("Blue", "Yellow"))],
[sg.Button('Change Progress'), sg.Exit(),sg.Button('Stop Progress')]]
window = sg.Window("Progress Bar", layout)
while True:
event, values = window.read()
if event == 'Exit' or event == sg.WIN_CLOSED:
break
progress_value = int(values['-PROGRESS_VALUE-'])
if event == "Change Progress":
for i in range(progress_value):
event, values = window.read(1000)
if event == "Stop Progress":
window['-PROGRESS_BAR-'].update(i-1)
break
window['-PROGRESS_BAR-'].update(max=progress_value)
window['-PROGRESS_BAR-'].update(i+1)
window.close()
as you can see the maximum which is "progress_value" is given by an input (progress_value=input())
but i want it to come from the input text of the user (sg.InputText(key='-PROGRESS_VALUE-', font='Lucida, 20', size=(20, 40))) and that value will be set to progress_value
Here's one way of doing what you're after using a single event loop
When changing the max value of a ProgressBar, you must set a current value too (in the same update call).
import PySimpleGUI as sg
sg.theme("LightBlue")
progress_value = 50
layout = [[sg.Text("Enter a number out of 50", font='Lucida'),
sg.InputText(key='-PROGRESS_VALUE-', font='Lucida, 20', size=(20, 40))],
[sg.ProgressBar(progress_value, orientation='h', size=(100, 20), border_width=4, key='-PROGRESS_BAR-',
bar_color=("Blue", "Yellow"))],
[sg.Button('Change Progress'), sg.Button('Start Progress'), sg.Button('Stop Progress')]]
window = sg.Window("Progress Bar", layout)
progress_started, counter, timeout = False, 0, None
while True:
event, values = window.read(timeout=timeout)
if event == 'Exit' or event == sg.WIN_CLOSED:
break
if event == "Change Progress":
progress_value = int(values['-PROGRESS_VALUE-'])
# NOTE - must set a current count when changing the max value
window['-PROGRESS_BAR-'].update(current_count= 0, max=progress_value)
elif event == 'Start Progress':
progress_started = True
counter = 0
timeout = 1000
elif event == 'Stop Progress':
progress_started = False
timeout = None
if progress_started:
window['-PROGRESS_BAR-'].update(counter)
counter += 1
if counter > progress_value:
progress_started = False
window.close()

Hotkeys in PySimpleGUI

I'd like to write a GUI with PySimpleGUI that can be used entirely by keyboard. Based on the following sample code:
import PySimpleGUI as sg
layout = [[sg.Text("Hello from PySimpleGUI")], [sg.Button(button_text="OK")]]
window = sg.Window("Demo", layout)
while True:
event, values = window.read()
if event == "OK" or event == sg.WIN_CLOSED:
break
window.close()
How can I add a hotkey which I can press using Alt+O to press the OK-Button? The O on the OK-Button should be underlined:
A minimalist working example derived from: https://github.com/PySimpleGUI/PySimpleGUI/issues/4122
import PySimpleGUI as sg
layout = [
[sg.Button("ok", size=(10, 2), key='button1'),
sg.Button("exit", size=(10, 2), key='button2')],
]
window = sg.Window('Hotkeys', layout, use_default_focus=False, finalize=True)
button1, button2 = window['button1'], window['button2']
window.bind("<Alt_L><o>", "ALT-o")
window.bind("<Alt_L><x>", "ALT-x")
button1.Widget.configure(underline=0, takefocus=0)
button2.Widget.configure(underline=1, takefocus=0)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event in ("button1", "ALT-o"):
print('OK')
elif event in ("button2", "ALT-x"):
break
window.close()

PySimpleGUI changing visibility breaks columns(?)

I have 2 columns and I want to reveal them on a button press by using the visibility parameter. However, it seems that columns that go from invisible to visible stop being next to each other and are instead arranged like rows.
Here is the code without the reveal, and the columns work fine:
import PySimpleGUI as sg
left_col = sg.Column([[sg.Frame('',[],background_color = '#FF0000',size = (60,40))]])
right_col = sg.Column([[sg.Frame('',[],background_color = '#00FF00',size = (60,40))]])
layout = [
[sg.Button('reveal')],
[left_col,right_col]]
window = sg.Window('Converter', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
window.close()
And here is the same code with the columns being revealed:
import PySimpleGUI as sg
left_col = sg.Column([[sg.Frame('',[],background_color = '#FF0000',size = (60,40))]],visible = False, key = 'left')
right_col = sg.Column([[sg.Frame('',[],background_color = '#00FF00',size = (60,40))]],visible = False, key = 'right')
layout = [
[sg.Button('reveal')],
[left_col,right_col]]
window = sg.Window('Converter', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'reveal':
window['left'].update(visible = True)
window['right'].update(visible = True)
window.close()
I guess my question is whether there is a workaround for this (or whether I did something wrong).
Elements after visible=False will miss it's location in the window, so use function pin to keep the location for element if you want to set it to be invisible.
layout = [
[sg.Button('reveal')],
[sg.pin(left_col), sg.pin(right_col)]]
By default, the background is the background color of theme. Of course, you can built one by yourself which with one more option bg as background_color.
Don't forget to set the background color of the Column in your layout at the same time.
def pin(elem, vertical_alignment=None, shrink=True, expand_x=None, expand_y=None, bg=None):
if shrink:
return sg.Column([[elem, sg.Column([[]], pad=(0,0), background_color=bg)]], background_color=bg, pad=(0, 0), vertical_alignment=vertical_alignment, expand_x=expand_x, expand_y=expand_y)
else:
return sg.Column([[elem]], pad=(0, 0), vertical_alignment=vertical_alignment, expand_x=expand_x, expand_y=expand_y, background_color=bg)
Then you code maybe something like this
import PySimpleGUI as sg
def pin(elem, vertical_alignment=None, shrink=True, expand_x=None, expand_y=None, bg=None):
if shrink:
return sg.Column([[elem, sg.Column([[]], pad=(0,0), background_color=bg)]], background_color=bg, pad=(0, 0), vertical_alignment=vertical_alignment, expand_x=expand_x, expand_y=expand_y)
else:
return sg.Column([[elem]], pad=(0, 0), vertical_alignment=vertical_alignment, expand_x=expand_x, expand_y=expand_y, background_color=bg)
left_col = sg.Column([[sg.Frame('', [], background_color = '#FF0000', size = (60,40))]], background_color='blue')
right_col = sg.Column([[sg.Frame('', [], background_color = '#00FF00', size = (60,40))]], background_color='blue')
layout = [
[sg.Button('reveal')],
[pin(left_col, bg='blue'), pin(right_col, bg='blue')]]
window = sg.Window('Converter', layout, background_color='blue')
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
window.close()

How do I respond to window resize in PySimpleGUI

How can I get notified when the window is resized in PySimpleGUI?
I have a window that enables resize events, but I'm not finding a way to move the elements around when that resize occurs, so my window renames top left centered the same size when the window changes size.
Here is the basic code:
import PySimpleGUI as sg
layout = [[sg.Button('Save')]]
window = sg.Window('Window Title',
layout,
default_element_size=(12, 1),
resizable=True) # this is the change
while True:
event, values = window.read()
if event == 'Save':
print('clicked save')
if event == sg.WIN_MAXIMIZED: # I just made this up, and it does not work. :)
window.maximize()
if event == sg.WIN_CLOSED:
break
Adding tkinter events to windows results in callback on change of windows size
import PySimpleGUI as sg
layout = [[sg.Button('Save')]]
window = sg.Window('Window Title',
layout,
default_element_size=(12, 1),
resizable=True,finalize=True) # this is the chang
window.bind('<Configure>',"Event")
while True:
event, values = window.read()
if event == 'Save':
print('clicked save')
if event == "Event":
print(window.size)
if event == sg.WIN_CLOSED:
print("I am done")
break
You need to bind "<Configure>" event to check zoomed event.
import PySimpleGUI as sg
layout = [[sg.Text('Window normal', size=(30, 1), key='Status')]]
window = sg.Window('Title', layout, resizable=True, finalize=True)
window.bind('<Configure>', "Configure")
status = window['Status']
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == 'Configure':
if window.TKroot.state() == 'zoomed':
status.update(value='Window zoomed and maximized !')
else:
status.update(value='Window normal')
window.close()

How to resize sg.window in PYsimpleGUI?

I am using PYsimpleGUI in my python code, and while using the window element to create the main window, this is my code.
My code:
import PySimpleGUI as sg
layout = [ [sg.Button('Close')] ]
window = sg.Window('This is a long heading.', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Close':
break
break
window.close()
I notice that when I run this program, the full heading is not shown as the window resizes itself and becomes small.
Is there a way to resize sg.window?
You can add size argument in the sg.Window.
Try this :
import PySimpleGUI as sg
layout = [ [sg.Button('Close')] ]
window = sg.Window('This is a long heading.', layout,size=(290, 50))
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Close':
break
break
window.close()

Categories