我正在尝试为我的账单建立一个调度程序。
单击“新建”或双击树元素,将弹出一个新窗口以编辑或添加新元素。
我的问题在于:如何告诉我的代码添加新内容或更改旧内容?
这是我的代码:
from Tkinter import *
import ttk
import sys
import os.path
tree_nr_columns = 5
tree_columns = ("date","payee","dex","every","amount")
tree_width = (100, 200, 250, 50, 100)
tree_anchor = ("c","w","w","c","e")
tree_txt = []
for i in range(0, tree_nr_columns):
tree_txt.append(" ")
file_name = "/media/tony/Vault/scadenziario.txt"
class MainWindow:
def __init__(self, master):
frame = Frame(master)
frame.pack(side = TOP, fill = BOTH, expand = 1)
frame2 = Frame(master)
frame2.pack(side = LEFT)
self.tree = ttk.Treeview(frame)
self.tree["columns"] = tree_columns
self.tree['show'] = 'headings' # remove the first empty column
for i in range(0, tree_nr_columns):
self.tree.column(tree_columns[i], width = tree_width[i], anchor = tree_anchor[i])
self.tree.heading(tree_columns[i], text=tree_columns[i])
self.tree.pack(fill = BOTH, expand = True)
self.tree.bind("<Double-1>", self.OnDoubleClick)
self.button_quit = Button(frame2, text = "EXIT", width = 10, command = self.B_Exit).pack(side = LEFT)
self.button_quit = Button(frame2, text = "DELETE", width = 10).pack(side = LEFT)
self.button_quit = Button(frame2, text = "NEW", width = 10, command = self.B_ManageEnterWindow).pack(side = LEFT)
self.FileRW("R")
def OnDoubleClick(self, event):
item = self.tree.selection()[0]
manageEnterWindow(self.tree.item(item,"values"))
def B_ManageEnterWindow(self):
manageEnterWindow(0)
print "X"
def FileRW(self, value):
if value == "R":
if os.path.isfile(file_name):
self.tree.insert("" , END, text="001", values=("2017 04 11" , "VODAFONE" , "bolletta telefono" , "1y" , "60.50"))
#~ f = open(file_name, 'r')
#~ while True:
#~ x = f.readline()
#~ if x == "":
#~ break
#~ x = x[:-1]
#~ self.tree.insert("" , END, text="001", values=(x))
else:
pass
#~ f = open(file_name, 'w')
#~ f.close()
if value == "W":
pass
#~ f = open(file_name, 'w')
#~ for child in self.tree.get_children():
#~ a = ""
#~ for x in self.tree.item(child)["values"]:
#~ a = a + ' "' + x + '" '
#~ a = a[1:-1] + "\n"
#~ f.write(a)
#~ f.close
def B_Exit(self):
self.FileRW("W")
exit()
class EnterWindow:
def __init__(self, master, values):
for i in range(0, tree_nr_columns):
frame = Frame(master)
frame.pack(side = TOP)
Label(frame, text=tree_columns[i], width = 8, anchor = W).pack(side = LEFT, fill = X)
if values:
tree_txt[i] = Entry(frame, textvariable = StringVar(frame, values[i]))
else:
tree_txt[i] = Entry(frame)
tree_txt[i].pack(side = RIGHT, fill = X, expand = True)
tree_txt[0].focus_set()
frame = Frame(master)
frame.pack(side = LEFT)
self.button_quit = Button(frame, text = "BACK", width = 10, command = self.B_Exit).pack(side = LEFT)
self.button_quit = Button(frame, text = "CONFIRM", width = 10, command = self.B_Confirm).pack(side = LEFT)
#~ root2.mainloop()
def B_Confirm(self):
#~ tree.insert("" , END, text="001", values=("2017 04 11" , "VODAFONE" , "bolletta telefono" , "1y" , "60.50"))
for i in range(0, tree_nr_columns):
print i, tree_txt[i].get()
self.B_Exit()
def B_Exit(self):
root2.destroy()
def manageEnterWindow(values):
global root2
try:
x = root2.state() is not None
root2.lift()
root2.focus_force()
except:
root2 = Tk()
app = EnterWindow(root2,values)
root2.mainloop()
root = Tk()
app = MainWindow(root)
root.mainloop()
答案 0 :(得分:0)
我解决了我的问题:正如Martineau建议的那样,我传递了树的id, self.tree 作为参数。
例如:
def OnDoubleClick(self, event):
item = self.tree.selection()[0]
manageEnterWindow(self.tree, self.tree.item(item,"values"))
所以(仅举例,不是真正的代码):
def B_Confirm(self, tree_id):
tree_id.insert("" , END, text="001", values=("A","B","C","D","E"))
self.B_Exit()