Tkinter Geometry Return to Normal

╄→гoц情女王★ 提交于 2020-01-02 23:37:08

问题


I have a tkinter window which I am able to make fullscreen, using geometry(width+height) and overrideredirect(True), but now when I return the window back to a normal size and execute the command overrideredirect(False), I cannot seem to get the window to automatically follow the size of the widgets inside it, as it would do had I not changed the size. Do you know any way which I could return the window to automatically following the size of the widgets again? Thank You in Advance!


回答1:


Call the geometry with a value of "" to get it to reset itself to its natural size.

Tkinter is based on tk, and the tk docs say this on the matter:

If newGeometry is specified as an empty string then any existing user-specified geometry for window is cancelled, and the window will revert to the size requested internally by its widgets.




回答2:


I believe you're looking for the winfo_reqwidth/reqheight() methods. These return the required width and height for all the widgets that are children of the widget they're called on. Just plug those into the geometry() method the same way you did to go fullscreen on your restore function, like this:

def fullscreen():
    root.overrideredirect(True)
    root.geometry('{0}x{1}+0+0'.format(root.winfo_screenwidth(), root.winfo_screenheight()))

def restore():
    root.overrideredirect(False)
    root.geometry('{0}x{1}'.format(root.winfo_reqwidth(), root.winfo_reqheight()))

root = Tk()

Button(root, text='Full Screen', command=fullscreen).pack()
Button(root, text='Restore', command=restore).pack()

root.mainloop()


来源:https://stackoverflow.com/questions/23853575/tkinter-geometry-return-to-normal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!