Python Tkinter:无消息框弹出时打开新窗口
Hey there! Let's fix your logic here—right now your code is popping all those error messages at once, which isn't what you want. Here's the breakdown of the issue and how to fix it properly:
The Problem With Your Current Code
Your current approach stores the result of every messagebox.showerror() call in a list, which means all error boxes will pop up immediately regardless of user input. Plus, messagebox.showerror() returns the string "ok" (when the user clicks "OK"), not a boolean False—so your conditional check won't work as intended.
The Correct Approach
We need to build a sequential validation flow:
- Check each registration condition one by one.
- If a condition fails, pop the corresponding error box and stop further checks.
- Only if all conditions pass (no error boxes popped), trigger the verification page.
Modified Code Implementation
First, create a validation function that checks each requirement step-by-step:
import re import tkinter.messagebox as messagebox def validate_registration(username, password, email, full_name): # Check if all fields are filled if not username.strip() or not password.strip() or not email.strip() or not full_name.strip(): messagebox.showerror("Fill the input fields!", "Please make sure that you filled all necessary information.") return False # Check full name has a space between names and surname if " " not in full_name.strip(): messagebox.showerror("Seperate!", "Please make sure you left a space between your name(s) and surname.") return False # Check username format (no uppercase or special chars) if not username.islower() or not username.isalnum(): messagebox.showerror("Characters", "Please make sure that your username doesn't have any uppercase letters or special characters e.g. '*','!',' ' etc.") return False # Check username length if len(username) > 20: messagebox.showerror("Length", "Please make sure that your username's length is no more than 20 characters.") return False # Check if user already exists (replace with your actual user check logic) def check_user_exists(uname): # Example placeholder: replace with your database/ storage check return False if check_user_exists(username): messagebox.showerror("Existed", "User already exists, please sign in.") return False # Check email format email_pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' if not re.match(email_pattern, email.strip()): messagebox.showerror("Syntax", "Please make sure your e-mail address is in the correct format.") return False # Check password length if not 8 <= len(password) <= 20: messagebox.showerror("Length", "Please make sure your password is of 8 to 20 characters.") return False # Check password complexity has_digit = any(char.isdigit() for char in password) has_symbol = any(char in '!@#$%^&*()_+-=[]{}|;:,.<>?' for char in password) has_upper = any(char.isupper() for char in password) has_lower = any(char.islower() for char in password) if not (has_digit and has_symbol and has_upper and has_lower): messagebox.showerror("Syntax", """Please make sure that your password meets the following: 1. At least one digit (0-9), 2. At least one symbol ('!','?','*' etc.), 3. At least one uppercase letter (A-Z), 4. At least one lowercase letter (a-z) """) return False # All checks passed return True
Then, hook this function to your "Sign Up" button's click event:
# Assuming you have Tkinter entry widgets for input def on_sign_up_click(): username = username_entry.get() password = password_entry.get() email = email_entry.get() full_name = full_name_entry.get() # Run validation; open verification page only if no errors if validate_registration(username, password, email, full_name): verification_page()
Why This Works
- Each validation check runs in order. If any check fails, we show the error and exit early—no unnecessary checks or popups.
- Only when all validations pass (no error boxes are shown) does
validate_registration()returnTrue, triggeringverification_page(). - This matches exactly what you need: no error messages → open verification page.
内容的提问来源于stack exchange,提问作者cagan

