首页 > 解决方案 > 如何使用 kivymd 制作按钮以打开超链接

问题描述

        from kivy.lang import Builder
        from kivymd.app import MDApp
        from kivymd.uix.button import MDRoundFlatIconButton
        from kivymd.uix.dialog import MDDialog
        import webbrowser
    

#KVMD 字符串

        root_kv = """
        BoxLayout:
            orientation: "vertical"
        
            MDToolbar:
                id: toolbar
                theme_text_color: "Primary"
                right_action_items: [["account-circle-outline", lambda x: x, "Accounts"]]
                elevation:10
                pos_hint:{'top':1}
        
            MDBottomNavigation:
                id: panel
            
            
        
                MDBottomNavigationItem:
                    name: "files0"
                    text: "Home"
                    icon: "home"
        
                    BoxLayout:
                        orientation: "vertical"
                        size: self.size
                        pos: self.pos
        
                        MDLabel:
                            font_style: "Body1"
                            theme_text_color: "Primary"
                            text: "Home"
                            halign:"center"
        
                MDBottomNavigationItem:
                    name: "files1"
                    text: "Timetable"
                    icon: "table-large"
        
                    BoxLayout:
                        orientation: "vertical"               
                        spacing: dp(10)
                        pos: self.pos
        
                        MDLabel:
                            font_style: "Body1"
                            theme_text_color: "Primary"
                            text: "Timetable"
                            valign: "top"
                            halign: "center"
            
                                            
        
                MDBottomNavigationItem:
                    name: "files2"
                    text: "Links"
                    icon: "link-variant"
                       
                    MDLabel:
                        font_style: "Body1"
                        theme_text_color: "Primary"
                        text: "Links"
                        pos_hint:{'center_x': .95, 'center_y': .9}
            
                

#纽扣

                        MDFillRoundFlatIconButton:
                            icon: "flask-round-bottom"
                            text: "CHEMISTRY"
                            theme_text_color: "Custom"
                            text_color: 0, 0, 0, 1
                            line_color: 227/255, 91/255, 100/255,1
                            icon_color: 1, 1, 1, 1
                            pos_hint:{'center_x': .4 ,'center_y': .8}
                            on_release:  SCHOOL("Chem")
            
                        MDFillRoundFlatIconButton:
                            icon: "magnet"
                            text: "PHYSICS"
                            theme_text_color: "Custom"
                            text_color: 0, 0, 0, 1
                            line_color: 227/255, 91/255, 100/255,1
                            icon_color: 1, 1, 1, 1
                            pos_hint:{'center_x': .6, 'center_y':.8}
                            on_release:  SCHOOL(Phy)
                            
                        MDFillRoundFlatIconButton:
                            icon: "calculator-variant"
                            text: "MATHEMATICS"
                            theme_text_color: "Custom"
                            text_color: 0, 0, 0, 1
                            line_color: 227/255, 91/255, 100/255,1
                            icon_color: 1, 1, 1, 1
                            pos_hint:{'center_x': .6 ,'center_y': .7}
                            on_release: SCHOOL(Math)
                            
                        MDFillRoundFlatIconButton:
                            icon: "alphabetical-variant"
                            text: "ENGLISH"
                            theme_text_color: "Custom"
                            text_color: 0, 0, 0, 1
                            line_color: 227/255, 91/255, 100/255,1
                            icon_color: 1, 1, 1, 1
                            pos_hint:{'center_x': .4 ,'center_y': .7}
                            on_release: SCHOOL(Eng)
            
                        MDFillRoundFlatIconButton:
                            icon: "laptop-chromebook"
                            text: "COMPUTER"
                            theme_text_color: "Custom"
                            text_color: 0, 0, 0, 1
                            line_color: 227/255, 91/255, 100/255,1
                            icon_color: 1, 1, 1, 1
                            pos_hint:{'center_x': .5,'center_y': .6}
                            on_release: SCHOOL(CS)
                            
            
                    MDBottomNavigationItem:
                        name: "files3"
                        text: "SCHEDULER"
                        icon: "calendar-clock"
            
                        MDLabel:
                            font_style: "Body1"
                            theme_text_color: "Primary"
                            text: "Scheduler"
                            halign:"center"
                            
            
            """
    
   

#链接的来源字典

School={"Phy":"Link1",
                    "Chem":"Link2",
                    "Math":"Link3",
                    "CS":"Link4",
                    "Eng":"Link5"}
        
        
        class MainApp(MDApp):
            def __init__(self, **kwargs):
                self.title = "KivyMD Examples - Bottom Navigation"
                super().__init__(**kwargs)
    

#根据作为参数引入的主题打开链接的函数

def SCHOOL(Subject):
                    webbrowser.open_new_tab(School[Subject])
        
            def build(self):
                self.theme_cls.primary_palette = "Indigo"
                self.root = Builder.load_string(root_kv)
        
        if __name__ == "__main__":
            MainApp().run()

   

我希望制作一个可以从字典中获取链接并根据函数调用打开它的函数。但我是第一次尝试使用 Kivy MD,我需要帮助

标签: kivymd

解决方案


推荐阅读