【问题标题】:Kivymd - changing screen from bottom toolbar is not workingKivymd - 从底部工具栏更改屏幕不起作用
【发布时间】:2020-12-27 16:35:36
【问题描述】:

当用户按下底部的图标时,我正在尝试设置一个逻辑来更改屏幕。我可以使用“测试”按钮更改屏幕,但底部工具栏图标有困难。下面是我的代码。当我按下第二个图标时,它会打印“Hello”,这意味着代码似乎可以工作,但我觉得我缺少连接到不同屏幕的 kivy 功能​​。 请让我知道我做错了什么。

from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.lang import Builder
from kivymd.uix.floatlayout import FloatLayout
from kivymd.uix.textfield import MDTextField

# Builder String
allLayout  = '''
ScreenManager:
    MainPage:
    #List:
    #History:
    StudentName:
    #Support:
    #FAQ:
    #TermsAndConditions:
    #MyAccount:
    
<Button@MdButton>: 
    font_size: 20
    background_color: 144/235,252/255,144/255,1
    size_hint: 0.32, 0.07

<Ticker@MDTextField>:
    font_size: 20
    hint_text: "Enter student Name"
    icon_right: 'database-search'
    helper_text: "From the list (2nd icon in bottom bar)"
    helper_text_mode: 'on_focus'
    size_hint_x : None
    width:200
<MainPage>:
    name: 'mainpage1'
    NavigationLayout:
        ScreenManager:
            Screen:
                BoxLayout:
                    orientation:'vertical'
                    MDToolbar:
                        title: 'Main Page'
                        elevation: 10
                        right_action_items: [["dots-vertical", lambda x: x]]
                           # on_action_button: app.callback(self.icon)
                    Widget: #if you delete this line... you will spend hours figuring out like before
                    MDBottomAppBar:
                        MDToolbar:
                            icon: "history"
                            text_color: 0,0,0,0
                            icon_size: 0.5                            
                            type: "bottom"
                            mode: "end"
                            left_action_items: [["home", lambda x: x], ["view-list", lambda x: app.listScreen()],  ["watch", lambda x: x], ["help", lambda x: x]]
                           

    Button:
        text:"Class 1"
        pos_hint: {'x': 0.025, 'y':.8}
    Button:
        text:"Class 2"
        pos_hint: {'x': 0.345, 'y':.8}
    Button:
        text:"Class 3"
        pos_hint: {'x': 0.665, 'y':.8}
    Ticker:
        pos_hint: {'x':0.025, 'y': .7}
    Button:
        text:"Test"
        pos_hint: {'x': 0.665, 'y':.6}
        on_release:
            root.manager.current = 'student'
    
<StudentName>:
    name: 'student'
    MDLabel:
        text: "Hi"
'''''

class MainPage(Screen):
    pass
class StudentName(Screen):
    pass

sm = ScreenManager()
sm.add_widget(MainPage(name = 'mainpage1'))
sm.add_widget(StudentName(name = 'student'))

class DemoApp(MDApp):
    def listScreen(self):
        
        self.current = StudentName()
        print("Hello")
     #   root.manager.transition.direction = 'left'
       
    def build(self):
        self.theme_cls.primary_palette = 'Lime'
        self.theme_cls.primary_hue = 'A700'
        screen = Screen()
        main_code = Builder.load_string(allLayout)
        screen.add_widget(main_code)
       return screen
DemoApp().run()
        

【问题讨论】:

    标签: python-3.x kivy kivy-language kivymd


    【解决方案1】:

    您的代码中发生了一些奇怪的事情。一是代码:

    sm = ScreenManager()
    sm.add_widget(MainPage(name = 'mainpage1'))
    sm.add_widget(StudentName(name = 'student'))
    

    它正在创建一个ScreenManager 并将Screens 添加到它。但是后来创建的ScreenManagersm)并没有用到这些行可以去掉。

    另一个是build()方法代码:

        screen = Screen()
        main_code = Builder.load_string(allLayout)
        screen.add_widget(main_code)
        return screen
    

    此代码使用Builder.load_string() 构建整个显示。该显示以ScreenManager 作为其根(在字符串allLayout 中定义)。然后将 ScreenManager 添加到 Screen (screen.add_widget(main_code)) 并返回 Screen 作为 App 根窗口。我怀疑这不是你想要的,但它完全是合法的代码。我怀疑build() 方法实际上应该是:

    def build(self):
        self.theme_cls.primary_palette = 'Lime'
        self.theme_cls.primary_hue = 'A700'
        main_code = Builder.load_string(allLayout)
        return main_code
    

    以上代码使ScreenManager成为App显示的根。

    最后一个问题是listScreen() 方法。行:

    self.current = StudentName()
    

    正在创建Student 的新实例并将其分配给DemoApp 类中名为current 的变量。我怀疑您实际上想像这样更改Screens

    def listScreen(self):
        self.root.current = 'student'
        print("Hello")
    

    此代码将应用根目录(即ScreenManager)的current 属性设置为StudentName 类的现有实例。

    【讨论】:

    • 谢谢!我已经按照您的建议更新了代码并且它有效。另外,感谢您花时间检查代码并解释出了什么问题以及我应该删除哪些行。我对 Kivy 和 Python 很陌生。非常感谢您花时间解释它,现在它更有意义了。
    猜你喜欢
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多