Python Flet 错误:接受 0 个位置参数,但给出了 1 个

问题描述 投票:0回答:1

我正在尝试使用 Python 和 flet 创建一个石头、剪刀、布应用程序。我得到:

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

我不明白该错误告诉我什么。我试图让它将“choices_text”从“玩家与计算机”更改为每个实体选择的任何内容(“玩家”更改为玩家的选择,“计算机”更改为计算机选择的随机发生器。)然后“winner_textField”将输出谁赢了或者是否平局。

这是迄今为止我的代码:

import flet as ft
import random

def main(page: ft.Page):

    ############ function ##################
    def rock():
        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():
        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():
        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():
        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)
python button flet
1个回答
0
投票

在 flet 中,

on_click
将调用指定的函数,其中包含一些控件细节。所以,被调用的函数就像
rock(e)
。更改您的函数以采用虚拟参数,并且将设置错误。

© www.soinside.com 2019 - 2024. All rights reserved.