当前位置:   article > 正文

Python+Tkinter画图工具_tkinter制作绘图软件

tkinter制作绘图软件

在本文中,我们将在 Python Tkinter GUI 中设计和构建一个基本的绘图应用程序,在这里我们可以简单地使用铅笔在画布上绘制一些东西并用橡皮擦擦除它,以及改变铅笔粗细的和橡皮擦。我们还可以修改画布的背景颜色并将特定的绘图保存在我们的本地计算机上。



from tkinter import *
from tkinter.ttk import Scale
from tkinter import colorchooser, filedialog, messagebox
import PIL.ImageGrab as ImageGrab
# from ttkbootstrap import ttk
from tkmacosx import Button

# Defining Class and constructor of the Program
class Draw():
    def __init__(self, root):

        # Defining title and Size of the Tkinter Window GUI
        self.root = root
        self.root.title("画图工具Python")
        self.root.geometry("810x566")
        self.root.configure(background="white")
        #         self.root.resizable(0,0)

        # variables for pointer and Eraser
        self.pointer = "black"
        self.erase = "white"

        # Widgets for Tkinter Window

        # Configure the alignment , font size and color of the text
        text = Text(root, height=2, width=100)
        text.tag_configure("tag_name", justify='center', font=('arial', 25), background='#292826', foreground='orange')

        # Insert a Text
        text.insert("1.0", "python中画画")

        # Add the tag for following given text
        text.tag_add("tag_name", "1.0", "end")
        text.pack()

        # Pick a color for drawing from color pannel
        self.pick_color = LabelFrame(self.root, text='Colors', font=('arial', 15), bd=5, relief=RIDGE, bg="white")
        self.pick_color.place(x=0, y=40, width=135, height=525)

        colors = ['blue', 'red', 'green', 'orange', 'violet', 'black', 'yellow', 'purple', 'pink', 'gold', 'brown',
                  'indigo']
        i = j = 0
        for color in colors:

            Button(self.pick_color, bg=color, width=61,
                   command=lambda col=color: self.select_color(col)).grid(row=i, column=j)
            i += 1
            if i == 6:
                i = 0
                j = 1

        # 擦除按钮
        self.eraser_btn = Button(self.root, text="Eraser", command=self.eraser, width=120)
        self.eraser_btn.place(x=7, y=230)  # 改

        # 清屏
        self.clear_screen = Button(self.root, text="Clear Screen",width=120,
                                   command=lambda: self.background.delete('all'))
        self.clear_screen.place(x=7, y=260)

        # 保存
        self.save_btn = Button(self.root, text="ScreenShot",command=self.save_drawing,width=120)
        self.save_btn.place(x=7, y=290)

        # 改变画布背景
        self.bg_btn = Button(self.root, text="Background", command=self.canvas_color,width=120)
        self.bg_btn.place(x=7, y=320)

        # 改变画笔大小
        self.pointer_frame = LabelFrame(self.root, text='size', bd=5, bg='white', font=('arial', 15, 'bold'),
                                        relief=RIDGE)
        self.pointer_frame.place(x=33, y=360, height=200, width=70)

        self.pointer_size = Scale(self.pointer_frame, orient=VERTICAL, from_=1, to=48, length=168)
        self.pointer_size.set(1)
        self.pointer_size.grid(row=0, column=1, padx=15)

        # 定一个画布
        self.background = Canvas(self.root, bg='white', bd=5, relief=GROOVE, height=507, width=650)
        self.background.place(x=140, y=45)

        # 监听画布被点击
        self.background.bind("<B1-Motion>", self.paint)

    # Functions are defined here

    # 画笔函数
    def paint(self, event):
        x1, y1 = (event.x - 2), (event.y - 2)
        x2, y2 = (event.x + 2), (event.y + 2)

        self.background.create_oval(x1, y1, x2, y2, fill=self.pointer, outline=self.pointer,
                                    width=self.pointer_size.get())

    # Function for choosing the color of pointer
    def select_color(self, col):
        self.pointer = col

    # Function for defining the eraser
    def eraser(self):
        self.pointer = self.erase

    # Function for choosing the background color of the Canvas
    def canvas_color(self):
        color = colorchooser.askcolor()
        self.background.configure(background=color[1])
        self.erase = color[1]

    # Function for saving the image file in Local Computer
    def save_drawing(self):
        try:
            # self.background update()
            file_ss = filedialog.asksaveasfilename(defaultextension='jpg')
            # print(file_ss)
            x = self.root.winfo_rootx() + self.background.winfo_x()
            # print(x, self.background.winfo_x())
            y = self.root.winfo_rooty() + self.background.winfo_y()
            # print(y)

            x1 = x + self.background.winfo_width()
            # print(x1)
            y1 = y + self.background.winfo_height()
            # print(y1)
            ImageGrab.grab().crop((x, y, x1, y1)).save(file_ss)
            messagebox.showinfo('Screenshot Successfully Saved as' + str(file_ss))

        except:
            print("Error in saving the screenshot")


if __name__ == "__main__":
    root = Tk()
    p = Draw(root)
    root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136

在这里插入图片描述
注意:
目前Mac测试OK。 windows需要改样式或者一些参数 具体自行研究
另外

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/772863
推荐阅读
相关标签