python - 在 GridLayout 中显示 Kivy 画布
问题描述
我试图在进一步开发之前通过绘制简单的矩形来引用 GridLayout 中的 Canvas。代码:
主要.py:
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.graphics.vertex_instructions import Rectangle
from kivy.graphics.context_instructions import Color
class Grafika(Widget):
def __init__(self, **kwargs):
super(Grafika,self).__init__(**kwargs)
with self.canvas:
Color(1, 0, 0, 1)
Rectangle(pos=self.pos,size=self.size)
kv=Builder.load_file("my.kv")
class MyMainApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyMainApp().run()
我的.py
<TextInput>:
font_size:20
color: 0.3, 0.6,0.7,1
size_hint: (0.3,0.5)
<Button>:
font_size:20
color: 0.3, 0.6,0.7,1
size_hint: (0.3,0.3)
Grafika:
GridLayout:
cols:2
GridLayout:
size_hint: (0.3,0.2)
cols:1
GridLayout:
cols:2
Label:
text:"no"
TextInput:
text: "50"
Label:
text:"rbr"
TextInput:
text: "100"
Button:
text:"calc"
canvas:
Color:
rbg:1, 1, 1, 1
Rectangle:
pos:self.pos
size:self.size
运行后我得到错误:
...
27: Button:
28: text:"calc"
>> 29: canvas:
30: Color:
31: rbg:1, 1, 1, 1
...
Canvas instructions added in kv must be declared before child widgets.
我是 kivy 的新手,因此不胜感激。
解决这个问题后,我打算运行自定义 python func ,在按钮 calc 下运行,并在画布上返回计算结果。Func 结果是二维线点,因此在画布上绘制结果是这里的目标。
解决方案
改变:
Grafika:
GridLayout:
cols:2
GridLayout:
size_hint: (0.3,0.2)
cols:1
GridLayout:
cols:2
Label:
text:"no"
TextInput:
text: "50"
Label:
text:"rbr"
TextInput:
text: "100"
Button:
text:"calc"
canvas:
Color:
rbg:1, 1, 1, 1
Rectangle:
pos:self.pos
size:self.size
至:
Grafika:
GridLayout:
cols:2
canvas:
Color:
rbg:1, 1, 1, 1
Rectangle:
pos:self.pos
size:self.size
GridLayout:
size_hint: (0.3,0.2)
cols:1
GridLayout:
cols:2
Label:
text:"no"
TextInput:
text: "50"
Label:
text:"rbr"
TextInput:
text: "100"
Button:
text:"calc"
把canvas:
指令摆在孩子们面前。
推荐阅读
- mongodb - 按部分文本搜索时的MongoDB快速查询
- java - java.lang.NoClassDefFoundError:名称错误?但如何?
- python - 为什么scrapy shell不适用于此网址?
- flutter - 颤振如何在键盘出现时禁用剪辑路径调整大小?
- apache-spark - 以非字符串化格式向 kafka 发送 json 事件
- c - 反斜杠换行符组合如何影响 C 预处理器的 __LINE__ 宏的值?
- pandas - 在 pandas 多索引中加入/合并索引和非索引列
- python - 如何从 .csv 文件中删除一行
- json - 使用 Express js 将 JSON 文件转发到其他地址
- angular - 支持 IE11 的 PDF 查看器的 Angular 包?