0% found this document useful (0 votes)
40 views

Simpleapp Py

This Python code creates a GUI using Tkinter to collect personal information from a user including name, age, gender, and newsletter subscription preference. It defines a submit function to validate the input data and display it in a message box. Widgets like labels, entries, radio buttons, and checkboxes are added to the main window to collect the information. A submit button calls the validation and display function when clicked.

Uploaded by

Gurudeep BN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Simpleapp Py

This Python code creates a GUI using Tkinter to collect personal information from a user including name, age, gender, and newsletter subscription preference. It defines a submit function to validate the input data and display it in a message box. Widgets like labels, entries, radio buttons, and checkboxes are added to the main window to collect the information. A submit button calls the validation and display function when clicked.

Uploaded by

Gurudeep BN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import tkinter as tk

from tkinter import messagebox

def submit():
# Retrieve data from widgets
name = name_entry.get()
age = age_entry.get()
gender = gender_var.get()
newsletter = newsletter_var.get()

# Validate data
if not name:
messagebox.showerror("Error", "Please enter your full name.")
return
try:
age_value = int(age)
if age_value < 1 or age_value > 120:
raise ValueError
except ValueError:
messagebox.showerror("Error", "Please enter a valid age (1-120).")
return

# Display or save the data (for now, just display using a messagebox)
message = f"Name: {name}\nAge: {age}\nGender: {gender}\nSubscribe to Newsletter:
{'Yes' if newsletter else 'No'}"
messagebox.showinfo("Information Collected", message)

# Create the main window


root = tk.Tk()
root.title("Personal Information Collector")
root.geometry("400x400")

# Add label and entry for Full Name


name_label = tk.Label(root, text="Full Name:")
name_label.pack(pady=10)
name_entry = tk.Entry(root)
name_entry.pack(pady=10)

# Add label and entry for Age


age_label = tk.Label(root, text="Age:")
age_label.pack(pady=10)
age_entry = tk.Entry(root)
age_entry.pack(pady=10)

# Add Radio buttons for Gender


gender_label = tk.Label(root, text="Gender:")
gender_label.pack(pady=10)
gender_var = tk.StringVar(value="Male")
male_rb = tk.Radiobutton(root, text="Male", variable=gender_var, value="Male")
male_rb.pack(anchor='w')
female_rb = tk.Radiobutton(root, text="Female", variable=gender_var, value="Female")
female_rb.pack(anchor='w')
other_rb = tk.Radiobutton(root, text="Other", variable=gender_var, value="Other")
other_rb.pack(anchor='w')

# Add Checkbox for Newsletter Subscription


newsletter_var = tk.BooleanVar()
newsletter_cb = tk.Checkbutton(root, text="Subscribe to Newsletter",
variable=newsletter_var)
newsletter_cb.pack(pady=10)

# Add Submit button


submit_btn = tk.Button(root, text="Submit", command=submit)
submit_btn.pack(pady=20)

# Start the GUI loop


root.mainloop()

You might also like