【问题标题】:Python: tkinter, adding entry to listbox doesn't responsePython:tkinter,向列表框添加条目没有响应
【发布时间】:2014-04-18 10:59:47
【问题描述】:

由于某种原因,它不会将元素添加到列表框中。我猜这是因为程序顺序,但我不确定我应该如何正确地将代码按正确的顺序放置

代码如下:

from tkinter import *

class App(Frame):

    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        self.parent = parent
        self.grid()

        self.lb = Listbox()
        self.lb.grid(column=0, row=1, columnspan=5, rowspan=4,padx=5,sticky=E+W+S+N)

        self.e = Entry()
        self.e.grid( column=0, row=0, padx=5)
        self.badd = Button(text="Add", command=self.addtolistbox())
        self.badd.grid( column=1, row=0, padx=5, pady=5)
        self.brmv = Button(text="Remove")
        self.brmv.grid(column=2, row=0, padx=5, pady=5)

    def addtolistbox(self):
        self.lb.insert(0, self.e.get())


App = App(Tk())
App.mainloop()

【问题讨论】:

    标签: python user-interface python-3.x tkinter


    【解决方案1】:

    改变这一行

    self.badd = Button(text="Add", command=self.addtolistbox())
    

    到这里

    self.badd = Button(text="Add", command=self.addtolistbox)
    

    问题在于,不是将回调函数绑定到按钮,而是执行一次函数(将空字符串添加到列表中),然后将函数调用的结果绑定到按钮,即None

    【讨论】:

      猜你喜欢
      • 2022-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 2020-10-30
      • 2013-12-23
      • 2016-04-26
      相关资源
      最近更新 更多