尝试更改屏幕时出现Kivy错误

时间:2020-01-11 20:37:43

标签: python-3.x kivy

我不想使用.kv语言。因此,当我按下按钮尝试运行功能时,就会出现我的问题。
我发现这些问题也有相同的问题,因此我从他们那里采用了on_press方法的代码:
kivy python passing parameters to fuction with button click
Python, Kivy, “AssertionError: None is not callable” Error on function call by button

错误

File "kivy\_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
   File "kivy\_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy\_event.pyx", line 1138, in kivy._event.EventObservers._dispatch
   File "C:\Users\schup\PycharmProjects\Workspace\kivy_app_go\Surface.py", line 28, in <lambda>
     btnHvH = Button(text="Human vs Human", size_hint=(0.35, 0.15), on_release=lambda *args: self.HelperMethodsInst.switch_screen(goal_screen="go_screen", Screenmanager=WindowManager.get_ScreenManager, *args))
 TypeError: switch_screen() got multiple values for argument 'Screenmanager'

Surface.py:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy_app_go.Control import HelperMethods
from kivy.properties import ObjectProperty
from kivy.graphics import Rectangle, Color, Line
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.screenmanager import FadeTransition

from functools import partial


class MenuScreen(Screen):
    def __init__(self, **kwargs):
        super(Screen, self).__init__(**kwargs)
        self.ScreenSize = Window.size
        self.HelperMethodsInst = HelperMethods()

        lytmain = FloatLayout(size=self.ScreenSize)
        lytbutton = BoxLayout(pos_hint={"y": 0.1, "x": 0.15}, size_hint=(2, 0.6), orientation='vertical', size=self.ScreenSize)

        lblHeadline = Label(text="Choose your Go Mode", font_size=40, pos_hint={"y": 0.8, "x": 0.325},
                            size_hint=(0.35, 0.15))
        btnHvH = Button(text="Human vs Human", size_hint=(0.35, 0.15), on_release=lambda *args: self.HelperMethodsInst.switch_screen(goal_screen="go_screen", Screenmanager=WindowManager.get_ScreenManager, *args))
        btnHvB = Button(text="Human vs Bot", size_hint=(0.35, 0.15), on_release=lambda *args: self.HelperMethodsInst.switch_screen(goal_screen="go_screen", Screenmanager=WindowManager.get_ScreenManager, *args))
        btnBvB = Button(text="Bot vs Bot", size_hint=(0.35, 0.15), on_release=lambda *args: self.HelperMethodsInst.switch_screen(goal_screen="go_screen", Screenmanager=WindowManager.get_ScreenManager, *args))

        self.add_widget(lytmain)
        lytmain.add_widget(lblHeadline)
        lytmain.add_widget(lytbutton)
        lytbutton.add_widget(btnHvH)
        lytbutton.add_widget(btnHvB)
        lytbutton.add_widget(btnBvB)



class GoScreen(Screen):
    def __init__(self, **kwargs):
        super(GoScreen, self).__init__(**kwargs)
        self.ScreenSize = Window.size

        lytmain = FloatLayout(size=self.ScreenSize)

        btnBack = Button(text="Back", size_hint=(0.25, 0.1))

        self.add_widget(lytmain)
        lytmain.add_widget(btnBack)


class WindowManager(ScreenManager):
    def __init__(self):
        self.sm = ScreenManager(transition=FadeTransition(duration=0.15))
        self.sm.add_widget(MenuScreen(name="menu_screen"))
        self.sm.add_widget(GoScreen(name="go_screen"))

    @property
    def get_ScreenManager(self):
        return self.sm

class Surface(App):
    def __init__(self):
        super().__init__()
        self.WindowManagerInst = WindowManager()

    def build(self):
        return self.WindowManagerInst.sm

    @staticmethod
    def create_Surface():
        return Surface().run()


def run():
    SurfaceInst = Surface()
    HelperMethodsInst = HelperMethods()



Control.py:

from kivy.uix.screenmanager import ScreenManager, Screen


class HelperMethods:
    def switch_screen(self, Screenmanager, goal_screen):
        screenmanager = goal_screen

main.py:

from kivy_app_go.Surface import Surface

if __name__ == "__main__":
    Surface.create_Surface()

我是新奇人。
我希望有人可以提供帮助^^

1 个答案:

答案 0 :(得分:1)

您正在获得

参数“ Screenmanager”的多个值

错误,因为您的lambda正在构造一个带有关键字参数且后跟*args的函数。由于位置参数必须位于关键字参数之前,因此您的*args被视为Screenmanager=关键字的一部分。这样,该关键字就会以以下方式传递给您的switch_screen方法

Screenmanager = WindowManager.get_ScreenManager, *args

如果您需要将Button实例(即*args传递给您的switch_screen方法,则需要修改该方法以接受它(并调整lambda )。

只需从*args的末尾删除lambda,就可以避免错误。

但是,同一行代码中还有另一个问题。您正在使用WindowManager.get_ScreenManager,但是WindowManager是类,而不是实例,并且在类文件get_ScreenManager中是属性。因此,您需要一个WindowManager实例,您可以从WindowManagerInst的{​​{1}}属性中获得该实例。因此,该行现在变为:(以及调用以获取正在运行的App):

App