Python GUI tkinter库 画图项目

1、源代码

from tkinter import *
from tkinter.colorchooser import *


win_width = 900
win_height = 450


class Appication(Frame):
    def __init__(self, master=None, bgcolor="#000000"):
        super().__init__(master)
        self.master = master
        self.pack()
        self.bgcolor = bgcolor
        self.fgcolor = "red"
        self.lastDraw = 1
        # 表示最后绘制图形的 id
        self.startDrawFlag = False
        self.CreateWidget()

    def CreateWidget(self):
        # 创建绘图区
        self.canvas1 = Canvas(self, bg=self.bgcolor, width=win_width, height=win_height*0.8)
        self.canvas1.pack()

        # 创建按钮
        self.button1_start = Button(self, text="开始", name="start")
        self.button1_start.pack(side="left", padx="10")
        self.button2_pen = Button(self, text="画笔", name="pen")
        self.button2_pen.pack(side="left", padx="10")
        self.button3_rect = Button(self, text="矩形", name="rect")
        self.button3_rect.pack(side="left", padx="10")
        self.button4_clear = Button(self, text="清屏", name="clear")
        self.button4_clear.pack(side="left", padx="10")
        self.button5_eraser = Button(self, text="橡皮擦", name="eraser")
        self.button5_eraser.pack(side="left", padx="10")
        self.button6_line = Button(self, text="直线", name="line")
        self.button6_line.pack(side="left", padx="10")
        self.button7_lineArrow = Button(self, text="箭头直线", name="lineArrow")
        self.button7_lineArrow.pack(side="left", padx="10")
        self.button8_color = Button(self, text="颜色", name="color")
        self.button8_color.pack(side="left", padx="10")

        # 事件处理
        self.button1_start.bind_class("Button", "<1>", self.eventManager)
        self.canvas1.bind("<ButtonRelease-1>", self.stopDraw)

        # 增加颜色切换的快捷键
        window.bind("<KeyPress-r>", self.kuaijiejian)
        window.bind("<KeyPress-g>", self.kuaijiejian)
        window.bind("<KeyPress-y>", self.kuaijiejian)

    def eventManager(self, event):
        name = event.widget.winfo_name()
        print(name)
        if name == "line":
            self.canvas1.bind("<B1-Motion>", self.myline)
        elif name == "lineArrow":
            self.canvas1.bind("<B1-Motion>", self.mylineArrow)
        elif name == "rect":
            self.canvas1.bind("<B1-Motion>", self.myRect)
        elif name == "pen":
            self.canvas1.bind("<B1-Motion>", self.myPen)
        elif name == "eraser":
            self.canvas1.bind("<B1-Motion>", self.myEraser)
        elif name == "clear":
            self.canvas1.delete(ALL)
        elif name == "color":
            c = askcolor(color=self.fgcolor, title="选择画笔颜色")
            self.fgcolor = c[1]
            self.canvas1.bind("<B1-Motion>", self.myEraser)

    def stopDraw(self, event):
        self.startDrawFlag = False
        self.lastDraw = 0

    def startDraw(self, event):
        self.canvas1.delete(self.lastDraw)

        if not self.startDrawFlag:
            self.startDrawFlag = True
            self.x = event.x
            self.y = event.y

    def myline(self, event):
        self.startDraw(event)
        self.lastDraw = self.canvas1.create_line(self.x, self.y,
                                                 event.x, event.y, fill=self.fgcolor)

    def mylineArrow(self, event):
        self.startDraw(event)
        self.lastDraw = self.canvas1.create_line(self.x, self.y,
                                                 event.x, event.y, arrow=LAST, fill=self.fgcolor)

    def myRect(self, event):
        self.startDraw(event)
        self.lastDraw = self.canvas1.create_rectangle(self.x, self.y,
                                                      event.x, event.y, fill="black", outline=self.fgcolor)

    def myPen(self, event):
        self.startDraw(event)
        self.canvas1.create_line(self.x, self.y,
                                 event.x, event.y, fill=self.fgcolor)
        self.x = event.x
        self.y = event.y

    def myEraser(self, event):
        self.startDraw(event)
        self.canvas1.create_rectangle(event.x - 4, event.y - 4, event.x + 4, event.y + 4, fill=self.bgcolor)
        self.x = event.x
        self.y = event.y

    def kuaijiejian(self, event):
        if event.char == "r":
            self.fgcolor = "#ff0000"
        elif event.char == "g":
            self.fgcolor = "#00ff00"
        elif event.char == "y":
            self.fgcolor = "#fff000"


if __name__ == "__main__":
    window = Tk()
    window.title("绘图软件")
    window.geometry(str(win_width) + "x" + str(win_height) + "+200+100")

    app = Appication(master=window, bgcolor="#000000")

    window.mainloop()

2、将 Python 程序打包成 exe 可执行文件

  1. 在 Terminal 终端里面输入 pyinstaller -F main.py
  2. 在 dist 文件夹里面可以找到对应生成的 exe 可执行文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是我来晚了!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值