上一次写文章还是7.18号了,到现在快20天没有更新了。也不是因为懒,我学习python的时间并不是很多。只有每天早上或者晚上下班回家才有时间。从上次更新到今天。这20天的时间里,学习了新的内容。就是 tkinter 界面设计。这里对 tkinter 就不过多介绍。先记录一下我学习的第一个软件 —— 画板
首先看一下界面效果。
导入需要的模块
from tkinter import *
import tkinter as tk
from tkinter.simpledialog import *
from tkinter.colorchooser import *
采用面向对象的方式,分步实现每个控件的功能。
1、设计主窗口界面
if __name__ == '__main__':
mywindow = Tk()
win_width = 900
win_height = 500
mywindow.geometry(str(win_width) + "x" + str(win_height + 50) + "+200+300")
mywindow.title('画板')
app = DrawApp(master=mywindow)
mywindow.mainloop()
geometry的代码格式是 geometry(“axb+c+d”) 其中“x” 是字母 x
2、创建类,继承 Frame 模块
class DrawApp(Frame):
"""开发画图软件"""
def __init__(self, master=None, bgcolor="white"):
super().__init__(master) # super 父类方法调用
self.master = master
# self.v = StringVar(mywindow) 用于存储变量,方便后面获取笔号,画出不同粗细的图画
self.v = StringVar(mywindow)
# 设置背景颜色,背景色为白色
self.bgcolor = bgcolor
# 设置xy 的初始值
self.x = 0
self.y = 0
# 设置默认前景色,也就是画笔的颜色
self.fgcolor = "red"
self.lastDraw = 0
self.startDrawFlag = False
self.pack()
self.creatwidget()
3、 创建画布,并且依次排列各个组件
def creatwidget(self):
"""创建新的组件"""
# 创建一个绘图区域
self.drawpad = Canvas(mywindow, width=win_width, height=win_height, bg=self.bgcolor)
self.drawpad.pack()
# 创建按钮
btn_start = Button(mywindow, text="开始", width=10, name="start")
btn_start.pack(side="left")
# 这里创建一个多选菜单,可供选择不同粗细的笔号
# 设置变量,存储笔号,方便后面判断不同的笔号,响应不同的程序,做出不同的输出
self.v.set("笔号")
btn_pen1 = OptionMenu(mywindow, self.v, "1号笔", "2号笔", "3号笔")
btn_pen1.pack(side="left")
btn_rect = Button(mywindow, text="矩形", width=10, name="rect")
btn_rect.pack(side="left")
btn_clear = Button(mywindow, text="清屏", width=10, name="clear")
btn_clear.pack(side="left")
btn_erasor = Button(mywindow, text="橡皮檫", width=10, name="erasor")
btn_erasor.pack(side="left")
btn_line = Button(mywindow, text="直线", width=10, name="line")
btn_line.pack(side="left")
btn_lineArrow = Button(mywindow, text="箭头直线", width=10, name="lineArrow")
btn_lineArrow.pack(side="left")
btn_color = Button(mywindow, text="颜色", width=10, name="color")
btn_color.pack(side="left")
btn_oval = Button(mywindow, text="椭圆", width=10, name="oval")
btn_oval.pack(side="left")
btn_Text = Button(mywindow, text="画笔", width=10, name="pen2")
btn_Text.pack(side="left")
# 事件处理
# bind_class,它接受三个参数,第一个参数是类名,第二个参数是事件类型,第三个参数是相应的操作
btn_pen1.bind_class("Button", "", self.eventManager)
self.drawpad.bind("", self.stopDraw)
4、分别处理不同的事件
def eventManager(self, event):
# 获取事件名字,根据事件名字判断操作类型
name = event.widget.winfo_name()
if name == "line":
# 在画图之前,必须要选择笔号才能够开始, self.message() 用来提示
# 用bind 函数绑定不同的事件
self.message()
self.drawpad.bind("", self.myLine)
elif name == "lineArrow":
self.message()
self.drawpad.bind("", self.myLineArrow)
elif name == "rect":
self.message()
self.drawpad.bind("", self.myRect)
elif name == "pen2":
self.message()
self.drawpad.bind("", self.myPen)
elif name == "oval":
self.message()
self.drawpad.bind("", self.myOval)
elif name == "erasor":
self.message()
self.drawpad.bind("", self.myErasor)
elif name == "clear":
self.drawpad.delete("all")
elif name == "color":
c = askcolor(color=self.fgcolor, title="选择画笔颜色")
self.fgcolor = c[1]
elif name == "text":
self.drawpad.bind("", self.myText)
5、写出对应事件的函数
# 增加一个判断
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
5.1 画直线
def myLine(self, event):
if self.v.get() == "1号笔":
self.startDraw(event) # 这个设置使得每次画线不会重复出现
self.lastDraw = self.drawpad.create_line(self.x, self.y, event.x, event.y,
fill=self.fgcolor, width=1)
if self.v.get() == "2号笔":
self.startDraw(event)
self.lastDraw = self.drawpad.create_line(self.x, self.y, event.x, event.y,
fill=self.fgcolor, width=3)
if self.v.get() == "3号笔":
self.startDraw(event)
self.lastDraw = self.drawpad.create_line(self.x, self.y, event.x, event.y,
fill=self.fgcolor, width=5)
5.2、画带箭头的直线
def myLineArrow(self, event):
if self.v.get() == "1号笔":
self.startDraw(event)
self.lastDraw = self.drawpad.create_line(self.x, self.y, event.x, event.y, arrow=LAST,
fill=self.fgcolor, width=1)
if self.v.get() == "2号笔":
self.startDraw(event)
self.lastDraw = self.drawpad.create_line(self.x, self.y, event.x, event.y, arrow=LAST,
fill=self.fgcolor, width=3)
if self.v.get() == "3号笔":
self.startDraw(event)
self.lastDraw = self.drawpad.create_line(self.x, self.y, event.x, event.y, arrow=LAST,
fill=self.fgcolor, width=5)
5.3、画矩形
def myRect(self, event):
if self.v.get() == "1号笔":
self.startDraw(event)
self.lastDraw = self.drawpad.create_rectangle(self.x, self.y, event.x, event.y,
outline=self.fgcolor, width=1)
if self.v.get() == "2号笔":
self.startDraw(event)
self.lastDraw = self.drawpad.create_rectangle(self.x, self.y, event.x, event.y,
outline=self.fgcolor, width=3)
if self.v.get() == "3号笔":
self.startDraw(event)
self.lastDraw = self.drawpad.create_rectangle(self.x, self.y, event.x, event.y,
outline=self.fgcolor, width=5)
5.4、画椭圆
def myOval(self, event):
if self.v.get() == "1号笔":
self.startDraw(event)
self.lastDraw = self.drawpad.create_oval(self.x, self.y, event.x, event.y,
outline=self.fgcolor, width=1)
if self.v.get() == "2号笔":
self.startDraw(event)
self.lastDraw = self.drawpad.create_oval(self.x, self.y, event.x, event.y,
outline=self.fgcolor, width=3)
if self.v.get() == "3号笔":
self.startDraw(event)
self.lastDraw = self.drawpad.create_oval(self.x, self.y, event.x, event.y,
outline=self.fgcolor, width=5)
5.5、自由画笔
def myPen(self, event):
if self.v.get() == "1号笔":
self.startDraw(event)
self.drawpad.create_line(self.x, self.y, event.x, event.y,
fill=self.fgcolor, width=1)
self.x = event.x
self.y = event.y
if self.v.get() == "2号笔":
self.startDraw(event)
self.drawpad.create_line(self.x, self.y, event.x, event.y,
fill=self.fgcolor, width=3)
self.x = event.x
self.y = event.y
if self.v.get() == "3号笔":
self.startDraw(event)
self.drawpad.create_line(self.x, self.y, event.x, event.y,
fill=self.fgcolor, width=5)
self.x = event.x
self.y = event.y
5.6、橡皮檫
def myErasor(self, event):
if self.v.get() == "1号笔":
self.startDraw(event)
self.drawpad.create_rectangle(event.x-3, event.y-3, event.x, event.y,
outline=self.bgcolor,
fill=self.bgcolor)
self.x = event.x
self.y = event.y
if self.v.get() == "2号笔":
self.startDraw(event)
self.drawpad.create_rectangle(event.x-8, event.y-8, event.x, event.y,
outline=self.bgcolor,
fill=self.bgcolor)
self.x = event.x
self.y = event.y
if self.v.get() == "3号笔":
self.startDraw(event)
self.drawpad.create_rectangle(event.x-20, event.y-20, event.x, event.y,
outline=self.bgcolor,
fill=self.bgcolor)
self.x = event.x
self.y = event.y
5.7、提示信息,如果没有选择画笔,则无法开始
def message(self):
if self.v.get() == "笔号":
messagebox.showwarning("未选择笔号", "请先选择笔号")
到此,整个程序就写完了。这个程序是根据老师的课程,做了稍微的改动。关于各个画笔的控件函数,需要好好的琢磨琢磨,这个可能不是很容易理解的。学习就是不断发现问题解决问题的过程中。逆水行舟,只有坚持才能前行。