• 十二、GUI设计-画图程序


    """小小画笔"""

    from tkinter import *
    from tkinter.filedialog import *
    from tkinter.colorchooser import *

    height_root = 450
    width_root = 900
    class Application(Frame):
    def __init__(self, master=None,bgcolors="#000000"):
    super().__init__(master) # super()代表的是父类的定义,而不是父类对象
    self.master = master
    self.bgcolors = bgcolors
    self.pack()
    self.createWidget()
    self.x = 0
    self.y = 0
    self.fgcolor = "#ff0000"
    self.lastdrawLine = 0
    self.drawFlag = FALSE
    def createWidget(self):
    #创建画图区域
    self.drawPad = Canvas(root, width=width_root, height=height_root*0.9, bg=self.bgcolors)
    self.drawPad.pack()

    #创建按钮
    btn_start = Button(text="开始", name="start")
    btn_start.pack(side="left",padx=10)
    btn_pen = Button(text="画笔", name="pen")
    btn_pen.pack(side="left", padx=10)
    btn_rect = Button(text="矩形", name="rect")
    btn_rect.pack(side="left", padx=10)
    btn_clear = Button(text="清屏", name="clear")
    btn_clear.pack(side="left", padx=10)
    btn_earsor = Button(text="橡皮差", name="earsor")
    btn_earsor.pack(side="left", padx=10)
    btn_line = Button(text="直线", name="line")
    btn_line.pack(side="left", padx=10)
    btn_lineArrow = Button(text="箭头直线", name="lineArrow")
    btn_lineArrow.pack(side="left", padx=10)
    btn_color = Button(text="颜色", name="color")
    btn_color.pack(side="left", padx=10)

    btn_pen.bind_class("Button", "<1>", self.eventManager)
    self.drawPad.bind("<ButtonRelease-1>", self.drawFlagRelease)

    def eventManager(self,event):
    name = event.widget.winfo_name()
    if name == "line":
    self.drawPad.bind("<B1-Motion>", self.myline)
    elif name == "lineArrow":
    self.drawPad.bind("<B1-Motion>", self.mylineArrow)
    elif name == "rect":
    self.drawPad.bind("<B1-Motion>", self.myrect)
    elif name == "pen":
    self.drawPad.bind("<B1-Motion>", self.mypen)
    elif name == "earsor":
    self.drawPad.bind("<B1-Motion>", self.myearsor)
    elif name == "clear":
    self.drawPad.delete("all")
    elif name == "color":
    colorpen = askcolor(color="red", title="请选择画笔颜色")
    self.fgcolor = colorpen[1]

    def drawFlagRelease(self,event):
    self.drawFlag = FALSE
    self.lastdrawLine = 0


    def drawStartMove(self,event):
    self.drawPad.delete(self.lastdrawLine)
    if not self.drawFlag:
    self.drawFlag = True
    self.x = event.x
    self.y = event.y

    def myline(self, event):
    self.drawStartMove(event)
    self.lastdrawLine = self.drawPad.create_line(self.x, self.y, event.x, event.y, fill=self.fgcolor)

    def mypen(self, event):
    self.drawStartMove(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 myearsor(self, event):
    self.drawStartMove(event)
    self.drawPad.create_rectangle(event.x-4, event.y-4, event.x+4, event.y+4, fill=self.bgcolors)



    def mylineArrow(self,event):
    self.drawStartMove(event)
    self.lastdrawLine = self.drawPad.create_line(self.x, self.y, event.x,event.y, arrow=LAST, fill=self.fgcolor)

    def myrect(self, event):
    self.drawStartMove(event)
    self.lastdrawLine = self.drawPad.create_rectangle(self.x, self.y, event.x, event.y, outline=self.fgcolor)


    if __name__ == '__main__':
    root = Tk()
    root.geometry(str(width_root)+"x"+str(height_root)+"+200+300")
    root.title("党京伟的一个画笔软件")
    app = Application(master=root)
    root.mainloop()

  • 相关阅读:
    用Service充当Domain Object
    Scrum方法回顾
    为什么使用User Story Map
    前端状态管理之状态机
    项目进度管理注意事项
    单元测试遇到的最难的问题
    JS AMD模块的循环依赖
    jupyter notebook常用快捷键
    Jupyter-NoteBook-你应该知道的N个小技巧
    Python之配置日志的几种方式(logging模块)
  • 原文地址:https://www.cnblogs.com/dangjingwei/p/12288435.html
Copyright © 2020-2023  润新知