israel-dryer / PyDataMath-II

A simple Python calculator gui application based loosely on the Texas Instruments DataMath II produced circa 1975 #PySimpleGUI #Tkinter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Setting window size

PySimpleGUI opened this issue · comments

I've noticed a number of your programs specify a fixed size for the window. I recommend not doing this for a couple of reasons.

0 - This isn't tkinter, you don't normally set the size. There are rare exceptions.
1 - Your window should naturally end up being the correct size to fit your elements. If not, something's not sized correctly within
2 - Your code is non-portable to other PySimpleGUI ports. By setting a size, you're guaranteed to almost for sure not have a well-functioning program should you change your import to PySimpleGUIQt for example. Again, allowing the elements inside to determine the window size is preferred as it'll run on Qt with minimal changes beyond the import statement.
3 - Your code is non-portable to other platforms - the Linux tkinter and the Windows tkinter will likely differ in their widget sizes
4 - Your code is non-portable to online platforms. I just tried your this calculator on Trinket and the size parameter caused a large blank area at the bottom for some reason. Removing the size and it works great.

On Trinket - no window size parameter:

image

On Trinket - your original code with size set

image

I don't disagree about portability. I probably need to learn how to control the overall layout better. The trouble is I'm working with elements that have different fonts and font sizes, so there is not a uniform system of measurement... as you can see from the images above. Setting the window size is a way to force a hard boundary. If the elements were measured in pixels or something, then it would be a different story.

Oh! Wait wait wait

I think I have a solution for some of these problems.

Like let's say the calculator. Rather than trying to compute the exact size of a text element that will SPAN the entire top, instead make a text element that's big enough for the information you want to display and then RIGHT JUSTIFY that element within a Column.

That's one thing I've not seen yet from you. It's a new capability that's only in the PySimpleGUI tkinter port, but I'm certainly going to cross port it.

With the newest code you can right, left, center justify all elements within a column. Previously it was EXTREMELY difficult to center elements down the middle of a window.

I'm thinking part of your problem may be that you have elements that need to be right justified but don't need to go all the way across the window.

Would that capability help you?

Then again, what's REALLY needed is the "expand" option that I had in the code that I removed. That would solve the problem for these expanding elements.

I tried the other day to make some direct .Widget access code that would enable turning that stuff on and off, but I got bogged down in needing to make changes to the Element, then the "Row Frame" it was contained inside of, then the ? that the row frame is inside of. Unless each of them expand, then eventually the expansion will stop when it hits one of the containers.

THIS time it worked!!!!

I know there's still a finalize problem, so what I did instead was a read(timeout=0) right after creating the window.

So this is the layout, with a shortened display field.

layout = [
    [sg.Text('PyDataMath-II', size=(50, 1), justification='right', background_color="#272533",
             text_color='white', font=('Franklin Gothic Book', 14, 'bold'))],
    [sg.Text('0.0000', size=(10, 1), justification='right', background_color='black', text_color='red',
             font=('Digital-7', 38), relief='sunken', key="_DISPLAY_")],
    [sg.Button('C', **bt), sg.Button('CE', **bt), sg.Button('%', **bt), sg.Button("/", **bt)],
    [sg.Button('7', **bw), sg.Button('8', **bw), sg.Button('9', **bw), sg.Button("*", **bt)],
    [sg.Button('4', **bw), sg.Button('5', **bw), sg.Button('6', **bw), sg.Button("-", **bt)],
    [sg.Button('1', **bw), sg.Button('2', **bw), sg.Button('3', **bw), sg.Button("+", **bt)],
    [sg.Button('0', **bw), sg.Button('.', **bw), sg.Button('=', **bo, bind_return_key=True)],
]
window = sg.Window('PyDataMath-II', layout=layout, background_color="#272533",  return_keyboard_events=True)

And it makes this window
image

And here comes the MAGIC!!!! (F*ck I love PySimpleGUI)

Right after creating the window, add these lines:

window.Read(timeout=0)
window['_DISPLAY_'].Widget.pack(expand=True, fill='both')
window['_DISPLAY_'].ParentRowFrame.pack(expand=True, fill='both')

Now when I run it I see:
image

I don't know why I wasn't able to get it to work the other day. Sometimes shit has to cook for a few days.

I'm pretty sure the ParentRowFrame member variable code has been released. It's at least on GitHub.

Try it and let me know!

That will definitely come in handy. I'll let you know how it goes. Thanks for the tips!

I should probably test some of this stuff on other platforms just to see what it looks like. I've got an RPi, so I could see how it runs on Linux.