如何在kivy python中绑定到GridLayout

2024-10-01 17:35:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用kv语言绑定到gridLayout,但是我得到了一个错误AttributeError:pressed。我在KivyDoc上做了一些研究,但是没有帮助。所以如果有人能解决这个问题,请你可能是一个很好的资源

这是我的测试应用程序.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()

这是我的测试电压kv在

^{pr2}$

Tags: fromimportself语言appdef错误class
1条回答
网友
1楼 · 发布于 2024-10-01 17:35:58

在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文件中:

^{pr2}$

注意:你的帖子也有一些小错误。例如,没有“on_pressed”方法,只有“on_press”,您还在py文件中将回调写为“mycall_back”,而在kv文件中将其写为“mycall_back”,这表示存在另一个方法。确保你的字母大小写匹配。在

video example

相关问题 更多 >

    热门问题