尝试更改屏幕时出现Kivy错误 [英] Kivy error while trying to change the screen

查看:86
本文介绍了尝试更改屏幕时出现Kivy错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想使用.kv语言.因此,当我按下按钮尝试运行功能时,会发生我的问题.
我发现这些问题具有相同的问题,因此我从他们那里采用了on_press方法的代码:
kivy python通过单击按钮将参数传递给功能
Python,Kivy,"AssertionError :没有一个是不可调用的",通过按钮调用函数时出错

I didn't want to use the .kv language. So my problem occurs when trying to run a function when I press a button.
I found these question which had the same problem so I adopted the code, for the on_press method, from them:
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()

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

I am new to kivy.
I hope someone can help ^^

推荐答案

您正在获得

参数"Screenmanager"的多个值

multiple values for argument 'Screenmanager'

错误,因为您的lambda正在构造带有关键字参数且后跟*args的函数.由于位置参数必须在关键字参数之前,因此您的*args被视为Screenmanager=关键字的一部分.因此,该关键字将作为

error because your lambda is constructing a function with keyword arguments followed by *args. Since positional arguments must come before keyword arguments, your *args is treated as part of the Screenmanager= keyword. So that keyword is passed to your switch_screen method as

Screenmanager = WindowManager.get_ScreenManager, *args

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

If you need the Button instance (which is what the *args is) passed to your switch_screen method, then you need to modify that method to accept it (and adjust the lambda).

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

You can avoid the error simply by removing the *args from the end of the lambda.

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

However, there is another problem in that same line of code. You are using WindowManager.get_ScreenManager, but WindowManager is a class, not an instance, and in the class file get_ScreenManager is a property. So you need the WindowManager instance, which you can get from the WindowManagerInst attribute of the App. So that line now become: (along with a call to get the running App):

app = App.get_running_app()
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=app.WindowManagerInst.get_ScreenManager))

这篇关于尝试更改屏幕时出现Kivy错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆