2

I am trying to set the minimum size of the window.

I followed the solution posted on How do I set a minimum window size in tkinter?

However, that doesn't take account of the height of the Menu I have.

The Menu code I have is:

menubar = tk.Menu(self)

menu = tk.Menu(menubar, tearoff=0)
menu.add_command(label="Change user name", \
                 command=self.prompt_new_name)
menu.add_separator()
menu.add_command(label="Exit", command=self.close_app)
menubar.add_cascade(label="Menu", menu=menu)
self.master.config(menu=menubar)

And the way how I am setting the minimum size is:

master = self.master
master.update()
print(master.winfo_height())
print(menubar.winfo_height())
master.minsize(master.winfo_width(), master.winfo_height())

master.winfo_height() looks like it returns the height of the window as if there was no Menu widget. I tried printing the height of menubar but it returns 1.

How do I get the height of the window including the menu or only get the height of the menu separately so I can add it to the master.winfo_height() and set that as the minsize?

Community
  • 1
  • 1
whiteSkar
  • 1,614
  • 2
  • 17
  • 30
  • Why do you need the window to be as tall as the menu? That seems like a very unusual request. – Bryan Oakley Nov 11 '15 at 21:45
  • Not as tall as menu. I need it to be no smaller than the initial window size including the menu and all other widgets. Right now, because master.winfo_height() does not include the height of the menu, I can still make the master window smaller and the widget at the very bottom gets cut off. – whiteSkar Nov 11 '15 at 21:51
  • As long as you use `grid` and/or `pack`, the window will always be big enough to contain everything. There should be no reason to set a minimum size. Just let tkinter figure out the size for you. I think you're attempting to solve a non-problem. – Bryan Oakley Nov 11 '15 at 22:07
  • It is an actual problem for me. I can click the bottom border of the window and move my mouse up to make the window smaller. Then the widgets at the bottom gets cut off. – whiteSkar Nov 11 '15 at 22:15
  • I'm having the same problem, the window may be initially large enough to contain everything, but it can be resized smaller, cutting off widgets (yes, I recognize that normally the user is allowed to shoot themselves in the foot, but I'm not the one calling the shots). If I set (after a forced `update`/`update_idletasks`) the minsize using `master.minsize(master.winfo_reqwidth(), master.winfo_reqheight() + menu.winfo_reqheight())` (where the menu reports a reqheight of 60) it sort of works, but it pads, half of which can't be shrunk away. – ShadowRanger Feb 22 '17 at 19:41

1 Answers1

1

Please read the documentation of winfo_height() and winfo_width(). As it states out there:

w.winfo_height()

[...] See the remarks on geometry updating under .winfo_geometry(), above. You may prefer to use .winfo_reqheight(), described below, which is always up to date.

w.winfo_reqheight()

These methods return the requested height of widget w. This is the minimum height necessary so that all of w's contents have the room they need. The actual height may be different due to negotiations with the geometry manager.

Also have a look at this question.

Community
  • 1
  • 1
R4PH43L
  • 2,122
  • 3
  • 18
  • 30