Pysimplegui: [ Enhancement / Question] generate event when window is resized

Created on 9 Sep 2019  路  8Comments  路  Source: PySimpleGUI/PySimpleGUI

Type of Issues (Enhancement, Error, Bug, Question)

Question

Operating System

Mac OS 10.14.6

Python version

3.7.1

PySimpleGUI Port and Version

PySimpleGUI 4.3.2

Your Experience Levels In Months or Years

6 months Python programming experience
30 years Programming experience overall
yes Have used another Python GUI Framework (tkiner, Qt, etc) previously (yes/no is fine)?

You have completed these steps:

[ X ] Read instructions on how to file an Issue
[ X ] Searched through main docs http://www.PySimpleGUI.org for your problem
[ X ] Searched through the readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
[ X ] Looked for Demo Programs that are similar to your goal http://www.PySimpleGUI.com
[ X ] Note that there are also Demo Programs under each port on GitHub
[ X ] Run your program outside of your debugger (from a command line)
[ X ] Searched through Issues (open and closed) to see if already reported

My question is:

I start digging into matplotlib. I'd like to adapt to changing window dimensions. So when the user resizes the window, I want to generate an event, query the new window size and do something meaningful to the matplotlib-canvas inside.

Did I miss this in the documentation, or is it not possible yet?

enhancement

All 8 comments

No possible yet.

Great, so I don't need to spend time on this :-) Sometimes less flexibility is very helpful for prioritization.

Hello @haukehess, I may have a workaround for you. It's not perfect but it serves its purpose:

import PySimpleGUI as sg

win_w, win_h = 400, 200

layout = [
    [sg.Text("", key="_TEXT_01_", font=(None, 18), size=(20, 2))], 
    [sg.Text("", key="_TEXT_02_", font=(None, 12), size=(20, 2))]
]

window = sg.Window("window resize test", layout, size=(win_w, win_h), resizable=True, finalize=True)

def check_win_size_changed():
    if win_w != win_w_new:
        window["_TEXT_02_"]("width changed!", text_color="#00ff00")
    if win_h != win_h_new:
        window["_TEXT_02_"]("height changed!", text_color="#ff0000")
    if win_w != win_w_new and win_h != win_h_new:
        window["_TEXT_02_"]("width and height changed!", text_color="#0000ff")

while True:
    event, values = window(timeout=100)

    if event is None or event == 'Exit':
        break

    win_w_new, win_h_new = window.Size
    check_win_size_changed()
    win_w, win_h = win_w_new, win_h_new

    window["_TEXT_01_"]("height = "+str(win_h_new)+"\n"+"width = "+str(win_w_new))

PLEASE do not post code here with timeout=0.

Sorry. Changed it to timeout=100.

Thank you :-)

I'm beginning a more serious campaign to make sure none of the examples in the readme, cookbook and demo programs have low timeout values unless absolutely necessarily. I need to urge people to seriously think about how often they really need to service the window.

In this case, a value of 100ms means you're checking the window 10 times a second. That's a fairly quick rate. Of course, if it's not fast enough, it can be changed. Going from timeout=0 to timeout=100 has an enormous impact on your CPUs load. HUGE difference. We're talking going from 100% down to 5%.

I just noticed the font=(None, 18). Now that seems smartish, but I'm not sure it'll work everywhere as I do assume it's a string. A null string might work in places it crashes though. I like this better than my 'ANY' filler that I use.

See, my claims of "learning something from every single PySimpleGUI program I see" is absolutely == True. I learn, steal, recycle, repost and the result has been PySimpleGUI.

OK, I tested font=(None, 20) for Text elements. It works on the tkinter and Qt ports, but crashes the WxPython and Web ports.

font=('',20) works on all 4 ports. I would recommend sticking with having a string in the first position as that's what's generally expected. Unfortunately in Python, None is quite special in that doing any operation on it will generate an exception.

I'll start using '' for fonts that I don't care about setting.

For font strings, I still have to put something in that first position.
I tried using something like
font = '_ 20' could be a good alternative to 'ANY 20' but it produced a smaller font for some reason. It was like _ is a valid font name. If I use '- 20' then I get an error. I think I'll stick with ANY 20 for font strings.

Was this page helpful?
0 / 5 - 0 ratings