python - Kivy on_press 声明后数据无效
问题描述
我正在尝试使用 floatlayout 向我的 Kivy 应用程序添加 2 个按钮并进行 on_press 调用。当我尝试运行它时,我得到一个错误:第 12 行声明后无效数据
我的 main.py
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
import __init__ as hvn
class HVNLayout(Widget):
def optBtn(self):
print("hello")
def genBtn(self):
print("Bye")
class HVNApp(App):
def build(self):
return HVNLayout()
if __name__ == '__main__':
HVNApp().run()
和.kv
<HVNLayout>:
canvas.before:
Rectangle:
size: self.size
source: "images/dice.jpg"
FloatLayout:
size: root.width, root.height
Button:
text: "Option"
on_press: root.btn()
pos_hint: {"top": 1, "right": 1}
size_hint: (None, None)
size: 200, 100
Button:
text: "Generate"
post_hint: {"x": 0.15, "y": 0.1}
size_hint: (None, None)
size: 200, 100
解决方案
您的 kv 文件的缩进不正确。它应该是:
<HVNLayout>:
canvas.before:
Rectangle:
size: self.size
source: "images/dice.jpg"
FloatLayout:
size: root.width, root.height
Button:
text: "Option"
on_press: root.btn()
pos_hint: {"top": 1, "right": 1}
size_hint: (None, None)
size: 200, 100
Button:
text: "Generate"
post_hint: {"x": 0.15, "y": 0.1}
size_hint: (None, None)
size: 200, 100
推荐阅读
- javascript - 多个按钮单击中间禁用
- javascript - 屏幕分辨率与 cookie 收集有何关系?
- django - 浏览器如何检索 Django 自定义小部件资产?
- c++ - 调用类的成员函数时类的对象损坏
- c# - C#如何从dll获取的结构中获取图片并保存在本地
- qt - 如何在自定义小部件插件上使用 qss
- node.js - 使用 puppeteer 创建流
- flutter - 如何在flutter中使用injectable和get_it使用inject String?
- mongodb - MongoDB - 按字段值对文档进行分组
- github - 如何在 GitHub 中设置标签的权限?