简体   繁体   English

Kivy:ScreenManager问题

[英]Kivy: ScreenManager Issues

I am struggling with Kivy's screen manager function. 我正在努力使用Kivy的屏幕管理器功能。 I want to set it up so that I can transition between screens, but I'm having trouble understanding the documentation. 我想对其进行设置,以便可以在不同的屏幕之间切换,但是我在理解文档时遇到了麻烦。 I think I may be misunderstanding because I am not very familiar with classes. 我认为我可能会误会,因为我对课程不是很熟悉。

Trying to run this code causes a crash. 尝试运行此代码会导致崩溃。 It gives the error message: 它给出错误信息:

   File "kivy\_event.pyx", line 254, in kivy._event.EventDispatcher.__init__ (kivy\_event.c:5332)
 TypeError: object.__init__() takes no parameters

I'd rather not mess around with the kivy language, please help me to understand how to make this work in python 3. 我宁愿不要随意使用kivy语言,请帮助我了解如何在python 3中进行这项工作。

"""
Python 3.6.4
Kivy 1.10.0

Combines various .py files and
allows the game to run.
"""

__author__ = "RidiculousName"
__date__ = "Jan. 2018"

import sys
from kivy.app import App
from kivy.config import Config
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen


class MainMenu(FloatLayout):
    def __init__(self, **kwargs):
        super(MainMenu, self).__init__(**kwargs)

        # declares widget buttons
        background = Image(source="Img\\BG.png",
                           pos=(0, 0))

        start_button = Button(pos=(25, 75),
                              background_normal="Img\\Start.png",
                              background_down="Img\\Start_Down.png",
                              size_hint=(.1, .1))

        load_button = Button(pos=(225, 75),
                             background_normal="Img\\Load.png",
                             background_down="Img\\Load_Down.png",
                             size_hint=(.1, .1))

        options_button = Button(pos=(425, 75),
                                background_normal="Img\\Options.png",
                                background_down="Img\\Options_Down.png",
                                size_hint=(.15, .1))

        quit_button = Button(pos=(708, 75),
                             background_normal="Img\\Quit.png",
                             background_down="Img\\Quit_Down.png",
                             size_hint=(.1, .1))
        quit_button.bind(on_press=sys.exit)

        # Adds widgets in order
        self.add_widget(background)
        self.add_widget(start_button)
        self.add_widget(load_button)
        self.add_widget(options_button)
        self.add_widget(quit_button)



class BanditKing(App, ScreenManager):
    def build(self, **kwargs):
        super(BanditKing, self).__init__(**kwargs)

        sm = ScreenManager
        sm.add_widget(ScreenManager(name="MainMenu"))
        self.title = "Bandit King"
        self.icon = "Window_Icon.png"
        return sm



def main():
    Config.set("graphics", "width", "1600")
    Config.set("graphics", "height", "900")
    Config.write()
    BanditKing().run()


if __name__ == "__main__":
    main()

The widgets that you add to a ScreenManager should be type Screen . 您添加到ScreenManager的小部件应为Screen So MainMenu should extend Screen instead of FloatLayout . 因此MainMenu应该扩展Screen而不是FloatLayout 'Screen' is a RelativeLayout , so it shouldn't affect your code much. “屏幕”是一个RelativeLayout ,因此它不会对您的代码产生太大影响。

And your BanditKing class build() should create a ScreenManager and return it, rather than extend it: 并且您的BanditKingbuild()应该创建一个ScreenManager并返回它,而不是对其进行扩展:

class BanditKing(App):
    def build(self, **kwargs):
        super(BanditKing, self).__init__(**kwargs)
        sm = ScreenManager()
        sm.add_widget(MainMenu(name="MainMenu"))
        self.title = "Bandit King"
        self.icon = "Window_Icon.png"
        return sm

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM