Python-从多维列表中搜索并在TKINTER小部件上显示

发布于 2025-01-24 01:49:24 字数 512 浏览 0 评论 0原文

这是我的列表:

contactlist = [
    ['Person1 Lastname1', '123123', 'Random City 1'],
    ['Person2 Lastname2', '246810', 'Random City 2'],
    ['Person3 Lastname3', '13579123', 'Random City 3'],
    ]

我有一个tkinter小部件,用户可以在其中输入名称以及显示搜索结果的位置。

Label(root,text = 'NAME',font = 'arial 12 bold',bg = 'SlateGray3').place(x=30,y=20)
Entry(root,textvariable = Name).place(x = 100,y = 20)

我要做的是每当用户输入窗口小部件(名称),然后按“搜索”按钮时,它将在小部件中显示全名。我该怎么做?

编辑:有关目标的更多澄清

This is my list:

contactlist = [
    ['Person1 Lastname1', '123123', 'Random City 1'],
    ['Person2 Lastname2', '246810', 'Random City 2'],
    ['Person3 Lastname3', '13579123', 'Random City 3'],
    ]

I have a TKinter Widget where the user can input a name and where the search result would be displayed.

Label(root,text = 'NAME',font = 'arial 12 bold',bg = 'SlateGray3').place(x=30,y=20)
Entry(root,textvariable = Name).place(x = 100,y = 20)

What i want to do is whenever the user inputs something in the widget (name), and then press the "search" button, it will display the full name in the widget. How do i do this?

Edit: More clarifcation on objective

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮子 2025-01-31 01:49:24

您可以尝试使用此程序,只是一个原始界面,您可以自己对其进行样式。

from tkinter import *

root = Tk()
contactlist = [
    ['Person1 Lastname1', '123123', 'Random City 1'],
    ['Person2 Lastname2', '246810', 'Random City 2'],
    ['Person3 Lastname3', '13579123', 'Random City 3'],
    ]
l1 = Label(root,text = 'Name')
l1.pack()
l2 = Label(root,text = 'Phn')
l2.pack()
l3 = Label(root,text = 'City')
l3.pack()
e = Entry(root)
e.pack()
def upd():
  global l1, l2, l3, e, contactlist
  resarr = [i for i in contactlist if i[0] == e.get()]
  if len(resarr)>0:
    l1.configure(text = resarr[0][0])
    l2.configure(text = resarr[0][1])
    l3.configure(text = resarr[0][2])
btn1 = Button(root, command=upd, text="Search")
btn1.pack()
root.mainloop()

您也可以自动替换

btn1 = Button(root, command=upd, text="Search")
btn1.pack()

e.bind("<KeyRelease>", upd)

更新内容

You can try using this program, just a raw interface you can style it yourself.

from tkinter import *

root = Tk()
contactlist = [
    ['Person1 Lastname1', '123123', 'Random City 1'],
    ['Person2 Lastname2', '246810', 'Random City 2'],
    ['Person3 Lastname3', '13579123', 'Random City 3'],
    ]
l1 = Label(root,text = 'Name')
l1.pack()
l2 = Label(root,text = 'Phn')
l2.pack()
l3 = Label(root,text = 'City')
l3.pack()
e = Entry(root)
e.pack()
def upd():
  global l1, l2, l3, e, contactlist
  resarr = [i for i in contactlist if i[0] == e.get()]
  if len(resarr)>0:
    l1.configure(text = resarr[0][0])
    l2.configure(text = resarr[0][1])
    l3.configure(text = resarr[0][2])
btn1 = Button(root, command=upd, text="Search")
btn1.pack()
root.mainloop()

You can also replace

btn1 = Button(root, command=upd, text="Search")
btn1.pack()

with

e.bind("<KeyRelease>", upd)

To update contents automatically

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文