python-3.x Kivy小部件层次结构未按预期运行

rt4zxlrg  于 2023-03-24  发布在  Python
关注(0)|答案(1)|浏览(106)

我正在努力学习Kivy,但似乎无法理解为什么我的代码(下面)不能正常工作。在我看来,第一个屏幕上的所有三个按钮都应该将您移动到第二个屏幕,但只有一个按钮可以!我认为,因为我指的是Widget树底部的窗口管理器(app.root),引用应该是绝对的和“位置无关的”。显然有一些我不明白的地方...

WindowManager:
        CourseGridLayout:
        CourseProgressWindow:
    
    <CourseGridLayout>:
        name: "courselist"
        id: courselist
        BoxLayout:
            orientation: "vertical"
            size: root.width, root.height
    
            Image:
                source: "logo.png"
    
            GridLayout:
                name: "bottomGrid"
                cols: 2
    
                Button:
                    text: "placeholder"
                    font_size: 32
                    on_press:
                        app.root.current: "courseprogress"
                        root.manager.transition.direction: "left"
    
                Button:
                    text: "placeholder2"
                    font_size: 32
                    on_press:
                        app.root.current: "courseprogress"
                        root.manager.transition.direction: "left"
    
            Button:
                text: "Next Screen"
                font_size: 32
                on_press:
                    app.root.current = "courseprogress"
                    root.manager.transition.direction = "left"
    
    <CourseProgressWindow>:
        name: "courseprogress"
        id: courseprogress
        BoxLayout:
            orientation: "vertical"
            size: root.width, root.height
    
            Label:
                id: progressoutput
            Button:
                text: "Go Back"
                font_size: 32
                on_press:
                    app.root.current = "courselist"
                    root.manager.transition.direction = "right"
#!/usr/bin/env python3
__author__ = "Arana Fireheart"

from kivy.app import App
from kivy. uix.widget import Widget
from kivy.lang import Builder
from kivy. uix.screenmanager import ScreenManager, Screen

#Define our different screens
class CourseGridLayout(Screen):
    pass

class CourseProgressWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

# Designate Our .kv design file
kivvy = Builder.load_file("CGMonitor2.kv")

class DemoApp(App):
    def build(self):
        return kivvy

if __name__ == "__main__":
    DemoApp().run()
xqk2d5yq

xqk2d5yq1#

在两个不工作的Buttons中,您用途:

app.root.current: "courseprogress"
                    root.manager.transition.direction: "left"

尝试将其更改为:

app.root.current = "courseprogress"
                    root.manager.transition.direction = "left"

on_press:后面的命令必须是python命令。

相关问题