diff --git a/drawmain.py b/drawmain.py new file mode 100644 index 0000000000000000000000000000000000000000..19483e10edc8b77260cd56662a8ae769a72628e9 --- /dev/null +++ b/drawmain.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +""" +Created on Fri Sep 10 13:58:05 2021 + +@author: flameabc +@dec: draw +简易画板,熟悉.kv文件,布局,按钮分布。。。 +""" + +from kivy.app import App +from kivy.graphics import Line, Color +from kivy.uix.widget import Widget +from kivy.utils import get_color_from_hex +from kivy.uix.behaviors import ToggleButtonBehavior +from kivy.uix.togglebutton import ToggleButton + + + +class DrawCanvasWidget(Widget): + + def __init__(self, **kwargs): + + super().__init__(**kwargs) + """ 设置默认颜色 """ + self.change_color(get_color_from_hex('#19caad')) + 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 FrameToggleButton(ToggleButton): + """ 当前按钮添加边框 """ + def do_press(self): + """ 点击改变状态 """ + if self.state == 'Normal': + ToggleButtonBehavior.do_press(self) + + +class PaintApp(App): + + def build(self): + + self.draw_canvas_widget = DrawCanvasWidget() + + return self.draw_canvas_widget + + +if __name__ == '__main__': + PaintApp().run() + diff --git a/paint.kv b/paint.kv new file mode 100644 index 0000000000000000000000000000000000000000..cfb748f9fc2445e7dac515d4c5bbe0df504d1182 --- /dev/null +++ b/paint.kv @@ -0,0 +1,98 @@ +#:import C kivy.utils.get_color_from_hex + +: + + group: 'color' + background_normal: 'radio_background_normal.png' + background_down: 'radio_background_down.png' + border: (3, 3, 3, 3) + on_release: app.draw_canvas_widget.change_color(self.background_color) + +: + + group: 'line_width' + color: C('#2c3e50') + background_color: C('#ecf0f1') + background_normal: 'radio_background_normal.png' + background_down: 'radio_background_down.png' + border: (3, 3, 3, 3) + on_release: app.draw_canvas_widget.change_line_width(self.text) + +: + + canvas.before: + Color: + rgba: 1, 1, 1, 1 + Rectangle: + # self here refers to the widget i.e FloatLayout + pos: self.pos + size: self.size + BoxLayout: + + orientation: 'horizontal' + padding: 2 + spacing: 2 + x: 0 + top: root.top + size_hint: None, None + size: 280, 40 + LineWidgetButton: + text: 'Thin' + LineWidgetButton: + text: 'Normal' + state: 'down' + LineWidgetButton: + text: 'Thick' + Button: + text: 'Clear' + on_release: root.clear_canvas() + + BoxLayout: + id: botton_box + orientation: 'horizontal' + + padding: 2 + spacing: 2 + size: root.width, 40 + + BottomColorButton: + background_color: C('#19caad') + state: 'down' + BottomColorButton: + background_color: C('#8cc7b5') + BottomColorButton: + background_color: C('#a0eee1') + BottomColorButton: + background_color: C('#bee7e9') + BottomColorButton: + background_color: C('#beedc7') + BottomColorButton: + background_color: C('#d6d5b7') + BottomColorButton: + background_color: C('#d1ba74') + BottomColorButton: + background_color: C('#e6ceac') + BottomColorButton: + background_color: C('#ecad9e') + BottomColorButton: + background_color: C('#f4606c') + BottomColorButton: + background_color: C('#3498db') + BottomColorButton: + background_color: C('#1abc9c') + BottomColorButton: + background_color: C('#2ecc71') + BottomColorButton: + background_color: C('#f1c40f') + BottomColorButton: + background_color: C('#e67e22') + BottomColorButton: + background_color: C('#e74c3c') + BottomColorButton: + background_color: C('#9b59b6') + BottomColorButton: + background_color: C('#ecf0f1') + BottomColorButton: + background_color: C('#95a5a6') + BottomColorButton: + background_color: C('#000000')