Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter - Maximizing and minimizing the window before using any widgets, hides the widgets when I use them

This is the code that I have written. As you can see, it has a button which when clicked opens up a Text where one can enter something. It works, the window resizes if you click the button.
But I have noticed that, if I maximize and minimize the window just after starting the program (i.e. the first thing I do is to maximize and minimize the window), then the window doesn't resize to fit the whole frame (on clicking the button) and my widgets get hidden.

from tkinter import *

def Home():
    global homeP
    homeP = Tk()
    homeP.title('Grades')

    enterButton = Button(homeP, text='Enter Grades', bg='blue', fg='white', command=enterG)
    enterButton.grid(row=1, column=0, padx=(5,5), pady=(5,2), sticky="e")

    homeP.mainloop()

curFrame = ''

def enterG():
    global homeP
    global eGrade
    global curFrame

    if curFrame == 'e':
        return
    if 'eGrade' not in globals(): #Prevent frames from stacking up on one another
        eGrade = Frame(homeP)
        enterGrades = Text(eGrade, width=64, height=10)
        enterGrades.grid(row=0, column=0)

        eGrade.grid(row=2, column=0, columnspan=2, padx=(10,10), pady=(1,5))
    else:
        eGrade.grid(row=2, column=0, columnspan=2, padx=(10,10), pady=(1,5))
    curFrame = 'e'

    # for name, value in globals().copy().items():
    #   print(name, value)
Home()

Any help as to why this is happening and how I can prevent this?

like image 596
Miraj50 Avatar asked Nov 08 '22 00:11

Miraj50


1 Answers

You will have exactly the same problem if you manually resize the window before clicking the button. The issue isn't tied to maximize/minimize per se, it's tied to the fact that the user has said "I want the window to be this size" and tkinter is trying to honor that by not resizing the window against the user's wishes.

The solution is to reset the size of the window before adding or removing widgets. Add the following line of code after your global statements in enterG:

homeP.geometry("")
like image 93
Bryan Oakley Avatar answered Nov 15 '22 11:11

Bryan Oakley