【问题标题】:Tkinter displaying button instead of LabelTkinter 显示按钮而不是标签
【发布时间】:2014-03-18 09:47:47
【问题描述】:

我正在创建一个小应用程序,它将从用户那里获取有关银行交易的详细信息,然后将其显示在表格中。 我目前正在使用 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是传入的参数。

这是新条目后的图像。底部的两个按钮应保持原样,顶部的两个按钮应为左列中的 d e f ,右列中应为空格

【问题讨论】:

  • 我认为您应该最后创建按钮,并且它们的 .grid 选项不应定义 row=。这样,它将插入到第一个 EMPTY 行中

标签: python button tkinter label


【解决方案1】:

将 self.pack() 替换为 self.grid()。此外,要将 Label 更改为 Button,您还必须定义一个在按下 Button 时执行的命令函数,请参见下面的代码

bttn = tk.Button(self, text = "buttontitle",  command = self.do_function )
bttn.grid(row = 14, column = 4, sticky = W)


def do_function():
    print "HI"

【讨论】:

  • 我认为@Martyn 的问题是在调用outGoingData() 函数时将Label 放在Button 之上,而不是在两种类型之间切换。
  • 好的,代码的一个问题是 OP 使用 self.pack() 但在 paint() 方法中使用 grid()。使用带有权重的 pack() 重写代码以动态创建一些按钮会更容易。另一件事是在 newEntryButtons(self) 中,if else 语句说不管 if(self.numIncoming
  • @user1749431 if else 语句的原因在于 .grid(row="") 位。如果 numIncoming
  • 如果您将所有按钮更改为 pack() 那么您可以使用 if(self.numIncoming
猜你喜欢
  • 2019-02-19
  • 1970-01-01
  • 2021-01-31
  • 2017-12-31
  • 1970-01-01
  • 2019-05-28
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多