Python 在Kivy中,如何通过该功能实现到新屏幕的转换?

Python 在Kivy中,如何通过该功能实现到新屏幕的转换?,python,python-3.x,kivy,kivy-language,Python,Python 3.x,Kivy,Kivy Language,我想在点击按钮后实现到新屏幕的转换,我想通过函数来实现。 我有这个密码 <ScreenFrame@BoxLayout>: orientation: 'vertical' cols: 3 ActionBar: ActionView: pos_hint: {'top':1} use_separator: True ActionPrevious:

我想在点击按钮后实现到新屏幕的转换,我想通过函数来实现。 我有这个密码

<ScreenFrame@BoxLayout>:
    orientation: 'vertical'
    cols: 3

    ActionBar:
        ActionView:
            pos_hint: {'top':1}
            use_separator: True
            ActionPrevious:
                title: "Title"
                with_previous: False
            ActionButton:
                text: "+"


    Label:
        text: "Content"


    BoxLayout:
        orientation: 'horizontal'
        cols: 4
        size_hint: 1, 0.15

        Button:
            text: "Notes"
            #don't work
            on_press: root.manager.current = 'nts'
        Button:
            text: "Docs"
            #don't work
            on_release: root.go_to_dcs()

        Button:
            text: "Videos"
            #don't work
            on_release: root.manager.current = "vds"
        Button:
            text: "Pictures"
            # don't work
            on_release: root.manager.current = 'pctrs'

<NtsScreen>:
    BoxLayout:
        orientation: "vertical"
        cols: 2

        ScreenFrame:
        Button:
            text: "f"
            #work, but me need implementaion through function
            on_press: root.manager.current = "vds"

<DocsScreen>:
    ScreenFrame:

<VdsScreen>:
    ScreenFrame:

<PctrsScreen>:
    ScreenFrame:
,但当我运行并按下Docs按钮时,出现了此错误

 Traceback (most recent call last):
   File "main.py", line 45, in <module>
     MainApp().run()
   File "/home/parvektor228/TAOKF/lib/python3.6/site-packages/kivy/app.py", line 826, in run
     runTouchApp()
   File "/home/parvektor228/TAOKF/lib/python3.6/site-packages/kivy/base.py", line 502, in runTouchApp
     EventLoop.window.mainloop()
   File "/home/parvektor228/TAOKF/lib/python3.6/site-packages/kivy/core/window/window_pygame.py", line 403, in mainloop
     self._mainloop()
   File "/home/parvektor228/TAOKF/lib/python3.6/site-packages/kivy/core/window/window_pygame.py", line 289, in _mainloop
     EventLoop.idle()
   File "/home/parvektor228/TAOKF/lib/python3.6/site-packages/kivy/base.py", line 340, in idle
     self.dispatch_input()
   File "/home/parvektor228/TAOKF/lib/python3.6/site-packages/kivy/base.py", line 325, in dispatch_input
     post_dispatch_input(*pop(0))
   File "/home/parvektor228/TAOKF/lib/python3.6/site-packages/kivy/base.py", line 291, in post_dispatch_input
     wid.dispatch('on_touch_up', me)
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "/home/parvektor228/TAOKF/lib/python3.6/site-packages/kivy/uix/behaviors/button.py", line 179, in on_touch_up
     self.dispatch('on_release')
   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 1098, in kivy._event.EventObservers._dispatch
   File "/home/parvektor228/TAOKF/lib/python3.6/site-packages/kivy/lang/builder.py", line 64, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "/home/parvektor228/TAOKF/KivyApp/memfier/main.kv", line 32, in <module>
     on_release: root.go_to_dcs()
   File "kivy/weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.__getattr__
 AttributeError: 'ScreenFrame' object has no attribute 'go_to_dcs'
回溯(最近一次呼叫最后一次):
文件“main.py”,第45行,在
MainApp().run()
文件“/home/parvektor228/TAOKF/lib/python3.6/site packages/kivy/app.py”,第826行,运行中
runTouchApp()
runTouchApp中的文件“/home/parvektor228/TAOKF/lib/python3.6/site packages/kivy/base.py”,第502行
EventLoop.window.mainloop()
文件“/home/parvektor228/TAOKF/lib/python3.6/site packages/kivy/core/window/window_pygame.py”,第403行,在主循环中
self._mainloop()
文件“/home/parvektor228/TAOKF/lib/python3.6/site packages/kivy/core/window/window\u pygame.py”,第289行,在主循环中
EventLoop.idle()
文件“/home/parvektor228/TAOKF/lib/python3.6/site packages/kivy/base.py”,第340行,处于空闲状态
self.dispatch_input()
文件“/home/parvektor228/TAOKF/lib/python3.6/site packages/kivy/base.py”,第325行,在调度输入中
发送后输入(*pop(0))
文件“/home/parvektor228/TAOKF/lib/python3.6/site packages/kivy/base.py”,第291行,发布后输入
wid.dispatch('我在润色时)
文件“kivy/_event.pyx”,第707行,在kivy.\u event.EventDispatcher.dispatch中
文件“/home/parvektor228/TAOKF/lib/python3.6/site-packages/kivy/uix/behavies/button.py”,第179行,在线润色
自我分派(“发布时”)
文件“kivy/_event.pyx”,第703行,在kivy.\u event.EventDispatcher.dispatch中
文件“kivy/_event.pyx”,第1214行,在kivy.\u event.eventobservators.dispatch中
文件“kivy/_event.pyx”,第1098行,在kivy.\u event.eventobserver.\u调度
文件“/home/parvektor228/TAOKF/lib/python3.6/site packages/kivy/lang/builder.py”,第64行,在自定义回调中
exec(\uuuukVLANG\uuuuuuu.co\u值,idmap)
文件“/home/parvektor228/TAOKF/KivyApp/memfier/main.kv”,第32行,in
发布时:root.go\u到\u dcs()
文件“kivy/weakproxy.pyx”,第30行,位于kivy.weakproxy.weakproxy.\uu getattr__
AttributeError:“ScreenFrame”对象没有“go\u to\u dcs”属性
哪里有错误,或者我能理解一些我不理解的东西吗

还有一些细节,以便bug通知消失。 还有一些细节,以便bug通知消失。
请提供更多详细信息,以使错误通知消失。

您的问题之所以出现,是因为
屏幕框
框布局
(根据您的kv文件),而不是
屏幕
。因此,您调用的
根目录
(在本例中为
屏幕框
)没有与其关联的
管理器
管理器
屏幕
本身相关联

您必须引用
屏幕框
小部件的父小部件,才能引用当前的
屏幕
,它与
管理器
关联。为了清晰起见,我建议按照如下方式重新构造Python代码和kv文件:

Python代码:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.widget import Widget

Builder.load_file("testthis.kv")

class ScreenFrame(BoxLayout):

    def go_to_screen(self, val):
        self.parent.manager.current = val

class NtsScreen(Screen):
    pass

class DcsScreen(Screen):
    pass

class VdsScreen(Screen):
    pass

class PctrsScreen(Screen):
    pass

class MyScreenManager(ScreenManager):
    pass

class MainApp(App):

    def build(self):
        self.sm = MyScreenManager()
        return self.sm

if __name__ == '__main__':
    MainApp().run()
kv文件:

<MyScreenManager>:
    NtsScreen:
        name: 'nts'
    DcsScreen:
        name: 'dcs'
    VdsScreen:
        name: 'vds'
    PctrsScreen:
        name: 'pctrs'

<NtsScreen>:
    ScreenFrame:

<DocsScreen>:
    ScreenFrame:

<VdsScreen>:
    ScreenFrame:

<PctrsScreen>:
    ScreenFrame:

<ScreenFrame>:

    orientation: 'vertical'
    rows: 3

    ActionBar:
        ActionView:
            pos_hint: {'top':1}
            use_separator: True
            ActionPrevious:
                title: "Title"
                with_previous: False
            ActionButton:
                text: "+"

    Label:
        text: "Content"

    BoxLayout:
        orientation: 'horizontal'
        cols: 4
        size_hint: 1, 0.15

        Button:
            text: "Notes"
            on_press: root.go_to_screen('nts')
        Button:
            text: "Docs"
            on_release: root.go_to_screen('dcs')
        Button:
            text: "Videos"
            on_release: root.go_to_screen('vds')
        Button:
            text: "Pictures"
            on_release: root.go_to_screen('pctrs')
:
NtsScreen:
名称:'nts'
DcsScreen:
名称:“dcs”
VdsScreen:
名称:“vds”
PctrsScreen:
名称:'pctrs'
:
屏幕框:
:
屏幕框:
:
屏幕框:
:
屏幕框:
:
方向:“垂直”
行数:3
操作栏:
ActionView:
位置提示:{'top':1}
使用分隔符:True
以前的行动:
标题:“标题”
与_previous一起:False
操作按钮:
正文:“+”
标签:
正文:“内容”
盒子布局:
方向:“水平”
科尔斯:4
尺寸提示:1,0.15
按钮:
正文:“注”
按:root.转到屏幕('nts')
按钮:
文本:“文件”
发布时:根目录。转到屏幕(“dcs”)
按钮:
文字:“视频”
发布时:根目录。转到屏幕(“vds”)
按钮:
文字:“图片”
发布时:root.转到屏幕('pctrs')
问题-属性错误 有两个属性错误,如下所示:

AttributeError:go_to_dcs()方法 在kv文件中,定义了一个,
,该文件没有实现
go\u to\u dcs()
方法

属性错误:经理 动态类
具有BoxLayout继承,但不具有Screen继承。因此,它没有名为manager的属性

解决方案 kv文件
  • 将动态类
    替换为类规则
  • 在类规则中,
    -将
    root.manager.current
    替换为
    app.root.current
  • 一小条 主电压(千伏)
    #:kivy 1.11.0
    :
    方向:“垂直”
    科尔斯:3
    操作栏:
    ActionView:
    位置提示:{'top':1}
    使用分隔符:True
    以前的行动:
    标题:“标题”
    与_previous一起:False
    操作按钮:
    正文:“+”
    标签:
    正文:“内容”
    盒子布局:
    方向:“水平”
    科尔斯:4
    尺寸提示:1,0.15
    按钮:
    正文:“注”
    按:app.root.current='nts'
    按钮:
    文本:“文件”
    发布时:root.go\u到\u dcs()
    按钮:
    文字:“视频”
    按:app.root.current='vds'
    按钮:
    文字:“图片”
    按:app.root.current='pctrs'
    :
    盒子布局:
    方向:“垂直”
    科尔斯:2
    屏幕框:
    按钮:
    案文:“f”
    #工作,但我需要通过功能实现
    按:root.manager.current=“vds”
    :
    屏幕框:
    :
    屏幕框:
    :
    屏幕框:
    
    输出

    您好,您知道为什么按“文档”按钮时窗口是黑色的吗?屏幕n的黑色屏幕
    <MyScreenManager>:
        NtsScreen:
            name: 'nts'
        DcsScreen:
            name: 'dcs'
        VdsScreen:
            name: 'vds'
        PctrsScreen:
            name: 'pctrs'
    
    <NtsScreen>:
        ScreenFrame:
    
    <DocsScreen>:
        ScreenFrame:
    
    <VdsScreen>:
        ScreenFrame:
    
    <PctrsScreen>:
        ScreenFrame:
    
    <ScreenFrame>:
    
        orientation: 'vertical'
        rows: 3
    
        ActionBar:
            ActionView:
                pos_hint: {'top':1}
                use_separator: True
                ActionPrevious:
                    title: "Title"
                    with_previous: False
                ActionButton:
                    text: "+"
    
        Label:
            text: "Content"
    
        BoxLayout:
            orientation: 'horizontal'
            cols: 4
            size_hint: 1, 0.15
    
            Button:
                text: "Notes"
                on_press: root.go_to_screen('nts')
            Button:
                text: "Docs"
                on_release: root.go_to_screen('dcs')
            Button:
                text: "Videos"
                on_release: root.go_to_screen('vds')
            Button:
                text: "Pictures"
                on_release: root.go_to_screen('pctrs')
    
         on_release: root.go_to_dcs()
       File "kivy/weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.__getattr__
     AttributeError: 'ScreenFrame' object has no attribute 'go_to_dcs'
    
         on_press: root.manager.current = 'nts'
       File "kivy/weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.__getattr__
     AttributeError: 'ScreenFrame' object has no attribute 'manager'
    
    <ScreenFrame>:
    
            Button:
                text: "Notes"
                on_press: app.root.current = 'nts'
            Button:
                text: "Docs"
                on_release: root.go_to_dcs()    
            Button:
                text: "Videos"
                on_press: app.root.current = 'vds'
            Button:
                text: "Pictures"
                on_press: app.root.current = 'pctrs'
    
    from kivy.app import App
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.uix.boxlayout import BoxLayout
    from kivy.lang import Builder
    
    
    Builder.load_file("main.kv")
    
    
    class ScreenFrame(BoxLayout):
        def go_to_dcs(self):
            print("\nScreenFrame.go_to_dcs")
            sm.current = "dcs"
    
    
    class NtsScreen(Screen):
        pass
    
    
    class DcsScreen(Screen):
        def go_to_dcs(self):
            sm.current = 'dcs'
    
    
    class VdsScreen(Screen):
        pass
    
    
    class PctrsScreen(Screen):
        pass
    
    
    # Create the screen manager
    sm = ScreenManager()
    sm.add_widget(NtsScreen(name='nts'))
    sm.add_widget(DcsScreen(name='dcs'))
    sm.add_widget(VdsScreen(name='vds'))
    sm.add_widget(PctrsScreen(name='pctrs'))
    
    
    class MainApp(App):
        def build(self):
            return sm
    
    
    if __name__ == "__main__":
        MainApp().run()
    
    #:kivy 1.11.0
    
    <ScreenFrame>:
        orientation: 'vertical'
        cols: 3
    
        ActionBar:
            ActionView:
                pos_hint: {'top':1}
                use_separator: True
                ActionPrevious:
                    title: "Title"
                    with_previous: False
                ActionButton:
                    text: "+"
    
    
        Label:
            text: "Content"
    
    
        BoxLayout:
            orientation: 'horizontal'
            cols: 4
            size_hint: 1, 0.15
    
            Button:
                text: "Notes"
                on_press: app.root.current = 'nts'
    
            Button:
                text: "Docs"
                on_release: root.go_to_dcs()
    
            Button:
                text: "Videos"
                on_press: app.root.current = 'vds'
    
            Button:
                text: "Pictures"
                on_press: app.root.current = 'pctrs'
    
    <NtsScreen>:
        BoxLayout:
            orientation: "vertical"
            cols: 2
    
            ScreenFrame:
            Button:
                text: "f"
                #work, but me need implementaion through function
                on_press: root.manager.current = "vds"
    
    <DcsScreen>:
        ScreenFrame:
    
    <VdsScreen>:
        ScreenFrame:
    
    <PctrsScreen>:
        ScreenFrame: