Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于确定操作是否已完成的复选框_Python_List_Dictionary_Tkinter - Fatal编程技术网

Python 用于确定操作是否已完成的复选框

Python 用于确定操作是否已完成的复选框,python,list,dictionary,tkinter,Python,List,Dictionary,Tkinter,我有一个客户字典列表,格式如下: dict_list = [{'Name of Business' : 'Amazon', 'Contact Name' : 'Jeff Bezos', 'Email' : 'Jeff@Amazon.com'}, {'Name of Business' : 'Microsoft', 'Contact Name' : 'Bill Gates', 'Email' : 'Bill@Microsoft.com'}] cbVars = {} for client in d

我有一个客户字典列表,格式如下:

dict_list = [{'Name of Business' : 'Amazon', 'Contact Name' : 'Jeff Bezos', 'Email' : 'Jeff@Amazon.com'}, {'Name of Business' : 'Microsoft', 'Contact Name' : 'Bill Gates', 'Email' : 'Bill@Microsoft.com'}]
cbVars = {}
for client in dict_list:
    ...
    bizname = client["Business Name"]
    cbVars[bizname] = IntVar()
    cb = Checkbutton(..., onvalue=1, offvalue = 0, variable = cbVars[bizname])
    ...
并将使用
tkinter
为每个客户端构建行,每个客户端旁边都有一个
复选框。稍后我将添加一个
按钮
,按下该按钮时,将根据从列表中提取的信息向每个客户发送电子邮件

目前我正在做一些类似的事情:

ClientCount = len(dict_list)
CurrentCount = 0 

while CurrentCount < ClientCount:
    for i in range(ClientCount):
        currentClient = Label(text='Client: ' + dict_list[i]['Client']).grid(row=[i], column=1)
        currentContactName = Label(text='Contact Name: ' + dict_list[i]['Contact Name']).grid(row=[i], column=2)
        currentEmail = Label(text='Contact Email: ' + dict_list[i]['Email']).grid(row=[i], column=3)
        CurrentCount += 1
ClientCount=len(目录列表)
CurrentCount=0
当CurrentCount
首先,我相信有一种更简单的方法可以做到这一点,我会采纳任何建议,但主要问题是添加一个复选框,当选中该复选框时,它将决定是否向该客户发送电子邮件。
(稍后添加的按钮将调用一个命令,该命令将检查是否已检查每个客户端,并仅发送给返回true的客户端等。)


我不确定是否应该创建一个要检查的新变量,向每个字典添加一个键和一个值以便以后读取,等等。

首先,您应该直接遍历列表,而不是使用计数器和while循环:

for client in dict_list:
    currentClient = Label(text='Client: ' + client['Client']).grid(row=[i], column=1)
    ...
其次,如果执行
x=Label(…).grid(…)
x
将始终为无。最佳实践是使用两种不同的语句。在这种情况下,这一点毫无意义,因为您从未使用过
currentClient
,但您应该养成总是将它们分开的习惯。将您的小部件创建和布局组合在一起,您的GUI将更易于管理:

for client in dict_list:
    clientLabel = Label(...)
    contactLabel = Label(...)
    emailLabel = Label(...)

    clientLabel.grid(...)
    contactLabel.grid(...)
    emailLabel.grid(...)
第三,这是您问题的答案,您可以为每个复选按钮创建一个
IntVar
实例,并将它们存储在单独的数据结构中或与数据一起存储。例如,要按企业名称存储它们,您可以这样做:

dict_list = [{'Name of Business' : 'Amazon', 'Contact Name' : 'Jeff Bezos', 'Email' : 'Jeff@Amazon.com'}, {'Name of Business' : 'Microsoft', 'Contact Name' : 'Bill Gates', 'Email' : 'Bill@Microsoft.com'}]
cbVars = {}
for client in dict_list:
    ...
    bizname = client["Business Name"]
    cbVars[bizname] = IntVar()
    cb = Checkbutton(..., onvalue=1, offvalue = 0, variable = cbVars[bizname])
    ...

我不认为有任何必要使用
while
循环
CurrentCount+=1
…for循环就足够了…感谢到目前为止的帮助!我现在唯一的问题是验证这个变量是否真的被复选框修改了。我在复选框上做了一个返回变量的命令,但是它总是显示为零,所以我认为我仍然做了一些错误的事情。