Home > Enterprise >  How do I resize my Window twice with the same button?
How do I resize my Window twice with the same button?

Time:09-17

So if i try to resize my Window with the same button back and forth just the one on the bottom will work but it wont go back to '384x470' as if the Window wasnt '500x500'

def sizeWindow():
    if root.geometry('500x500'):
        root.geometry('384x470')

    else:
        root.geometry('500x500')
        buttonWinSize.configure(text="<<")```

CodePudding user response:

You seem to misunderstand several things here. Tkinter has relatively cool feature that allows you to use some methods for different things. Either you get or you set with the same method. To get you leave the interface empty (e.g. root.geometry()) or you set with the required argument (e.g. root.geometry('500x500 0 0')). You should be aware that you get the current value(s) in the same format as you set them.

Therefore you retrieve a geometry string by root.geometry() and this has the form of width x height x y.

However, not every time this feature is the right tool to achieve the overall goal. In your case you want to check for the width and height. Tkinter provides another feature for exactly this task. It lets you ask for the width and height via winfo_width() and winfo_height().

In addition you should know that every function in python needs to return something and if nothing is specified None is returned by this function. Since you use the setter variant of root.geometry('500x500') you receive None and None equals to False in python, therefore your else block will always be executed instead of your if block.

Suggested change:

def sizeWindow():
    if root.winfo_width() == 500 and root.winfo_height() == 500:
        root.geometry('384x470')
    else:
        root.geometry('500x500')

CodePudding user response:

Your if-statement is wrong. You are actually setting the size with your first root.geometry('500x500'). This does (AFAIK) not return a boolean. So you need to check the actual current dimensions:

if root.winfo_width() == 500 and root.winfo_height() == 500:
    root.geometry('384x470')
else:
    root.geometry('500x500')
    buttonWinSize.configure(text="<<")
  • Related