Tkinter:制作任意数量的按钮并获取其字段

时间:2015-05-08 23:38:27

标签: python user-interface tkinter

我正在尝试使用tKinter在Python中创建一个gui应用程序,以在按钮中显示任意数量的电话号码,并单击该按钮以显示该联系人的短信。

我拥有的是:

from tkinter import *

def displayTexts(name, list_of_Contacts):
    cont = findContact(name, list_of_Contacts)
    for text in cont.listOfTexts:
        (Message(right_frame, text=text.body)).pack(side=TOP)

for contact in list_of_Contacts:
    (Button(left_frame, text=contact.name, command= lambda: displayTexts(contact.name, list_of_Contacts))).pack()

但是,单击任何按钮会导致仅显示上次创建的联系人按钮中的最后一个文本。类似的issue已经出现了,虽然他说他解决了他的问题但我似乎无法理解如何或为什么。

我尝试调整他的代码以适应我的但我收到一个错误,说Nonetype没有listofTexts属性。

buttons = dict()
for k in list_of_Contacts:
    buttons[k.name] = Button(left_frame, text=k.name, command= lambda a=k.name: displayTexts(buttons[a], list_of_Contacts)).pack()

我无法弄清楚的一件事是如何从按钮[a]获取按钮的文本,另一件事是如何实现此列表/词典解决方案。

谢谢

编辑:我不知道Checkboxes是如何复制的,事实上我也不理解他的问题。答案听起来像是可以应用在这里,但我没有最微弱的方式。

edit2:该解决方案不起作用,它仍然为nameContant提供名称字符串的noneType。

1 个答案:

答案 0 :(得分:1)

我已经解决了这个问题。

Test 1: local 1.1.1.1  remote 2.2.2.2  state GOOD
Test ID: 2.2.2.2
Test Type: ABD
Admin State: START
DFD: Disabled
Address family: ipv4-unicast  
Options: < Refresh >  
Updates Received: 0,  Updates Sent: 7
Data Received: 853,  Data Sent: 860
Time since last received update: n/a
Number of transitions to GOOD: 1
Time since last entering GOOD state: 22384 seconds
Retry Interval: 120 seconds
Hold Time: 90 seconds,  Keep Test Time: 30 seconds

Test 2: local 1.1.1.1  remote 2.2.2.2  state GOOD
Test ID: 2.2.2.2
Test Type: ABD
Admin State: START
DFD: Disabled
Address family: ipv4-unicast  
Options: < Refresh >  
Updates Received: 0,  Updates Sent: 7
Data Received: 853,  Data Sent: 860
Time since last received update: n/a
Number of transitions to GOOD: 1
Time since last entering GOOD state: 22384 seconds
Retry Interval: 120 seconds
Hold Time: 90 seconds,  Keep Test Time: 30 seconds

Test 3: local 1.1.1.1  remote 2.2.2.2  state GOOD
Test ID: 2.2.2.2
Test Type: ABD
Admin State: START
DFD: Disabled
Address family: ipv4-unicast  
Options: < Refresh >  
Updates Received: 0,  Updates Sent: 7
Data Received: 853,  Data Sent: 860
Time since last received update: n/a
Number of transitions to GOOD: 1
Time since last entering GOOD state: 22384 seconds
Retry Interval: 120 seconds
Hold Time: 90 seconds,  Keep Test Time: 30 seconds

接受表示名称的字符串和listOfContacts。 与此同时:

def displayTexts(name, list_of_Contacts): 

没有传递一个字符串作为名称,它正在传递按钮。 通过制作它来修复:

...command= lambda a=k.name: displayTexts(buttons[a], list_of_Contacts))