用python+kivy实现简易的电子白板功能
立即下载
资源介绍:
用python+kivy实现简易的白板功能:具有以下功能:
1.显示画笔轨迹
2.设置画笔的颜色,可选择10种颜色,可修改代码添加
3.设置线条的粗细,也叫线宽(提供三个选择:细、正常、粗)
4.清屏
from kivy.uix.behaviors import ToggleButtonBehavior
from kivy.uix.togglebutton import ToggleButton
from kivy.app import App
from kivy.graphics import Line, Color
from kivy.uix.widget import Widget
class FrameToggleButton(ToggleButton):
# 当前按钮添加边框
def do_press(self):
# 点击改变状态
if self.state == 'normal':
ToggleButtonBehavior.do_press(self)
class DrawCanvasWidget(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# set default color
self.canvas.add(Color(rgb=[0,0,0]))
self.line_width = 2
def on_touch_down(self, touch):
# 触摸显示轨迹
if Widget.on_touch_down(self,touch):
return
with self.canvas:
touch.ud['current_line'] = Line(points=(touch.x,touch.y), width=self.line_width)
def on_touch_move(self, touch):
# 连线
if 'current_line' in touch.ud:
touch.ud['current_line'].points += (touch.x, touch.y)
def change_color(self, new_color):
# 调色
self.last_color = new_color
self.canvas.add(Color(*new_color))
def change_line_width(self, line_width='Normal'):
# 线宽
self.line_width = {'Thin': 1, 'Normal': 2, 'Thick': 4}[line_width]
def clear_canvas(self):
# 清屏
saved = self.children[:]
self.clear_widgets()
self.canvas.clear()
for widget in saved:
self.add_widget(widget)
self.change_color(self.last_color)
class PaintApp(App):
def build(self):
self.draw_canvas_widget = DrawCanvasWidget()
return self.draw_canvas_widget
if __name__ == '__main__':
pa = PaintApp()
pa.run()