import tkinter as tk
from tkinter import messagebox
Here, we import the Tkinter library to create the graphical user interface (GUI) and the messagebox
module to display pop-up messages.
def register():
username = entry_username.get()
password = entry_password.get()
confirm_password = entry_confirm_password.get()
if not username or not password or not confirm_password:
messagebox.showerror("Error", "Please fill in all fields.")
return
if password != confirm_password:
messagebox.showerror("Error", "Passwords do not match.")
return
# Add your registration logic here (e.g., save to a database)
messagebox.showinfo("Success", "Registration successful!")
This is the registration function. It retrieves the values entered in the username, password, and confirm password entry fields. It then performs basic validation, checking if any of the fields are empty and if the passwords match. If the validation fails, it shows an error message using messagebox.showerror
. If the validation passes, you would typically add your registration logic here, such as saving the user information to a database. Finally, it shows a success message.
root = tk.Tk()
root.title("Registration Form")
This creates the main window and sets its title.
label_username = tk.Label(root, text="Username:")
label_username.pack()
entry_username = tk.Entry(root)
entry_username.pack()
label_password = tk.Label(root, text="Password:")
label_password.pack()
entry_password = tk.Entry(root, show="*") # Show * for password
entry_password.pack()
label_confirm_password = tk.Label(root, text="Confirm Password:")
label_confirm_password.pack()
entry_confirm_password = tk.Entry(root, show="*")
entry_confirm_password.pack()
btn_register = tk.Button(root, text="Register", command=register)
btn_register.pack()
These lines create various GUI elements such as labels, entry fields, and a button. Labels are used to display text, entry fields are used for user input, and the button is used to trigger the registration process. The pack
method is used to organize these elements in a vertical layout.
root.mainloop()
This line starts the Tkinter event loop, allowing the GUI to run and respond to user interactions.
You can run this script in a Python environment, and a simple registration form window will appear. Remember that this is a basic example, and for a real-world application, you would want to add more features, improve input validation, and handle user data securely.
Photo by Emile Perron on Unsplash