【发布时间】:2017-07-07 15:55:47
【问题描述】:
你好 Python 社区,
我正在使用 Python 3.6,我不知道如何将空列表中的存储项目显示到列表框中。谁能评估我的代码并告诉我我缺少什么?提前致谢!
from tkinter import *
root = Tk()
root.title("Name Entry")
root.geometry("240x250")
mylist = []
def get_data(l):
l.append(box1.get())
print(l)
label1 = Label(root,text = "ID:",height = 2)
label1.grid(row = 0, column = 0)
ID=StringVar()
box1 = Entry(root, bd = 4, textvariable = ID)
box1.grid(row = 0, column = 1)
botonA = Button(root, text = "accept",command=lambda: get_data(mylist), width = 5)
botonA.grid(row = 0, column = 2)
list_names = Listbox(root).grid(row = 2, column = 1, rowspan = 7)
for item in mylist:
list_names.insert("end", item)
root.mainloop()
在 Matteo 的帮助下,我能够创造出我想要的东西。再次感谢您为我解决这个问题! :)
{from tkinter import *
root = Tk()
root.title("Name Entry")
root.geometry("240x250")
mylist = []
def get_data(l):
l.append(box1.get())
print(l)
display_data()
def display_data():
list_names.delete(0, "end")
for items in mylist:
list_names.insert(END, items)
label1 = Label(root,text = "ID:",height = 2)
label1.grid(row = 0, column = 0)
ID = StringVar()
box1 = Entry(root, bd = 4, textvariable = ID)
box1.grid(row = 0, column = 1)
botonA = Button(root, text = "accept",command = lambda: get_data(mylist),
width = 5)
botonA.grid(row = 0, column = 2)
list_names = Listbox(root)
list_names.grid(row = 2, column = 1, rowspan = 7)
root.mainloop()}
【问题讨论】:
-
您好,欢迎来到 Stack Overflow,请花点时间通过 welcome tour 了解您在此处的方式(并获得您的第一个徽章),阅读如何创建 Minimal, Complete, and Verifiable example并检查How to Ask Good Questions,这样您就有机会获得反馈和有用的答案。
-
如何将一个项目存储在一个空列表中?如果有东西存放在那里,它就不会是空的。
标签: python list tkinter listbox python-3.6