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