You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter Entry组件不更新问题:Python剪刀石头布游戏输入逻辑异常求助

Fixing the Input Update Issue in Your Tkinter Rock-Paper-Scissors Game

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:

  1. Update your global variable definition:

    # Replace text = [] with this
    latest_input = ""
    
  2. 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)
    
  3. In final_calculation(), replace all instances of text[0] with latest_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:

  1. Remove the text global variable and simplify your button command:

    player_button = Button(root, text="Confirm", command=lambda:[changeValue(), final_calculation()])
    
  2. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 11:57:32