我创建了两个可以调整大小的Scatter
小部件,我正在寻找的是一种产生更多Scatter
小部件(可能最多20个)的方法时间按下按钮。我尝试编写并将addfunction()
绑定到Button
,但它似乎无法正常工作。
我正在寻找一种方法来生成Scatterer
窗口小部件的新实例(以及具有Drawer
的子窗口小部件canvas
来显示它{&#39}。提示时占用空间。
这是我的python代码:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.scatter import Scatter
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
class Scatterer(Scatter):
pass
class Drawer(FloatLayout):
pass
class MainWindowWidget(FloatLayout):
def addfunction(*args):
root = MainWindowWidget()
s = Scatterer()
d = Drawer()
root.add_widget(s)
s.add_widget(d)
print("button is pressed")
class TestApp(App):
def build(self):
return MainWindowWidget()
if __name__ == '__main__':
TestApp().run()
和我的.kv文件:
#:kivy 1.0.9
<MainWindowWidget>
Scatterer:
id: scatter2
size: 80, 80
Drawer:
size: scatter2.size
Scatterer:
id: scatter1
size: 80, 80
Drawer:
size: scatter1.size
Button:
text: 'press'
size_hint: None, None
size: 100, 100
pos: 100, 0
on_release: root.addfunction()
<Scatterer>:
do_rotation: False
size_hint: None, None
size: self.size
canvas.after:
Color:
rgba: 1, 1, 0, 0.5
Rectangle:
size: self.size
pos: self.pos
<Drawer>:
size: self.size
canvas:
Color:
rgba: 0, 1, 0, 0.3
Rectangle:
pos: self.pos
size: self.size
答案 0 :(得分:2)
root = MainWindowWidget()
你的问题是你写了这个,然后将新的Scatter添加到root
,但是从不对root做任何事情,因此它永远不会显示。
您不希望将Scatterer添加到 new MainWindowWidget,而是添加其方法正在运行以生成新Scatterer的当前。所以用
替换你方法的声明def addfunction(self, *args):
s = Scatterer()
d = Drawer()
self.add_widget(s)
s.add_widget(d)
print("button is pressed")