繁体   English   中英

如何记住python 3中的tkinter窗口位置?

[英]How to remember tkinter window position in python 3?

我有一个 tkinter gui,我希望它在重新启动时保留原来的窗口位置和大小。

这是一个说明如何设置特定位置和尺寸的答案,但没有说明如何记住设置: 如何指定 Tkinter 窗口打开的位置?

非常感谢任何帮助。

从会话到会话记住设置的唯一方法是将它们写入文件。 因此,获取根窗口几何图形(它是一个字符串)并将其写入文件。 如果希望函数作为钩子自动执行,请将其绑定到“<Configure>”事件:

def save_size(event):
  with open("myapp.conf", "w") as conf:
    conf.write(root.geometry()) # Assuming root is the root window

root.bind("<Configure>",save_size)

您可以稍后从文件中读取几何图形并恢复它。

#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)

我花了很多时间来理解这个的实际实现。 所以我想分享我的最终代码。 基于 DyZ 建议。

我没有按照建议使用<Configure>事件,因为只有在退出前保存对我来说就足够了。

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM