How do I respond to window resize in PySimpleGUI - python

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

Related

Mouse coordinates on Python with PySimpleGUI

I'm trying to get the mouse coordinates, with pynput, and see it with a GUI with PySimpleGUI but I am getting a lot of problens, and I don't know how can I do this.
I want to show the mouse coordinates at the "x - y" string below
import PySimpleGUI as sg
from pynput import mouse
sg.theme("DarkAmber")
layout = [[sg.Text("Mouse Coord")],
[sg.Text("x - y")]]
window = sg.Window("Mouse Coord 1.0", layout, keep_on_top = True)
while True:
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
break
window.close()
Not sure why you need the mouse coordinate, following code demo the way to get it by PySimpleGUI.
import PySimpleGUI as sg
sg.theme("DarkAmber")
layout = [[sg.Text("Mouse Coord:"), sg.Text(size=20, key='Coordinate')]]
window = sg.Window("Mouse Coord", layout, finalize=True)
window.bind('<Motion>', 'Motion')
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'Motion':
e = window.user_bind_event
window['Coordinate'].update(f'({e.x}, {e.y})')
window.close()

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

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

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

Using 'esc' to close PySimpleGUI window

I have a PySimpleGUI window that I want to maximise, eventually without a title bar. I want to use the 'esc' key to close the window.
Here's my (simplified) code:
import msvcrt
import PySimpleGUI as sg
layout = [[sg.Text(size=(40, 1), font=("Arial", (32)), justification='left', key='-TEXT-')]]
window = sg.Window(title="Window", layout=layout, grab_anywhere=True, finalize = True, no_titlebar=False)
window.maximize()
escape = False
while True:
event, values = window.read()
if msvcrt.kbhit() and msvcrt.getch() == chr(27).encode():
escape = True
else:
ecape = False
if event == sg.WIN_CLOSED or event == 'Cancel' or escape == True:
break
window.close()
The close button works fine - but pressing escape does nothing.
I've tried several of the answers here, but with no luck.
What's going wrong, and how can I fix it?
Bind event "<Escape>" to window to generate an event,
import PySimpleGUI as sg
layout = [[sg.Text(size=(40, 1), font=("Arial", (32)), justification='left', key='-TEXT-')]]
window = sg.Window(title="Window", layout=layout, grab_anywhere=True, finalize = True, no_titlebar=False)
window.maximize()
window.bind("<Escape>", "-ESCAPE-")
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, "-ESCAPE-"):
break
print(event, values)
window.close()
Solved.
As #knosmos pointed out, getch is only for the command line. Adding return_keyboard_events=True and event == 'Escape:27' did the trick.

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