【问题标题】:Linking a Kivy Button to Function on Builder将 Kivy 按钮链接到 Builder 上的功能
【发布时间】:2021-09-05 15:38:10
【问题描述】:

我想给一个函数附加屏幕上的按钮,但是每次下面都会显示错误

AttributeError: 'MDFlatButton' 对象没有属性 'plus_drive'

这是一个包含字符串页面的帮助文件

screen_helper = """
    Screen:
        canvas:
            
            RoundedRectangle:
                pos: self.pos
                size: self.size
                radius: [25, 0, 0, 0]
        ScreenManager:
            clearcolor:[0, 1, 0, 1]
            MenuScreen:
            ransomwareScreen:
            keyScreen:
        
        
    <MenuScreen>:
        name:'menu'
    
        MDBoxLayout:
            orientation: "vertical"
    
            MDToolbar:
                title: "Title"
               
    
            MDBoxLayout:
    
                MDNavigationRail:
                    id: rail
                    use_resizeable: True
    
                    MDNavigationRailItem:
                        icon: "comment-account"
                        text: "profile"
                        
    
                    MDNavigationRailItem:
                        icon: "security"
                        text: "security"
                        on_touch_up : root.manager.current = 'ransomware'
    
    
                    MDNavigationRailItem:
                        icon: "folder-key"
                        text: "folder-key"
                        on_press : root.manager.current = 'key_file'    
    
    
    <ransomwareScreen>:
        name: 'ransomware'
        
        MDList:   
            OneLineListItem:
                text : "item"
        MDFlatButton:
            text : "salam"
            pos_hint: {"center_x": .9, "center_y": .1}
            on_release : self.plus_drive        
    
        MDBoxLayout:
            orientation: "vertical"
    
            MDToolbar:
                title: "demo"
    
            MDBoxLayout:
    
                MDNavigationRail:
                    id: rail
                    use_resizeable: True
    
                    MDNavigationRailItem:
                        icon: "comment-account"
                        text: "profile"
                        on_release : root.manager.current = 'menu'
    
                    MDNavigationRailItem:
                        icon: "security"
                        text: "security"
                        on_release : root.manager.current = 'ransomware'
                        
    
    
                    MDNavigationRailItem:
                        icon: "folder-key"
                        text: "folder-key"
                        on_release : root.manager.current = 'key_file'  
    
    
    
    <keyScreen>:
        name: 'key_file'
        OneLineListItem:
            id : "drive"
    
        MDBoxLayout:
            orientation: "vertical"
    
            MDToolbar:
                title: "demo"
    
            MDBoxLayout:
    
                MDNavigationRail:
                    id: rail
                    use_resizeable: True
    
                    MDNavigationRailItem:
                        icon: "comment-account"
                        text: "profile"
                        on_release : root.manager.current = 'menu'
    
                    MDNavigationRailItem:
                        icon: "security"
                        text: "security"
                        on_release : root.manager.current = 'ransomware'
    
    
                    MDNavigationRailItem:
                        icon: "folder-key"
                        text: "folder-key"
                        on_release : root.manager.current = 'key_file'   
    
    """

和我运行应用程序的代码:

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from helper import *

class MenuScreen(Screen):
    pass

class ransomwareScreen(Screen):

    def plus_drive(self , obj):
        print("salam")
        return

class keyScreen(Screen):
    pass

sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(ransomwareScreen(name='ransomware'))
sm.add_widget(keyScreen(name='key_file'))

class DemoApp(MDApp):
    def build(self):
        screen = Builder.load_string(screen_helper)
        return screen


DemoApp().run()

【问题讨论】:

  • 您能告诉我们您是如何添加 MD 按钮的吗?我的意思是您添加该按钮的功能是什么,或者您是否已经在 kivy lang 部分添加了该按钮?或者您是否尝试动态添加该按钮并为该按钮附加一个功能?
  • 尝试使用app.plus_drive()

标签: python kivymd


【解决方案1】:
MDFlatButton:
    text : "salam" 
    pos_hint: {"center_x": .9, "center_y": .1}
    on_release : app.plus_drive()

因为您正在调用应用程序中的函数

【讨论】:

    猜你喜欢
    • 2014-06-01
    • 2017-01-10
    • 2017-07-07
    • 2017-07-04
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    相关资源
    最近更新 更多