[英]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个错误:
Clock.schedule_once()
passes as an argument the time interval to the callback so you must pass an additional parameter. Clock.schedule_once()
将时间间隔作为参数传递给回调,因此您必须传递一个附加参数。
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"
# ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.