简体   繁体   English

TypeError:method()接受1个位置参数,但给出2个(Python和Kivy)

[英]TypeError: method()takes in 1 positional argument but 2 were given (Python and Kivy)

Im trying to use a function inside my py file to change screens after a certain number of seconds, these are the 2 screens and the ScreenManager(although not all the code,shortened it to the important bits): 我试图在我的py文件中使用函数在一定秒数后更改屏幕,这些是2个屏幕和ScreenManager(尽管不是所有代码,都将其缩短为重要的位):

class StartScreen(Screen):
    pass

class Buttons(Screen):

    def change_screen(self):
        WindowManager.current = "start_screen"


class WindowManager(ScreenManager):
    pass

kv = Builder.load_file("my.kv")

class MyApp(App):

    def build(self):
        return kv


if __name__ == '__main__':
    Window.fullscreen = "auto"
    MyApp().run()

and this is the whole kv file: 这是整个kv文件:

#:import NoTransition kivy.uix.screenmanager.NoTransition
#:import Clock kivy.clock.Clock

WindowManager:
    transition: NoTransition()

    StartScreen:
    Buttons:


<StartScreen>:
    name: "start_screen"

    Button:
        background_normal: "maxresdefault.jpg"
        background_down: "maxresdefault.jpg"
        size_hint: 0.3, 0.3
        pos_hint: {"x": .35, "y": .35}
        text: "Play"
        font_size: 250
        on_release:
            app.root.current = "btn_screen"
            root.reset_score()


<Buttons>:
    name: "btn_screen"
    btn: btn
    on_enter:
        Clock.schedule_once(root.change_screen, 5)

    Button:
        background_normal: "pepe11.png"
        background_down: "pepe11.png"
        id: btn
        size_hint: 0.2, 0.3
        pos_hint: {"x": .4, "y": .35}
        on_press:
            root.respawn()

and so my problem is with the Clock.schedule_once command I try to call when entering the Buttons screen, I just keep getting "TypeError: change_screen() takes 1 positional argument but 2 were given". 所以我的问题出在进入Buttons屏幕时尝试调用Clock.schedule_once命令,我一直在获取“ TypeError:change_screen()接受1个位置参数,但给出2个”。

Thank you all in advance. 谢谢大家。

You have 2 errors: 您有2个错误:

  1. Clock.schedule_once() passes as an argument the time interval to the callback so you must pass an additional parameter. Clock.schedule_once()将时间间隔作为参数传递给回调,因此您必须传递一个附加参数。

  2. WindowManager.current is not valid because current is not a static attribute so you must access it through an object, in the Screen case you can access the ScreenManager that manages it through the manager property. WindowManager.current无效,因为current不是静态属性,因此您必须通过一个对象访问它,在Screen情况下,您可以访问通过manager属性管理它的ScreenManager

So the solution is: 因此解决方案是:

# ...
class Buttons(Screen):
    def change_screen(self, dt):
        self.manager.current = "start_screen"
# ...

Clock uses the dt argument (delta-time) for the callback function. Clock使用dt参数(增量时间)作为回调函数。
You have to change your function like this: 您必须像这样更改功能:

def change_screen(self, dt):
    WindowManager.current = "start_screen"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:函数需要 1 个位置参数,但 2 个被赋予了 kivy python - TypeError: Function takes 1 positional argument but 2 were given kivy python TypeError:method() 接受 1 个位置参数,但给出了 2 个 - TypeError: method() takes 1 positional argument but 2 were given TypeError: method() 接受 1 个位置参数,但给出了 4 个。 [使用 tkinter] - TypeError: method() takes 1 positional argument but 4 were given. [using tkinter] Python3 TypeError:接受1个位置参数,但给定2个 - Python3 TypeError: takes 1 positional argument but 2 were given Python 继承 - 类型错误:__init__() 采用 1 个位置参数,但给出了 4 个 - Python Inheritance - TypeError: __init__() takes 1 positional argument but 4 were given Python Selenium TypeError:__init__() 需要 1 个位置参数,但给出了 2 个 - Python Selenium TypeError: __init__() takes 1 positional argument but 2 were given Python tqdm TypeError: <lambda> ()接受1个位置参数,但给出了2个 - Python tqdm TypeError: <lambda>() takes 1 positional argument but 2 were given 类型错误:__init__() 需要 1 个位置参数,但给了 2 个 Python - TypeError: __init__() takes 1 positional argument but 2 were given Python TypeError:parse()接受1个位置参数,但给出了6个 - TypeError: parse() takes 1 positional argument but 6 were given 类型错误:需要 1 个位置参数,但给出了 2 个 - TypeError: takes 1 positional argument but 2 were given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM