简体   繁体   English

使用.kv文件从kivy BoxLayout到ScreenManager

[英]from kivy BoxLayout to ScreenManager using .kv file

I have an application with 1 screen in BoxLayout (filename qmscreens.py). 我在BoxLayout中有一个带有1个屏幕的应用程序(文件名qmscreens.py)。

from kivy.app import App
from kivy.core.window import Window

class qmHome(BoxLayout):
    pass  

class qmscreensApp(App):
    def build(self):
        Window.clearcolor = (1,1,1,1)
        Window.size = (500, 500)
        homeWin = qmHome()
        return homeWin

qmscreensApp().run()

The examples below have the changes implemented as suggested. 下面的示例已按照建议实施更改。

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen

class EditImage(Screen):
    pass

class QmHome(Screen):
    pass

class QManager(ScreenManager):
    elogging = BooleanProperty()

    elogging = True
    # and a lot of other python code here below


class qmscreensApp(App):
    def build(self):
        Window.clearcolor = (1,1,1,1)
        Window.size = (500, 500)
        homeWin = QManager()
        return homeWin

qmscreensApp().run()

and the following qmscreens.kv file: 和以下qmscreens.kv文件:

QManager:
    QmHome:
    EditImage:

<QmHome>:
    name: 'home'
    Button:
        on_press: app.root.current = 'edit'
        text: 'to edit screen'
        font_size: 50

<EditImage>:
    name: 'edit'
    Button:
        on_release: app.root.current = 'home'
        text: 'back to the home screen'
        font_size: 50

    CheckBox:
        id: _evlogg_cb
        active: root.elogging

The example above gives the error message: 上面的示例给出了错误消息:

AttributeError: 'QmHome' object has no attribute 'elogging' AttributeError:'QmHome'对象没有属性'elogging'

I guess the code line elogging = BooleanProperty() should be changed from the QManager to the QmHome class. 我猜代码行elogging = BooleanProperty()应该从QManager更改为QmHome类。 But then I get other errors. 但是后来我得到其他错误。 So I am completely lost. 所以我完全迷路了。 Your help and directions are highly appreciated. 非常感谢您的帮助和指导。 Thanks in advance. 提前致谢。

Few fixes. 很少修复。 QManager should be the root widget and you missed a <> in the kv file QManager应该是根窗口小部件,并且您错过了kv文件中的<>

def build(self):
    Window.clearcolor = (1,1,1,1)
    Window.size = (500, 500)
    #homeWin = qmHome()
    return QManager() # <---- :)

in the kv file: 在kv文件中:

<QManager>:
    qmHome:
    EditImage:

Rename all occurrences of the class qmHome to QmHome . 将所有出现的类qmHome重命名为QmHome By convention across many different languages, beginning of a class should always start with a capital letter. 按照许多不同语言的约定,课程开始时应始终以大写字母开头。

Kivy not recognizing the screen named with lowercase seems like a bug. Kivy无法识别以小写字母命名的屏幕似乎是一个错误。

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

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