Python tkinter GUI在意外关闭时打开以前关闭的数据
我正在用tkinter构建一个GUI,以便在treeview上显示数据,用户可以在其中编辑、删除、添加和保存treeview数据。我知道treeview昨天收集数据的地方 (使用timedelta),但如果用户或其他原因意外导致应用程序关闭。我如何让应用程序使用他们正在使用的数据而不是昨天的数据打开 我的树景Python tkinter GUI在意外关闭时打开以前关闭的数据,python,user-interface,tkinter,Python,User Interface,Tkinter,我正在用tkinter构建一个GUI,以便在treeview上显示数据,用户可以在其中编辑、删除、添加和保存treeview数据。我知道treeview昨天收集数据的地方 (使用timedelta),但如果用户或其他原因意外导致应用程序关闭。我如何让应用程序使用他们正在使用的数据而不是昨天的数据打开 我的树景 def tree(self): self.tree1 = ttk.Treeview(self) self.tree1['columns'] = ("Testin
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)
以某种方式将数据保存到文件中。
json
包非常适合这一点。创建一些描述所有内部数据的数据结构,每当发生变化时将其转储到.json文件中,并添加一个按钮或其他东西,允许用户从该.json文件加载数据。我对tkinter.ttk.Treeview不太了解,但您可以将程序的状态保存到文件中,并在用户再次运行程序时将其还原。基本上,您需要创建一个自动保存功能
这是一个使用tkinter.Entry
将tkinter作为tk导入
导入操作系统路径
def自动保存(事件):
#在自动保存文件中保存用户的输入
打开(“autosave.data”、“w”)作为文件:
file.write(entry.get())
root=tk.tk()
entry=tk.entry(根)
entry.pack()
#按任意/所有键绑定到用户
entry.bind(“,自动保存)
#如果文件存在,则读取该文件并将数据放入条目中
如果os.path.isfile(“autosave.data”):
打开(“autosave.data”、“r”)作为文件:
entry.insert(0,file.read())
root.mainloop()
如果你没有看到你的大部分代码,我无法给你一个正确的答案,但想法仍然是一样的。将GUI的状态(在本例中是条目中的数据)保存到文件中。每当程序运行时,将数据还原回GUI中保存用户在GUI中输入的信息,并在用户再次运行程序时还原。您针对这个问题发布的代码太多了。请创建一个。如果问题是关于保存数据的,你不需要一个或两个以上的小部件来说明这个问题。现在到了。