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

使用Flet开发石头剪刀布应用遇TypeError:函数参数不匹配

石头剪刀布Flet应用错误修复方案

错误解析

你遇到的错误:

TypeError: main.<locals>.rock() takes 0 positional arguments but 1 was given

原因是Flet的按钮点击回调函数会自动传递一个点击事件对象作为参数,但你的rock()、paper()、scissors()、reset()函数都没有定义参数接收这个对象,导致调用时参数不匹配。

修复后的完整代码

只需给每个按钮回调函数添加一个参数(参数名可自定义,比如e)即可解决问题,同时保留你原本的功能逻辑:

import flet as ft
import random

def main(page: ft.Page):

    ############ function ##################
    def rock(e):
        computer = random.choice(['rock', 'paper', 'scissors'])
        if computer == 'rock':
            choices_text.value = "Rock vs Rock"
            winner_textField.value = "It's a tie!"
        elif computer == "paper":
            choices_text.value = "Rock vs Paper"
            winner_textField.value = "Computer wins!"
        else:
            choices_text.value = "Rock vs Scissors"
            winner_textField.value = "You win!"
        page.update()

    def paper(e):
        computer = random.choice(['rock', 'paper', 'scissors'])
        if computer == 'rock':
            choices_text.value = "Paper vs Rock"
            winner_textField.value = "You win!"
        elif computer == "paper":
            choices_text.value = "Paper vs Paper"
            winner_textField.value = "It's a tie!"
        else:
            choices_text.value = "Paper vs Scissors"
            winner_textField.value = "Computer wins!"
        page.update()
    
    def scissors(e):
        computer = random.choice(['rock', 'paper', 'scissors'])
        if computer == 'rock':
            choices_text.value = "Scissors vs Rock"
            winner_textField.value = "Computer wins!"
        elif computer == "paper":
            choices_text.value = "Scissors vs Paper"
            winner_textField.value = "You win!"
        else:
            choices_text.value = "Scissors vs Scissors"
            winner_textField.value = "It's a tie!"
        page.update()
        
    def reset(e):
        choices_text.value = "Player vs Computer"
        winner_textField.value = " "
        page.update()

    ############# page setup ################
    page.title="Rock Paper Scissors"
    page.window_width = 500
    page.window_height = 700
    page.theme_mode = "light"
    page.window_center()
    page.vertical_alignment=ft.MainAxisAlignment.SPACE_EVENLY
    page.horizontal_alignment=ft.CrossAxisAlignment.CENTER

    ########### page content ###############
    title_text = ft.Text("Rock Paper Scissors", color="BLUE", size=30)
    choices_text = ft.Text("Player vs Computer", color="BLACK", size=25)

    ########### output ###############
    winner_textField = ft.TextField(width=300, height=100, color="BLACK", text_size=20, text_align="center")    

    ########### input buttons ###############
    rock_button = ft.ElevatedButton(content=ft.ResponsiveRow([ft.Text("ROCK", size=20, text_align="center")]), width=150, height=100, bgcolor="BLUE", color="WHITE", on_click=rock)
    paper_button = ft.ElevatedButton(content=ft.ResponsiveRow([ft.Text("PAPER", size=20, text_align="center")]), width=150, height=100, bgcolor="BLUE", color="WHITE", on_click=paper)
    scissors_button = ft.ElevatedButton(content=ft.ResponsiveRow([ft.Text("SCISSORS", size=20, text_align="center")]), width=200, height=100, bgcolor="BLUE", color="WHITE", on_click=scissors)  

    ############ reset button ###############
    reset_button = ft.ElevatedButton(content=ft.ResponsiveRow([ft.Text("Reset", size=20, text_align="center")]), width=300, height=100, bgcolor="BLUE", color="WHITE", on_click=reset)


    page.add(
        title_text, 
        choices_text, 
        rock_button, 
        paper_button, 
        scissors_button, 
        winner_textField,
        reset_button
    )

ft.app(target=main)

额外优化建议(可选)

代码中三个选择函数逻辑高度重复,可抽成通用函数减少冗余,示例如下:

def handle_choice(e, player_choice):
    computer_choice = random.choice(['rock', 'paper', 'scissors'])
    # 更新选择文本
    choices_text.value = f"{player_choice.capitalize()} vs {computer_choice.capitalize()}"
    # 判断胜负
    if player_choice == computer_choice:
        winner_textField.value = "It's a tie!"
    elif (player_choice == 'rock' and computer_choice == 'scissors') or \
         (player_choice == 'paper' and computer_choice == 'rock') or \
         (player_choice == 'scissors' and computer_choice == 'paper'):
        winner_textField.value = "You win!"
    else:
        winner_textField.value = "Computer wins!"
    page.update()

# 按钮绑定修改为:
rock_button = ft.ElevatedButton(content=ft.ResponsiveRow([ft.Text("ROCK", size=20, text_align="center")]), width=150, height=100, bgcolor="BLUE", color="WHITE", on_click=lambda e: handle_choice(e, 'rock'))
paper_button = ft.ElevatedButton(content=ft.ResponsiveRow([ft.Text("PAPER", size=20, text_align="center")]), width=150, height=100, bgcolor="BLUE", color="WHITE", on_click=lambda e: handle_choice(e, 'paper'))
scissors_button = ft.ElevatedButton(content=ft.ResponsiveRow([ft.Text("SCISSORS", size=20, text_align="center")]), width=200, height=100, bgcolor="BLUE", color="WHITE", on_click=lambda e: handle_choice(e, 'scissors'))

这样能大幅减少重复代码,后期维护更方便。

内容的提问来源于stack exchange,提问作者Anna Quigley

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 02:45:20