用画布撤消和重做按钮

发布于 2025-01-18 18:01:01 字数 4934 浏览 3 评论 0原文

昨天我完成了一个简单的油漆应用程序的Kivy项目,然后将其交给我的一个朋友,他告诉我,如果您添加两个撤消和重做按钮,那么您的应用程序看起来会很好,并且会更有用。但是当我尝试这样做时,我无法做很多事情,我搜索很多,但我无法得到任何令人满意的答案

带有撤消按钮的主python文件,但没有功能

from random import random
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.graphics import Color, Ellipse, Line
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.spinner import Spinner
from kivy.uix.widget import Widget


class MyPaintWidget(Widget):
    def on_touch_down(self, touch):
        self.d = MyPaintApp.sizeSpinner.text
        if MyPaintApp.colorSpinner.text == "Random Changing":
            color = (random(), random(), random())
        if MyPaintApp.colorSpinner.text == "Black":
            color = (0, 0, 0)
        if MyPaintApp.colorSpinner.text == "Red":
            color = (1, 0, 0)
        if MyPaintApp.colorSpinner.text == "Green":
            color = (0, 1, 0)
        if MyPaintApp.colorSpinner.text == "Blue":
            color = (0, 0, 1)
        if MyPaintApp.colorSpinner.text == "Pink":
            color = (1, 0, 1)
        if MyPaintApp.colorSpinner.text == "Sky Blue":
            color = (50/255.0, 150/255.0, 209/255.0)
        if MyPaintApp.colorSpinner.text == "Gray":
            color = (75/255.0, 82/255.0, 92/255.0)
        if MyPaintApp.colorSpinner.text == "White":
            color = (1, 1, 1)

        with self.canvas:
            Color(*color)
            Ellipse(pos=(touch.x - int(self.d) / 2, touch.y -int(self.d) / 2), size=(int(self.d), int(self.d)))
            if int(self.d)<=25:
                self.line = Line(points=(touch.x, touch.y), width=int(self.d)/3)
            else:
                self.line = Line(points=(touch.x, touch.y), width=int(self.d)/2.5)

    def on_touch_move(self, touch):
        self.line.points += [touch.x, touch.y]



class MyPaintApp(App):
    colorSpinner = Spinner(
        text="Black",
        values=["Random Changing", "Red", "Green", "Blue", "Black", "White", "Pink", "Gray", "Sky Blue"],
        font_size=20,
        background_normal="",
        background_color=(0, 0, 1, .75),
        bold=True,
        italic=True
    )
    sizeSpinner = Spinner(
        text="10",
        values=["5", "10", "15", "20", "25", "30", "35", "40", "45", "50"],
        font_size=20,
        background_normal="",
        background_color=(0, 1, 0, .75),
        bold=True,
        italic=True,
    )
    bgColor = Spinner(
        text="White",
        values=["White", "Red", "Green", "Blue", "Black", "Pink", "Gray", "Sky Blue"],
        font_size=20,
        background_normal="",
        background_color=(0, 0, 1, .75),
        bold=True,
        italic=True
    ) 

    undoBtn = Button(
        text="Undo",
        background_normal="",
        background_color=(1, 0, 1, .75),
        bold=True,
        italic=True,
        font_size=20
    )

    def __init__(self, **kw):
        super(MyPaintApp, self).__init__(**kw)
        Clock.schedule_interval(self.update, 1/60)


    def build(self):
        parent = BoxLayout(
            orientation="vertical"
        )
        self.painter = MyPaintWidget()
        bottom_bar=BoxLayout(size_hint=(1, .1))
        clearbtn = Button(
            text='Clear',
            bold=True,
            italic=True,
            font_size=24,
            background_normal="",
            background_color=(1, 0, 0, .75),
        )
        clearbtn.bind(
            on_release=self.clear_canvas
        )

        bottom_bar.add_widget(clearbtn)
        bottom_bar.add_widget(self.colorSpinner)
        bottom_bar.add_widget(self.sizeSpinner)
        bottom_bar.add_widget(self.bgColor)
        bottom_bar.add_widget(self.undoBtn)



        parent.add_widget(self.painter)
        parent.add_widget(bottom_bar)
        return parent

   def clear_canvas(self, obj):
       self.painter.canvas.clear()

   def update(self, dt):
        if self.bgColor.text == "Random Changing":
            Window.clearcolor = (random(), random(), random(), 1)
        if self.bgColor.text == "Black":
            Window.clearcolor = (0, 0, 0, 1)
        if self.bgColor.text == "Red":
            Window.clearcolor = (1, 0, 0, 1)
        if self.bgColor.text == "Green":
           Window.clearcolor = (0, 1, 0, 1)
        if self.bgColor.text == "Blue":
            Window.clearcolor = (0, 0, 1, 1)
        if self.bgColor.text == "Pink":
           Window.clearcolor = (1, 0, 1, 1)
        if self.bgColor.text == "Sky Blue":
            Window.clearcolor = (50/255.0, 150/255.0, 209/255.0, 1)
        if self.bgColor.text == "Gray":
            Window.clearcolor = (75/255.0, 82/255.0, 92/255.0, 1)
        if self.bgColor.text == "White":
            Window.clearcolor = (1, 1, 1, 1)


if __name__ == "__main__":
    app = MyPaintApp(title="PaintVerse: A Simple And Easy To Use Paint App")
    app.run()

Yesterday i Completed my Kivy Projects of a simple paint app and i give it to one of my friend and he told me that if You add Two Undo and Redo Buttons Then Your Application will Look Good and it will be more usable . But When I Try To Do It I Was Not Able To Do It i search a lot but i can't get any satisfying answer

Main Python File With Undo Button But No Function

from random import random
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.graphics import Color, Ellipse, Line
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.spinner import Spinner
from kivy.uix.widget import Widget


class MyPaintWidget(Widget):
    def on_touch_down(self, touch):
        self.d = MyPaintApp.sizeSpinner.text
        if MyPaintApp.colorSpinner.text == "Random Changing":
            color = (random(), random(), random())
        if MyPaintApp.colorSpinner.text == "Black":
            color = (0, 0, 0)
        if MyPaintApp.colorSpinner.text == "Red":
            color = (1, 0, 0)
        if MyPaintApp.colorSpinner.text == "Green":
            color = (0, 1, 0)
        if MyPaintApp.colorSpinner.text == "Blue":
            color = (0, 0, 1)
        if MyPaintApp.colorSpinner.text == "Pink":
            color = (1, 0, 1)
        if MyPaintApp.colorSpinner.text == "Sky Blue":
            color = (50/255.0, 150/255.0, 209/255.0)
        if MyPaintApp.colorSpinner.text == "Gray":
            color = (75/255.0, 82/255.0, 92/255.0)
        if MyPaintApp.colorSpinner.text == "White":
            color = (1, 1, 1)

        with self.canvas:
            Color(*color)
            Ellipse(pos=(touch.x - int(self.d) / 2, touch.y -int(self.d) / 2), size=(int(self.d), int(self.d)))
            if int(self.d)<=25:
                self.line = Line(points=(touch.x, touch.y), width=int(self.d)/3)
            else:
                self.line = Line(points=(touch.x, touch.y), width=int(self.d)/2.5)

    def on_touch_move(self, touch):
        self.line.points += [touch.x, touch.y]



class MyPaintApp(App):
    colorSpinner = Spinner(
        text="Black",
        values=["Random Changing", "Red", "Green", "Blue", "Black", "White", "Pink", "Gray", "Sky Blue"],
        font_size=20,
        background_normal="",
        background_color=(0, 0, 1, .75),
        bold=True,
        italic=True
    )
    sizeSpinner = Spinner(
        text="10",
        values=["5", "10", "15", "20", "25", "30", "35", "40", "45", "50"],
        font_size=20,
        background_normal="",
        background_color=(0, 1, 0, .75),
        bold=True,
        italic=True,
    )
    bgColor = Spinner(
        text="White",
        values=["White", "Red", "Green", "Blue", "Black", "Pink", "Gray", "Sky Blue"],
        font_size=20,
        background_normal="",
        background_color=(0, 0, 1, .75),
        bold=True,
        italic=True
    ) 

    undoBtn = Button(
        text="Undo",
        background_normal="",
        background_color=(1, 0, 1, .75),
        bold=True,
        italic=True,
        font_size=20
    )

    def __init__(self, **kw):
        super(MyPaintApp, self).__init__(**kw)
        Clock.schedule_interval(self.update, 1/60)


    def build(self):
        parent = BoxLayout(
            orientation="vertical"
        )
        self.painter = MyPaintWidget()
        bottom_bar=BoxLayout(size_hint=(1, .1))
        clearbtn = Button(
            text='Clear',
            bold=True,
            italic=True,
            font_size=24,
            background_normal="",
            background_color=(1, 0, 0, .75),
        )
        clearbtn.bind(
            on_release=self.clear_canvas
        )

        bottom_bar.add_widget(clearbtn)
        bottom_bar.add_widget(self.colorSpinner)
        bottom_bar.add_widget(self.sizeSpinner)
        bottom_bar.add_widget(self.bgColor)
        bottom_bar.add_widget(self.undoBtn)



        parent.add_widget(self.painter)
        parent.add_widget(bottom_bar)
        return parent

   def clear_canvas(self, obj):
       self.painter.canvas.clear()

   def update(self, dt):
        if self.bgColor.text == "Random Changing":
            Window.clearcolor = (random(), random(), random(), 1)
        if self.bgColor.text == "Black":
            Window.clearcolor = (0, 0, 0, 1)
        if self.bgColor.text == "Red":
            Window.clearcolor = (1, 0, 0, 1)
        if self.bgColor.text == "Green":
           Window.clearcolor = (0, 1, 0, 1)
        if self.bgColor.text == "Blue":
            Window.clearcolor = (0, 0, 1, 1)
        if self.bgColor.text == "Pink":
           Window.clearcolor = (1, 0, 1, 1)
        if self.bgColor.text == "Sky Blue":
            Window.clearcolor = (50/255.0, 150/255.0, 209/255.0, 1)
        if self.bgColor.text == "Gray":
            Window.clearcolor = (75/255.0, 82/255.0, 92/255.0, 1)
        if self.bgColor.text == "White":
            Window.clearcolor = (1, 1, 1, 1)


if __name__ == "__main__":
    app = MyPaintApp(title="PaintVerse: A Simple And Easy To Use Paint App")
    app.run()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文