[英]KIVY: How do I make a button to initiate my Paint app
我有一個工作的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}
如何制作一個新的繪畫畫面。
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()
您可以使用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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.