Checkbox to determine if an action is completed or not

2024/10/15 16:20:38

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

dict_list = [{'Name of Business' : 'Amazon', 'Contact Name' : 'Jeff Bezos', 'Email' : '[email protected]'}, {'Name of Business' : 'Microsoft', 'Contact Name' : 'Bill Gates', 'Email' : '[email protected]'}]

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

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.

Answer

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)...

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(...)

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])...
https://en.xdnf.cn/q/117812.html

Related Q&A

Python not concatenating string and unicode to link

When I append a Unicode string to the end of str, I can not click on the URL.Bad:base_url = https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&tit…

Decrypt python/django password with Symfony 2.5 (using symfony security)

I want to use symfony 2.5.10 security in order to login in from users that were created with pyhton/django security. Passwords in db that are encrypted in this format:pbkdf2_sha256$12000$dVPTWPll8poG$3…

Reuse getCmd object in pysnmp

In the pysnmp documentation there is a getCmd class, I was wondering if it was possible to just instantiate the class once and reuse it at a later point by passing it new oids. I am not sure if the ge…

Django plugged into Apache not working like Django standalone

I have encountered a hodgepodge of errors trying to bring my django site into production with Apache. Having finally gotten mod_wsgi sorted and Apache at least seeming to be trying to load the site Im …

How to filter overlap rows in a big file in python

I am trying to filter overlap rows in a big file in python.The overlap degrees is set to 25%. In other words,the number of element of intersection between any two rows is less than 0.25 times of union …

Installing wxPython in Ubuntu 12.10

I am trying to install wxPython on my Ubuntu 12.10 but with no success. I have gone through all the answers given on this website. Can someone please help me in this or point me in the right direction.…

Open a file name +date as csv in Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

TextCtrl providing an out of bound exception in wxPython

I am new to WX, so I decided to make a program that will periodically write out a line of text to the screen based on an outside input. The basis of the program contains a basic window with the multili…

Matplotlib: different color for every point of line plot

Im trying to make a plot like in the following figure (source of image): Im talking about the plot in the right panel even though there is some correlation between the two panels: the colorbar. Just s…

getting template syntax error in django template

my code in view :tracks = client.get(/tracks, order=hotness, limit=4) artwork_url=[] for track in tracks:artwork_url.append(str(track.artwork_url).replace("large", "t300x300")) …