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
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 可执行文件
- 在 Terminal 终端里面输入 pyinstaller -F main.py
- 在 dist 文件夹里面可以找到对应生成的 exe 可执行文件