How to center a tkinter window and retain the “fit to children” behavior?

后端 未结 1 1695
闹比i
闹比i 2021-01-14 08:33

I have a window whose content changes. Sometimes the content is larger than the window, so the window expands to fit it\'s children. However, when I center a window using

相关标签:
1条回答
  • 2021-01-14 09:07

    When you call the geometry command, don't provide a width and height -- just supply the x/y values. When you give it an explicit width and height, you're telling Tk "I want the window to be exactly this size" so it turns it's auto-resize behavior off.

    root.geometry("+%d+%d" % (rootsize + (x, y)))
    

    Also, you can use winfo_width() and winfo_height() to get the actual size of the window, instead of parsing the output of the geometry method.

    0 讨论(0)
提交回复
热议问题