如何为 kivyMD 按钮分配一个功能
How to assign a function to a kivyMD button
我是 kivy 和 kivymd 的新手,我有一个问题。问题是我需要为 KivyMD RectangleFlatButton 分配一个函数来打开一个网站,但我不知道该怎么做。请告诉我怎么做。
这是我试过的;
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
import webbrowser
class func():
def open():
webbrowser.open('google.com')
KV = '''
<ContentNavigationDrawer>:
orientation: "vertical"
padding: "8dp"
ScrollView:
MDList:
MDLabel:
text: "App"
font_style: "H3"
size_hint_y: None
height: self.texture_size[1]
OneLineListItem:
text: "1"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 1"
OneLineListItem:
text: "2"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 2"
OneLineListItem:
text: "3"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 2"
Screen:
pos: 0, root.height - 591
MDToolbar:
id: toolbar
pos_hint: {"top": 1}
elevation: 10
title: "App"
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
NavigationLayout:
x: toolbar.height
ScreenManager:
id: screen_manager
Screen:
name: "scr 1"
BoxLayout:
orientation: "vertical"
padding: "10db"
MDLabel:
text: "Google"
font_style: "H3"
halign: "center"
MDLabel:
text: "Go to google!!!"
font_style: "Body2"
halign: "center"
MDRectangleFlatButton:
id: "button1"
text: "Go to Google"
pos_hint: {"center_x": 0.5}
on_press: func.open()
Screen:
name: "scr 2"
MDLabel:
text: "Screen 2"
halign: "center"
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
'''
class ContentNavigationDrawer(BoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class App(MDApp):
def build(self):
return Builder.load_string(KV)
App().run()
但是当我 运行 代码出现错误时。这是错误;
Traceback (most recent call last):
File "c:/Users/DTLM/Desktop/Python/kivy/app.py", line 113, in <module>
EduLink().run()
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\app.py", line 950, in run
runTouchApp()
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py", line 573, in runTouchApp
EventLoop.mainloop()
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py", line 347, in mainloop
self.idle()
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py", line 391, in idle
self.dispatch_input()
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py", line 342, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py", line 248, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\core\window\__init__.py", line 1412, in on_motion
self.dispatch('on_touch_down', me)
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\core\window\__init__.py", line 1428, in on_touch_down
if w.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\relativelayout.py", line 297, in on_touch_down
ret = super(RelativeLayout, self).on_touch_down(touch)
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\screenmanager.py", line 1198, in on_touch_down
return super(ScreenManager, self).on_touch_down(touch)
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\relativelayout.py", line 297, in on_touch_down
ret = super(RelativeLayout, self).on_touch_down(touch)
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivymd\uix\behaviors\ripplebehavior.py", line 231, in on_touch_down
return super().on_touch_down(touch)
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivymd\uix\button.py", line 961, in on_touch_down
return super().on_touch_down(touch)
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
self.dispatch('on_press')
File "kivy\_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch
File "kivy\_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1132, in kivy._event.EventObservers._dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\lang\builder.py", line 57, in custom_callback
exec(__kvlang__.co_value, idmap)
File "<string>", line 72, in <module>
NameError: name 'func' is not defined
请帮忙,谢谢。
在您的 kv
文件中,在开头插入:
#:import webbrowser webbrowser
那么你的on_press
可以是:
on_press: webbrowser.open('http://google.com')
我是 kivy 和 kivymd 的新手,我有一个问题。问题是我需要为 KivyMD RectangleFlatButton 分配一个函数来打开一个网站,但我不知道该怎么做。请告诉我怎么做。
这是我试过的;
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
import webbrowser
class func():
def open():
webbrowser.open('google.com')
KV = '''
<ContentNavigationDrawer>:
orientation: "vertical"
padding: "8dp"
ScrollView:
MDList:
MDLabel:
text: "App"
font_style: "H3"
size_hint_y: None
height: self.texture_size[1]
OneLineListItem:
text: "1"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 1"
OneLineListItem:
text: "2"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 2"
OneLineListItem:
text: "3"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 2"
Screen:
pos: 0, root.height - 591
MDToolbar:
id: toolbar
pos_hint: {"top": 1}
elevation: 10
title: "App"
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
NavigationLayout:
x: toolbar.height
ScreenManager:
id: screen_manager
Screen:
name: "scr 1"
BoxLayout:
orientation: "vertical"
padding: "10db"
MDLabel:
text: "Google"
font_style: "H3"
halign: "center"
MDLabel:
text: "Go to google!!!"
font_style: "Body2"
halign: "center"
MDRectangleFlatButton:
id: "button1"
text: "Go to Google"
pos_hint: {"center_x": 0.5}
on_press: func.open()
Screen:
name: "scr 2"
MDLabel:
text: "Screen 2"
halign: "center"
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
'''
class ContentNavigationDrawer(BoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class App(MDApp):
def build(self):
return Builder.load_string(KV)
App().run()
但是当我 运行 代码出现错误时。这是错误;
Traceback (most recent call last):
File "c:/Users/DTLM/Desktop/Python/kivy/app.py", line 113, in <module>
EduLink().run()
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\app.py", line 950, in run
runTouchApp()
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py", line 573, in runTouchApp
EventLoop.mainloop()
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py", line 347, in mainloop
self.idle()
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py", line 391, in idle
self.dispatch_input()
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py", line 342, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\base.py", line 248, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\core\window\__init__.py", line 1412, in on_motion
self.dispatch('on_touch_down', me)
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\core\window\__init__.py", line 1428, in on_touch_down
if w.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\relativelayout.py", line 297, in on_touch_down
ret = super(RelativeLayout, self).on_touch_down(touch)
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\screenmanager.py", line 1198, in on_touch_down
return super(ScreenManager, self).on_touch_down(touch)
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\relativelayout.py", line 297, in on_touch_down
ret = super(RelativeLayout, self).on_touch_down(touch)
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivymd\uix\behaviors\ripplebehavior.py", line 231, in on_touch_down
return super().on_touch_down(touch)
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivymd\uix\button.py", line 961, in on_touch_down
return super().on_touch_down(touch)
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
self.dispatch('on_press')
File "kivy\_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch
File "kivy\_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1132, in kivy._event.EventObservers._dispatch
File "C:\Users\DTLM\AppData\Local\Programs\Python\Python38\lib\site-packages\kivy\lang\builder.py", line 57, in custom_callback
exec(__kvlang__.co_value, idmap)
File "<string>", line 72, in <module>
NameError: name 'func' is not defined
请帮忙,谢谢。
在您的 kv
文件中,在开头插入:
#:import webbrowser webbrowser
那么你的on_press
可以是:
on_press: webbrowser.open('http://google.com')