5

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?

Miraj50
  • 4,257
  • 1
  • 21
  • 34
  • I had the same problem but solved by the different way. Can I show you that ? – Ghantey Nov 06 '18 at 14:35
  • when I had the same situation I just did this **text = scrollbar.ScrolledText(master=root, wrap = WORD, width=1366, height=768).pack()** I have changed width and height to the maximum which solved my problem and here is the complete code https://pastebin.com/9zW7hPAu – Ghantey Nov 06 '18 at 14:47
  • @markkeven Okay, I guess it is a hack. It will make my code dependent on a system. Isn't it? I wanted to avoid that as there are systems with less than a 15.6' screen. – Miraj50 Nov 06 '18 at 14:51
  • oh yeah that didn't come in my head – Ghantey Nov 06 '18 at 14:54

1 Answers1

1

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("")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685