Skills101
← All posts

Voting Eligibility Program in Python

Pankaj Sharma ·

Python program using Tkinter where the user can enter their birth year, and the program will calculate if they are eligible for voting:

import tkinter as tk
from datetime import datetime

def check_voting_eligibility():
    birth_year = int(year_entry.get())
    current_year = datetime.now().year
    age = current_year - birth_year

    if age >= 18:
        result_label.config(text="You are eligible to vote!", fg="green")
    else:
        result_label.config(text="You are not eligible to vote.", fg="red")

# Create the main window
root = tk.Tk()
root.title("Voting Eligibility Checker")

# Create and place the widgets
tk.Label(root, text="Enter your Birth Year:").pack(pady=10)

year_entry = tk.Entry(root)
year_entry.pack(pady=5)

submit_button = tk.Button(root, text="Check Eligibility", command=check_voting_eligibility)
submit_button.pack(pady=10)

result_label = tk.Label(root, text="")
result_label.pack(pady=10)

# Start the main event loop
root.mainloop()

How the Program Works:

  • The user is prompted to enter their birth year.
  • The program calculates the user's age based on the current year.
  • If the user's age is 18 or above, the program displays a message indicating they are eligible to vote. Otherwise, it shows that they are not eligible.

Steps to Run:

  1. Copy the code into a Python script file (e.g., voting_eligibility_checker.py).
  2. Run the script using Python.
  3. A window will appear where you can enter your birth year and check eligibility.

This simple program can be expanded with more features if needed!