Tkinter Entry组件不更新问题:Python剪刀石头布游戏输入逻辑异常求助
Hey there! Let's break down why your game is stuck reusing the first input value every time, and how to fix it step by step.
The Root Cause
Looking at your code, the problem lies in how you're storing the player's input. You're using a global list text and appending new inputs with text.append(player_entry.get()) every time the button is clicked. But in final_calculation(), you always reference text[0]—the very first input you entered. No matter how many times you add new values to the list, you're never accessing the latest one!
Simple Fixes
Here are two straightforward ways to resolve this:
Option 1: Use a Single Variable Instead of a List
Instead of appending to a list, store only the latest input in a string variable. This way, you always work with the most recent value:
Update your global variable definition:
# Replace text = [] with this latest_input = ""Modify the
button_command()function to overwrite the variable instead of appending:def button_command(): global latest_input latest_input = player_entry.get() player_entry.delete(0, END)In
final_calculation(), replace all instances oftext[0]withlatest_input:if latest_input == 'Rock' or latest_input == 'rock': player_choice = 0 elif latest_input == 'Paper' or latest_input == 'paper': player_choice = 1 elif latest_input == 'Scissors' or latest_input == 'scissors': player_choice = 2 else: player_choice = 3 print('Something went wrong with the input')
Option 2: Skip the Global Variable Entirely (Cleaner!)
You don't even need a global variable—you can fetch the input directly in final_calculation() and clear the entry box right there. This eliminates the need for button_command() altogether if you want:
Remove the
textglobal variable and simplify your button command:player_button = Button(root, text="Confirm", command=lambda:[changeValue(), final_calculation()])Rewrite
final_calculation()to get the input directly:def final_calculation(): cmpt_choice = random.choice([0, 1, 2]) def determine_win(user_choice, cmpt_choice): return (user_choice + -cmpt_choice) % 3 # Get the latest input directly from the entry box user_input = player_entry.get() player_entry.delete(0, END) # Clear the box immediately if user_input == 'Rock' or user_input == 'rock': player_choice = 0 elif user_input == 'Paper' or user_input == 'paper': player_choice = 1 elif user_input == 'Scissors' or user_input == 'scissors': player_choice = 2 else: player_choice = 3 print('Something went wrong with the input') if player_choice != 3: result = determine_win(player_choice, cmpt_choice) print(result) return result
Testing the Fix
After making either change:
- If you first enter
Rock, it'll output the correct result (0 if computer picks 0). - Next, enter something invalid like
dsadsad, and you'll see the error message as expected. - Then enter
Paper, and it'll calculate the result based on this new input.
A quick note about your changeValue() function: right now it toggles buttonClicked but doesn't actually use it for anything. You can remove it if you don't plan to add logic that relies on it later.
Don't worry about not having the "most efficient" code—everyone starts small, and figuring out these little bugs is how you learn! 😊
内容的提问来源于stack exchange,提问作者user14177981

