复选框以确定操作是否完成 [英] Checkbox to determine if an action is completed or not

查看:60
本文介绍了复选框以确定操作是否完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下格式的客户字典:

I have a list of dictionaries of clients in a format like this:

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'}]

,并且将使用tkinter为每个客户端建立行,并在每个客户端旁边放置一个checkbox.稍后,我将添加一个button,当按下该button时,它将根据从列表中拉出的信息向每个客户端发送电子邮件.

and will be using tkinter to build rows for each client with a checkbox next to each. I will later add a button that, when pressed, will send an email to each client based on the information it pulls from the list.

当前我正在做类似的事情:

Currently I am doing something like:

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

首先,我肯定有做这更简单的方法,并会采取任何建议,对如此,但主要问题是增加一个复选框,将选中时,确定是否要发送电子邮件发送到客户端.
(稍后添加的按钮将调用一个命令,该命令将检查是否选中了每个客户端,并且仅发送给返回true等的客户端.)

First, I am sure there is an easier way to do this and will take any suggestions towards that but the main issue is adding a checkbox that will, when selected, determine whether or not to send an email to that client.
(A button, added later, will call a command that will check if each client is checked and only send to those that return true etc.)

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

I am not sure whether I should be creating a new variable to be checked, adding a key and a value to each dictionary to be read at a later point, etc.

推荐答案

首先,您应该直接在列表上进行迭代,而不要使用计数器和while循环:

First, you should iterate directly over the list rather than using a counter and a while loop:

for client in dict_list:
    currentClient = Label(text='Client: ' + client['Client']).grid(row=[i], column=1)
    ...

第二,如果您执行x=Label(...).grid(...),则x将始终为无".最佳实践是使用两个不同的语句.在这种情况下,要点很重要,因为您从不使用currentClient,但是您应该养成始终将它们分开的习惯.将您创建的小部件以及布局一起分组,这样您的GUI将会更易于管理:

Second, if you do x=Label(...).grid(...), x will always be None. Best practice is to use two different statements. In this case the point is moot since you never use currentClient, but you should get in the habit of always separating them. Group your widget creation together, and your layout together, and your GUI will be much easier to manage:

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

    clientLabel.grid(...)
    contactLabel.grid(...)
    emailLabel.grid(...)

第三-这是您问题的答案-您可以为每个检查按钮创建IntVar的实例,并将它们存储在单独的数据结构中或与数据一起存储.例如,要按公司名称存储它们,您可以这样做:

Third -- and this is the answer to your question -- you can create an instance of IntVar for each checkbutton, and store them either in a separate data structure or right along with your data. For example, to store them by business name you might do it like this:

cbVars = {}
for client in dict_list:
    ...
    bizname = client["Business Name"]
    cbVars[bizname] = IntVar()
    cb = Checkbutton(..., onvalue=1, offvalue = 0, variable = cbVars[bizname])
    ...

这篇关于复选框以确定操作是否完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆