【问题标题】:Use a variable to choose a dictionary to search in Python 3 and Tkinter?使用变量选择要在 Python 3 和 Tkinter 中搜索的字典?
【发布时间】:2013-05-04 17:20:04
【问题描述】:

我有一些客户,例如customer1。我已经将他们的数据存储在这样的字典中:

customer1 = {"Age": 20, "Gender": "Male",}

我想要一个 Tkinter 输入框和一个名为 searchterm 的文本变量:

searchterm = StringVar()
entry = Entry(frame, textvariable=searchterm)

然后我希望能够使用输入的值找到字典,然后在文本小部件中显示数据。

我是 python 新手,所以请帮忙。

【问题讨论】:

  • 不要使用每个客户的变量。改用字典; customers = {} 然后customers['customer1'] = {...}

标签: python python-3.x tkinter


【解决方案1】:

假设您有一个名为 frame1 的框架

#Create Text to present result and Entry for input

text1 = Text(frame1,bg="white",width=60)
entry1 = Entry(frame1,bg="yellow")

#The handler necessary to process input

def handler1(event):
        text1.delete(1.0,END)
        data=entry1.get()
        #What ever you want to do with data#
        #get result...#
        text1.insert(result)

#A button to engage your function with input

b1=Button(self,text="Open",width=20)
b1.bind("<Button-1>",handler1)

【讨论】:

  • 很抱歉,我认为我没有正确解释。这是我已经拥有的代码: from tkinter import * root = Tk() frame = Frame() word1 = StringVar() customer1 = {'Lawn_Area': '20 m2', 'Lawn_Cost': '£30',} entry = Entry(frame, textvariable=word1) entry.pack() text = Text(frame, width=40, height=10) text.pack() diction = word1.get() text.insert('1.0', diction. items()) root.mainloop()
  • 所有客户的字典格式是什么?或者您的客户数据在哪里?在文件中?
  • 抱歉,我不太明白你的意思?
  • 首先,你没有正确调用字典。
  • 很难正确阅读您的代码,但正如我所见,您从不调用客户字典...您编写 text.insert('1.0', diction.items()) 但 diction是一个字符串,它是你的输入而不是字典,这就是为什么你应该得到一个错误。在我看来,您必须使用字典从客户集合中拨打电话...意思是上面提到的 Martijn Pieters...例如 text.insert('1.0', customers[diction]) where customers = {customer1 , 客户2, ...}
猜你喜欢
  • 1970-01-01
  • 2015-08-13
  • 2017-10-02
  • 2017-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多