kivymd - 如何使用 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,我需要帮助
解决方案
推荐阅读
- javascript - AngularJS | 仅在满足条件时如何呈现 HTML 元素(指令)?
- ansible - ansible 使用 with_items 在 hostvars 中分配 shell 结果特定主机
- mysql - 如何使用字符串连接创建 SQL 过程?
- python-3.x - 错误:mdb_env_open: No such file or directory BLAST+ 本地数据库问题
- android - 在 App Distribution 中上传的新应用不会显示在 Crashlytics 中
- dependency-injection - 如何在 DI 中用模拟替换类?Phalcon + Codeception
- java - Java 更新后 Windows 身份验证无法连接
- angular - 当在 mat 菜单中启用 contenteditable 时,contentEditable.focus 需要 setTimeout 才能工作
- apache-kafka - 一些数据没有被 kafka-engine 消耗
- javascript - Mongoose:如何通过对象的键进行查询