【发布时间】:2017-10-21 18:04:53
【问题描述】:
我正在使用 python 和 kivy 构建一个基本的绘画应用程序,用于学习应用程序开发。我试图通过修改 .kv 文件上的代码来弄清楚如何在屏幕上绘图后清除画布。
在 .kv 文件中,我想我需要修改 #on_release:root.canvas.clear() 部分代码,目前它删除了整个画布和按钮。我试图弄清楚如何让它只清除屏幕,允许再次在屏幕上重绘,并且不删除按钮。
这里有一张图片作为上下文 Painter App
按下清除按钮后会发生什么 Blank Screen
from random import random
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, Color, Ellipse
class Painter(Widget):
def on_touch_down(self, touch):
color = (random(), 1.,1.) #reduce number of possible colors
with self.canvas:
Color(*color, mode='hsv') #sets the colors to be equally bright
d = 30.
Ellipse(pos=(touch.x - d / 2,touch.y - d / 2), size=(d,d))
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 AnotherScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("main3.kv") #load the kivy file
class SimpleKivy7(App):
def build(self):
return presentation
if __name__== "__main__":
SimpleKivy7().run()
以下是kivy文件
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
MainScreen:
AnotherScreen:
<MainScreen>:
name: "main"
Button:
on_release: app.root.current = "other"
text: "Next Screen"
font_size: 50
<AnotherScreen>:
name: "other"
FloatLayout:
Painter
Button:
color:0,1,0,1
font_size: 25
size_hint: 0.3, 0.2
text: "Back Home"
on_release: app.root.current = "main"
pos_hint: {"right":1, "top":1}
Button:
color:0,1,0,1
font_size: 25
size_hint: 0.3, 0.2
text: "Clear"
#on_release:root.canvas.clear() #root.canvas.clear() clears everything.
pos_hint: {"left":1, "top":1}
【问题讨论】:
标签: python button canvas kivy kivy-language