大家好,欢迎来到IT知识分享网。
from tkinter import *
from tkinter import messagebox
from tkinter.colorchooser import askcolor
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.createWidget()
self.x = 0
self.y = 0
self.lastDraw = 0 #最后绘制的图形id
self.startDrawFlag = False
self.fgcolor = “red”
def createWidget(self):
#创建绘图区
self.drawPad = Canvas(root, width=900, height=500, bg=”white”)
self.drawPad.pack()
#画图软件的各种按钮
btn_pen = Button(self, text=”画笔”, name=”pen”)
btn_pen.pack(side=”left”, padx=”10″)
btn_rect = Button(self, text=”矩形”, name=”rect”)
btn_rect.pack(side=”left”, padx=”10″)
btn_clear = Button(self, text=”清屏”, name=”clear”)
btn_clear.pack(side=”left”, padx=”10″)
btn_erasor = Button(self, text=”橡皮擦”, name=”erasor”)
btn_erasor.pack(side=”left”, padx=”10″)
btn_line = Button(self, text=”直线”, name=”line”)
btn_line.pack(side=”left”, padx=”10″)
btn_lineArrow = Button(self, text=”圆规”, name=”circle”)
btn_lineArrow.pack(side=”left”, padx=”10″)
btn_color = Button(self, text=”画笔颜色”, name=”color”)
btn_color.pack(side=”left”, padx=”10″)
#为所有button绑定事件
btn_pen.bind_class(“Button”, “<1>”, self.eventManage)
self.drawPad.bind(“<ButtonRelease-1>”, self.stopDraw)
def stopDraw(self, event):
self.startDrawFlag = False
self.lastDraw = 0
def startDraw(self, event):
self.drawPad.delete(self.lastDraw)
if not self.startDrawFlag:
self.startDrawFlag = True
self.x = event.x
self.y = event.y
def myCircle(self, event):
self.startDraw(event)
self.lastDraw = self.drawPad.create_oval(self.x, self.y, event.x, event.y, outline=self.fgcolor)
def eventManage(self, event):
name = event.widget.winfo_name()
print(name)
if name == “line”:
self.drawPad.bind(“<B1-Motion>”, self.myline)
elif name == “circle”:
self.drawPad.bind(“<B1-Motion>”, self.myCircle)
elif name == “rect”:
self.drawPad.bind(“<B1-Motion>”, self.myRect)
elif name == “pen”:
self.drawPad.bind(“<B1-Motion>”, self.myPen)
elif name == “erasor”:
self.drawPad.bind(“<B1-Motion>”, self.myErasor)
elif name == “clear”:
self.drawPad.delete(“all”)
elif name == “color”:
c = askcolor(color=self.fgcolor, title=”选择画笔颜色”)
self.fgcolor = c[1]
def myline(self, event):
self.startDraw(event)
self.lastDraw = self.drawPad.create_line(self.x, self.y, event.x, event.y, fill=self.fgcolor)
def mylineArrow(self, event):
self.startDraw(event)
self.lastDraw = self.drawPad.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.drawPad.create_rectangle(self.x, self.y, event.x, event.y, outline=self.fgcolor)
def myPen(self, event):
self.startDraw(event)
self.drawPad.create_line(self.x, self.y, event.x, event.y, fill=self.fgcolor)
self.x = event.x
self.y = event.y
def myErasor(self, event):
self.startDraw(event)
self.drawPad.create_rectangle(event.x-3, event.y-3, event.x+3, event.y+3, fill=”black”)
self.x = event.x
self.y = event.y
if __name__ == ‘__main__’:
root = Tk()
root.geometry(“900×500+200+200”)
root.title(“画图软件”)
app = Application(master=root)
root.mainloop()
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/32566.html