首页 > 解决方案 > 让 tkinter GUI 在意外关闭时打开以前关闭的数据

问题描述

我正在使用 tkinter 构建一个 GUI,以便在树视图上显示数据,并且用户可以在其中编辑、删除、添加和保存树视图数据。我将它放在树视图提取昨天数据的位置(使用 timedelta),但是如果用户或某些东西意外导致应用程序关闭。我如何让应用程序打开他们正在使用的数据而不是昨天的数据?

我的树视图

def tree(self):
    self.tree1 = ttk.Treeview(self)

    self.tree1['columns'] = ("Testing Station", "Break_Lunch", "Teammate", "Start Date", "Agency", "Hours Work", "Notes")

    self.tree1.column('#0', width=0, stretch=NO)
    self.tree1.column('Testing Station', anchor=CENTER, width=80)
    self.tree1.column('Break_Lunch', anchor=CENTER, width=80)
    self.tree1.column('Teammate', anchor=CENTER, width=120)
    self.tree1.column('Start Date', anchor=CENTER, width=80)
    self.tree1.column('Agency', anchor=CENTER, width=120)
    self.tree1.column('Hours Work', anchor=CENTER, width=80)
    self.tree1.column('Notes', anchor=CENTER, width=120)

    self.tree1.heading('#0', text='', anchor=CENTER)
    self.tree1.heading('Testing Station', text='Station #', anchor=CENTER)
    self.tree1.heading('Break_Lunch', text='Break--Lunch', anchor=CENTER)
    self.tree1.heading('Teammate', text='Teammates', anchor=CENTER)
    self.tree1.heading('Start Date', text='Start Date', anchor=CENTER)
    self.tree1.heading('Agency', text='Agency', anchor=CENTER)
    self.tree1.heading('Hours Work', text='Hours Work', anchor=CENTER)
    self.tree1.heading('Notes', text='Notes', anchor=CENTER)

    self.tree1.grid(row=4, column=1, columnspan=7, pady=10, padx=10)

    os.chdir('Csv Files')
    self.path2 = os.getcwd()

    with open("Testing A-Side " + self.yesterday.strftime("%m" + '.' + "%d" + '.' + "%Y") + ".csv", newline='')as f:
        reader = csv.DictReader(f)
        '''
        with open('A-side.csv', newline='')as f:
            reader = csv.DictReader(f)
        '''
        for col in reader:
            station = col['Testing Station']
            b_l = col['Break_Lunch']
            name = col['Teammate']
            start = col['Start Date']
            agency = col['Agency']
            hw = col['Hours Work']
            note = col['Notes']
            self.tree1.insert('', 0, values=(station, b_l, name, start, agency, hw, note))


    self.parent2 = os.path.dirname(self.path2)
    os.chdir(self.parent2)

我的保存功能

def saveSheet(self):
    os.chdir('Teammate Sheet')
    self.path = os.getcwd()

    self.excolumns = ["Testing Station", "Break_Lunch", "Teammates", "Start Date", "Agency", "Hours Work", "Notes"]#, "Production Total"]
    self.info = [self.tree1.item (item) ['values'] for item in self.tree1.get_children()]
    self.df = pd.DataFrame(self.info, columns=self.excolumns)
    self.df.to_excel("Testing A-Side " + self.today.strftime("%m" + '.' + "%d" + '.' + "%Y") + ".xlsx", engine='xlsxwriter', index=False)

    self.parent = os.path.dirname(self.path)
    os.chdir(self.parent)

标签: pythonuser-interfacetkinter

解决方案


我不太了解,tkinter.ttk.Treeview但您可以将程序的状态保存到文件中,并在用户再次运行程序时恢复它。基本上你需要创建一个自动保存功能。

这是一个适用于tkinter.Entry

import tkinter as tk
import os.path

def autosave(event):
    # Save the user's input in the autosave file
    with open("autosave.data", "w") as file:
        file.write(entry.get())

root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
# Bind to the user pressing any/all keys
entry.bind("<KeyPress>", autosave)

# If the file exists read it and put the data in the entry
if os.path.isfile("autosave.data"):
    with open("autosave.data", "r") as file:
        entry.insert(0, file.read())

root.mainloop()

如果没有看到你的大部分代码,我无法给你一个正确的答案,但想法仍然是一样的。将 GUI 的状态(在示例中是条目中的数据)保存到文件中。每当程序运行时,将数据恢复到 GUI


推荐阅读