我正在创建一个小应用程序,它将从用户处获取有关银行交易的详细信息,然后将其显示在表格中。 我目前正在使用CSV文件存储数据,然后在放入新数据时,它也会显示。
我有一个标签列表,然后在它们底部有一个按钮来提交新条目。我得到的问题是当表的长度超过它开始时的显示错误。
class accountant(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
master.title("Accountant")
self.pack()
self.numIncoming = 0
self.numOutgoing = 0
self.incoming = fs.fileStore("incoming", "csv")
print("log: incoming.csv opened sucessfully")
self.outgoing = fs.fileStore("outgoing", "csv")
print("log: outgoing.csv opened sucessfully")
self.setup()
def setup(self):
self.paint()
self.incomingData()
self.outgoingData()
self.newEntryButtons()
def paint(self):
tk.Label(self, width=45, text="incoming").grid(row=1, column=0, columnspan=45)
tk.Label(self, width=45, text="outgoing").grid(row=1, column=45, columnspan=45)
tk.Label(self, width=15, text="Date").grid(row=2, column=0, columnspan=15)
tk.Label(self, width=15, text="Transaction Name").grid(row=2, column=15, columnspan=15)
tk.Label(self, width=15, text="Amount").grid(row=2, column=30, columnspan=15)
tk.Label(self, width=15, text="Date").grid(row=2, column=45, columnspan=15)
tk.Label(self, width=15, text="Transaction Name").grid(row=2, column=60, columnspan=15)
tk.Label(self, width=15, text="Amount").grid(row=2, column=75, columnspan=15)
def incomingData(self):
self.incoming.closeFile()
self.incoming.openFile()
i = 3
for cell in self.incoming.reader:
#cell[0] = Date, cell[1]= Transaction Name, cell[2] =amount
tk.Label(self, width=15, text=cell[0]).grid(row=i, column=0, columnspan=15)
tk.Label(self, width=15, text=cell[1]).grid(row=i, column=15, columnspan=15)
tk.Label(self, width=15, text=cell[2]).grid(row=i, column=30, columnspan=15)
i += 1
self.numIncoming = i
print("incoming:", self.numIncoming-3)
print("outgoing:", self.numOutgoing-3)
def outgoingData(self):
self.outgoing.closeFile()
self.outgoing.openFile()
i = 3
for cell in self.outgoing.reader:
#cell[0] = Date, cell[1]= Transaction Name, cell[2] =amount
tk.Label(self, width=15, text=cell[0]).grid(row=i, column=45, columnspan=15)
tk.Label(self, width=15, text=cell[1]).grid(row=i, column=60, columnspan=15)
tk.Label(self, width=15, text=cell[2]).grid(row=i, column=75, columnspan=15)
i += 1
self.numOutgoing = i
print("incoming:", self.numIncoming-3)
print("outgoing:", self.numOutgoing-3, '\n\n')
def newEntryButtons(self):
if(self.numIncoming < self.numOutgoing):
tk.Button(self, text="new incoming", width=45, command=lambda: self.newEntry(self.incoming)).grid(row=self.numOutgoing, column=0, columnspan=45)
tk.Button(self, text="new outgoing", width=45, command=lambda: self.newEntry(self.outgoing)).grid(row=self.numOutgoing, column=45, columnspan=45)
else:
tk.Button(self, text="new incoming", width=45, command=lambda: self.newEntry(self.incoming)).grid(row=self.numIncoming, column=0, columnspan=45)
tk.Button(self, text="new outgoing", width=45, command=lambda: self.newEntry(self.outgoing)).grid(row=self.numIncoming, column=45, columnspan=45)
def newEntry(self, inFile):
win = tk.Toplevel()
self.newName = tk.StringVar()
self.newDate = tk.StringVar()
self.newAmount = tk.StringVar()
tk.Label(win, width=5, text="Name:").grid(row=0, column=0, columnspan=5)
tk.Entry(win, textvariable=self.newName).grid(row=0, column=5, columnspan=5)
tk.Label(win, width=5, text="date:").grid(row=1, column=0, columnspan=5)
tk.Entry(win, textvariable = self.newDate).grid(row=1, column=5, columnspan=5)
tk.Label(win, width=5, text="amount: £").grid(row=2, column=0, columnspan=5)
tk.Entry(win, textvariable=self.newAmount).grid(row=2, column=5, columnspan=5)
button = tk.Button(win, text="submit", width=5, command= lambda: self.submit(win, inFile))
button.grid(row=5, column=5, columnspan=5)
def submit(self, win, inFile):
with open(inFile.file, 'a') as f:
string= '\n'+self.newName.get() + ',' + self.newDate.get() + ',' + self.newAmount.get()
f.write(string)
if inFile.fileName == "incoming":
self.numIncoming += 1
# print("incoming:", self.numIncoming-3)
# print("outgoing:", self.numOutgoing-3)
else:
self.numOutgoing += 1
print("outgoing:", self.numOutgoing-3)
win.destroy()
self.setup()
filestore只是一个基本上打开csv的类
reader = csv.reader(open(file+'.'fileExt))
其中file和fileExt是传入的参数。
这是新条目之后的图像。底部的两个按钮应该保持不变,前两个按钮应该在左列中,而在右列中只有空格
答案 0 :(得分:1)
将self.pack()替换为self.grid()。要将标签更改为按钮,您还必须定义按下按钮时执行的命令功能,请参阅下面的代码
bttn = tk.Button(self, text = "buttontitle", command = self.do_function )
bttn.grid(row = 14, column = 4, sticky = W)
def do_function():
print "HI"