我有一个有效的Kivy代码,如下所示。我运行后能够绘制随机形状。现在,我想在单击Hello按钮一次后才启用绘图。我该如何修改我的代码?
Python代码
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.widget import Widget
from kivy.graphics import Line
class Painter(Widget):
def on_touch_down(self, touch):
with self.canvas:
touch.ud["line"] = Line( points = (touch.x, touch.y))
def on_touch_move(self, touch):
touch.ud["line"].points += [touch.x, touch.y]
class MainScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("main3.kv")
class MainApp(App):
def build(self):
return presentation
if __name__=="__main__":
MainApp().run()
kv文件:
#:kivy 1.0.9
ScreenManagement:
MainScreen:
<MainScreen>:
name:"main"
FloatLayout:
Painter
Button:
text: "Hello"
font_size: 50
color: 0,1,0,1
size_hint: 0.3,0.2
pos_hint: {"right":1, "top":1}
答案 0 :(得分:0)
如何制作新的绘画画面。
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.widget import Widget
from kivy.graphics import Line
KV = '''
ScreenManagement:
MainScreen:
PaintScreen:
<MainScreen>:
name:"main"
FloatLayout:
Button:
text: "Go paint"
font_size: 50
color: 0,1,0,1
size_hint: 0.3,0.2
pos_hint: {"right":1, "top":1}
on_release:
root.manager.current = 'paint'
<PaintScreen@Screen>:
name: 'paint'
FloatLayout:
Button:
text: "Exit painting"
font_size: 40
color: 0,1,0,1
size_hint: 0.3,0.2
pos_hint: {"right":1, "top":1}
on_release:
root.manager.current = 'main'
Painter:
'''
class Painter(Widget):
def on_touch_down(self, touch):
with self.canvas:
touch.ud["line"] = Line( points = (touch.x, touch.y))
def on_touch_move(self, touch):
touch.ud["line"].points += [touch.x, touch.y]
class MainScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
class MainApp(App):
def build(self):
return Builder.load_string(KV)
if __name__=="__main__":
MainApp().run()
答案 1 :(得分:0)
您可以使用a切换按钮并将其状态链接到Painter:
...
Painter:
hello_active: hello_button.state == 'down'
ToggleButton:
id: hello_button
text: "Hello"
font_size: 50
color: 0,1,0,1
size_hint: 0.3,0.2
pos_hint: {"right":1, "top":1}
并在python文件中......
class Painter(Widget):
hello_active = BooleanProperty(False)
def on_touch_down(self, touch):
if not self.hello_active:
return #nothing to see here ...
with self.canvas:
touch.ud["line"] = Line( points = (touch.x, touch.y))
def on_touch_move(self, touch):
if 'line' in touch.ud:
touch.ud["line"].points += [touch.x, touch.y]