如何记住Python3中tkinter窗口的位置?
我有一个tkinter gui,我希望它在重新启动时保持原来的窗口位置和大小 以下是一个说明如何设置特定位置和尺寸的答案,但不是关于记住设置的单词:如何记住Python3中tkinter窗口的位置?,python,windows,python-3.x,tkinter,python-3.6,Python,Windows,Python 3.x,Tkinter,Python 3.6,我有一个tkinter gui,我希望它在重新启动时保持原来的窗口位置和大小 以下是一个说明如何设置特定位置和尺寸的答案,但不是关于记住设置的单词: 非常感谢您的帮助。在会话之间记住设置的唯一方法是将它们写入文件。因此,获取根窗口几何体(它是一个字符串)并将其写入文件。如果希望函数作为挂钩自动执行,请将其绑定到“”事件: def保存大小(事件): 以open(“myapp.conf”、“w”)作为conf: conf.write(root.geometry())#假设root是根窗口 root.
非常感谢您的帮助。在会话之间记住设置的唯一方法是将它们写入文件。因此,获取根窗口几何体(它是一个字符串)并将其写入文件。如果希望函数作为挂钩自动执行,请将其绑定到“”事件:
def保存大小(事件):
以open(“myapp.conf”、“w”)作为conf:
conf.write(root.geometry())#假设root是根窗口
root.bind(“,保存大小)
稍后,您可以从文件中读取几何体并将其还原。我花了相当多的时间才了解这一点的实际实现。所以我想分享我最后的代码。基于DyZ的建议 我没有按建议使用
事件,因为只有在退出前保存才足够
class Window(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# ...
# bla bla bla
# My GUI codes came here
# ...
# Try to load last saved window data
self.statusbar['text']="reading ini file..."
ini_file_path = "mapedit.ini"
try:
# if the file is there
# get geometry from file
ini_file = open(ini_file_path,'r')
self.geometry(ini_file.read())
self.statusbar['text']= "ini file read"
ini_file.close()
except:
# if the file is not there, create the file and use default
# then use default geometry.
self.statusbar['text']="ini file not found. New file created."
ini_file = open(ini_file_path, 'w')
ini_file.close()
self.geometry("640x400+100+200")
def client_exit(self):
self.save_geo()
self.destroy()
def save_geo(self):
# save current geometry to the ini file
try:
with open("mapedit.ini", 'w') as ini_file:
ini_file.write(self.geometry())
self.statusbar['text']="geo sv"
ini_file.close()
except:
statusbar['text']="ini file not found"
'''
This is where I created GUI instance
'''
if __name__ == "__main__":
win = Window()
# attach deletion handler to 'client_exit' method
win.protocol("WM_DELETE_WINDOW", win.client_exit)
win.mainloop()
听起来不错,但如何将其与on_resize事件挂钩?我是python新手。您从未问过调整大小事件,这就是为什么我没有回答。检查更新的答案。太好了,我已经把它添加到我的gui类中。这完美地回答了我的问题。但是,我想提出一个小建议:在退出应用程序时更新几何体。这是您唯一真正需要将其写入磁盘的时间。
class Window(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# ...
# bla bla bla
# My GUI codes came here
# ...
# Try to load last saved window data
self.statusbar['text']="reading ini file..."
ini_file_path = "mapedit.ini"
try:
# if the file is there
# get geometry from file
ini_file = open(ini_file_path,'r')
self.geometry(ini_file.read())
self.statusbar['text']= "ini file read"
ini_file.close()
except:
# if the file is not there, create the file and use default
# then use default geometry.
self.statusbar['text']="ini file not found. New file created."
ini_file = open(ini_file_path, 'w')
ini_file.close()
self.geometry("640x400+100+200")
def client_exit(self):
self.save_geo()
self.destroy()
def save_geo(self):
# save current geometry to the ini file
try:
with open("mapedit.ini", 'w') as ini_file:
ini_file.write(self.geometry())
self.statusbar['text']="geo sv"
ini_file.close()
except:
statusbar['text']="ini file not found"
'''
This is where I created GUI instance
'''
if __name__ == "__main__":
win = Window()
# attach deletion handler to 'client_exit' method
win.protocol("WM_DELETE_WINDOW", win.client_exit)
win.mainloop()
#Here I save the x and y position of the window to a file "myapp.conf"
#Here I see if the file exists.
if os.path.isfile("myapp.conf"):
#Here I read the X and Y positon of the window from when I last closed it.
with open("myapp.conf", "r") as conf:
root.geometry(conf.read())
else:
#Default window position.
root.geometry('610x270+0+0')
def on_close():
#custom close options, here's one example:
#close = messagebox.askokcancel("Close", "Would you like to close the program?")
#if close:
#Here I write the X Y position of the window to a file "myapp.conf"
with open("myapp.conf", "w") as conf:
conf.write(root.geometry())
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_close)