1

I'm fairly new to coding with Python.

I'm making a simple voting system program where each button represents the candidates and below the button is its vote count for that corresponding candidate. The issue with my code is that it only updates the vote count of the last candidate regardless of what button I click.

I also included a total vote count and that one is working fine though. Here's what I worked on so far:

from tkinter import *

root = Tk()
root.title("Election Voting")
root.geometry("350x300")

candidates = {"Candidate Alice": 0, "Candidate Bob": 0, "Candidate Charlie": 0, "Candidate Dave": 0}
total_votes = 0


def clicked_candidate_btn():
    global candidates, total_votes

    candidates[candidate] += 1
    count.config(text=f"Vote: {candidates[candidate]}")

    total_votes += 1
    total_lbl.config(text=f"Total Votes: {total_votes}")


for candidate in candidates:
    vote_button = Button(root, text=candidate, font="None 15", command=clicked_candidate_btn)
    vote_button.pack()

    count = Label(root, text=f"Vote: {candidates[candidate]}")
    count.pack()

total_lbl = Label(root, text=f"Total Votes: {total_votes}")
total_lbl.pack()

root.mainloop()
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Kris R.
  • 7
  • 2
  • In `clicked_candidate_btn` where does `count` come from to call `config` on? – Chris May 22 '23 at 16:21
  • it's a label widget that i defined a few lines down but it's not doing anything to update each of the candidate's respective vote counts., instead, all counts collect on the last candidate.. – Kris R. May 22 '23 at 16:39
  • Hi @KrisR. I know an admin decided to close your post but seeing as you are new to Python I did have a go at fixing your code, hoping that if you see the fixes that include the use of the partial function you will understand it better seen on your own code. I added comments to explain what I did, hope everything is clear. You can see the code here: [code_for_kris](https://pastecode.io/s/fm734mm6) – Robert Salamon May 22 '23 at 17:34
  • Thank you so much. I appreciate the help. Since I'm not familiar with the partial function, I would like to clarify that this particular function helps you in changing values of variables? Thanks in advance.The code you sent is what I expected to work. – Kris R. May 23 '23 at 06:22

0 Answers0