【问题标题】:How to store a string in a empty list and display the results in a listbox如何将字符串存储在空列表中并在列表框中显示结果
【发布时间】: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


【解决方案1】:

您必须在按下按钮时插入元素!在您的代码中,当 mylist 为空时,您将其添加到列表框中。

这是工作代码:

from tkinter import *

root = Tk()
root.title("Name Entry")
root.geometry("240x250")

mylist = []


def get_data(l):
    l.append(box1.get())
    list_names.insert(END, l)
    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)
list_names.grid(row=2, column=1, rowspan=7)

root.mainloop()

我还修改了另外两处:

  • 我不确定这是否是您想要的,但 l.append(box1.get()) 将列表的所有元素添加到列表框中,而不仅仅是最后一个一个我认为你需要的。

  • list_names = Listbox(root).grid(row = 2, column = 1, rowspan = 7) 表示 list_names 变量是网格函数(无)。您必须首先保存 ListBox 变量和网格。

【讨论】:

  • 这就是问题所在——但这有效吗?函数能看到list_names吗?
  • 是的,因为 get_data 在列表框创建后运行。实际上,代码可以用更好的方式编写,因为调用尚未创建的全局变量(在本例中为列表框变量)的函数永远不会好。
  • 伙计们!感谢您为我解决这个问题!!!!刚接触 python 并知道 v2 和 v3 之间的区别有点困难。 Matteo,使用您提供的数据,我能够准确地创建我正在寻找的东西。我还添加了一个函数来清除列出的名称,然后垂直显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
  • 2018-05-11
  • 2022-06-12
  • 1970-01-01
相关资源
最近更新 更多