python - 在 Kivy 中发布的多项操作
问题描述
我可以在发布时在其中添加更多功能,否则将无法正常工作。我想要显示更多的弹出功能和图片,可能是一些音频等。这是.kv文件:
<Root>:
orientation: 'vertical'
RecordButton:
id: record_button
background_color: 1,1.01,0.90,1
text: 'Order'
on_release:
self.record()
root.pop1()
height: '100dp'
size_hint_y: None
TextInput:
text: record_button.output
readonly: True
解决方案
将事件回调定义为语句序列。
KV 文件内部
缩进和结构化控制流的可读性在 KV 文件中受到限制。正如inclement 所评论的,基本上有两种定义回调序列的方法:
- 每行语句(相同的缩进)
- 分号分隔的语句
on_release:
self.record()
root.pop1()
on_press: print('pressed'); self.insert_text("pressed!")
见 Kivy 语言语法里面的有效表达式:
[..] 多个单行语句是有效的,包括那些转义换行符的语句,只要它们不添加缩进级别。
在 Python 中定义一个函数
您可以更灵活地在 Python 脚本中定义 a 函数并在 KV 文件中的事件上声明此回调。
例如,RecordButton
在 Python 中作为方法的函数(假设它是一个扩展 Button 的类):
class RecordButton(Button):
# callback function tells when button released
# It tells the state and instance of button.
def record_and_pop(self, instance):
print("Button is released")
print('The button <%s> state is <%s>' % (instance.text, instance.state))
self.record()
root.pop1()
然后在 KV 内部使用:
RecordButton:
on_release: self.record_and_more()
也可以看看
- kivy:单击 1 次按钮触发多个功能
- Kivy API:按钮类
推荐阅读
- vmware - 如何替换 ova 文件中的 vmdk 文件
- flutter - 如何将 showModalBottomSheet 设置为最初的一半高度但可扩展和可关闭
- azure-iot-hub - 设置 Azure IoT Edge 模块设备范围设置
- mysql - MYSQL - 警告:#1681 不推荐使用整数显示宽度
- windows - PowerShell Format-Table -AutoSize 不生成输出文件
- tsql - 显示用于所有表的磁盘空间 - Azure SQL 数据仓库
- linux - 更新后的linux路径问题
- bash - 如何在 Makefile 中记录准确的配方行
- python - for 循环和 if 语句交互没有按预期工作?
- javascript - 角度 2 - TypeError:无法读取属性