[英]Python - How to bind Button on_press on Kivy with multiple parameters
[英]How to Bind on_press to GridLayout in kivy python
我試圖用kv語言將on_press方法綁定到gridLayout,但出現此錯誤AttributeError:pressed 。 即使在kivy文檔中我也進行了一些研究,但是沒有幫助。因此,如果有人可以解決此問題,那么您可能是一個很好的資源
這是我的testApp.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class screendb(BoxLayout):
def mycall_back(self):
print('hello')
class testApp(App):
def build(self):
return screendb()
if __name__=='__main__':
testApp().run()
這是我的testApp.kv
<Screendb@BoxLayout>:
GridLayout:
on_pressed:root.Mycall_back()
在您的py文件中:
# Behaviors let you add behavior from one widget to another widget
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.gridlayout import GridLayout
# We create a new widget that is a GridLayout with access to Button Behaviors
# Like Button's 'on_press' method
class ButtonGrid(ButtonBehavior, GridLayout):
pass
class screendb(BoxLayout):
def mycall_back(self):
print('hello')
class testApp(App):
def build(self):
return screendb()
在您的kv文件中:
# We create a Root Widget for the ButtonGrid
<ButtonGrid>:
<Screendb@BoxLayout>:
# We add an instance of the ButtonGrid class to our main layout
ButtonGrid:
# We can now use on_press because it is a part of the ButtonBehavior class, that makes up your ButtonGrid class.
on_press:root.mycall_back()
注意:您的帖子中也有一些小錯誤。 例如,沒有方法“ on_pressed”,只有“ on_press”,您還在py文件中將回調寫為“ mycall_back”,而在kv文件中將其寫為“ Mycall_back”,這是指存在的另一種方法。 確保您的字母大小寫匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.