会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132428个问题
JAVA 全系列/第十八阶段:亿级高并发电商项目_架构/编码(旧)/电商:基于SpringSecurity实现后台登录功能 32630楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 32631楼
WEB前端全系列/第十九阶段:Vue2知识体系(旧)/网络请求 32633楼
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 32634楼
JAVA 全系列/第八阶段:Linux入门到实战/Maven 32635楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 32637楼

"""
试做一个画图软件
"""

# 导入相关模块
from tkinter.filedialog import *
from tkinter.colorchooser import *

'''
# 定义窗口大小    
root = Tk()
root.geometry("1000x400+900+200")
root.title("百战程序员的画图软件")

---以上三行使用在后续下方不适用类的情况;
---若使用类,则需要在最下方写if.... 来设定窗口大小

'''


# 定义初始类-父类Frame
class Application(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master

        self.x = 0  # 定义一个初始的X和Y的坐标,后续捕捉实际参数坐标来画线;
        self.y = 0
        self.fgcolor="red"
        self.pack()

        self.createWidget()


# 创建绘图区
    def createWidget(self):
        drawpad = Canvas(root, width=1000, height=350, bg="green").pack()          # Canvas=画布
# 建立按键
        btn_start = Button(root, text="开始", name="start")
        btn_start.pack(side="left", padx="10")
        btn_pen = Button(root, text="画笔", name="pen")
        btn_pen.pack(side="left", padx="10")
        btn_rect = Button(root, text="矩形", name="rect")
        btn_rect.pack(side="left", padx="10")
        btn_line = Button(root, text="直线", name="line")
        btn_line.pack(side="left", padx="10")
        btn_lineArrow = Button(root, text="箭头", name="lineArrow")
        btn_lineArrow.pack(side="left", padx="10")
        btn_color = Button(root, text="颜色", name="color")
        btn_color.pack(side="left", padx="10")
        btn_erasor = Button(root, text="橡皮擦", name="erasor")
        btn_erasor.pack(side="left", padx="10")
        btn_clear = Button(root, text="清除", name="clear")
        btn_clear.pack(side="left", padx="10")

 # 触发事件条件
        btn_line.bind_class("Button", "<1>", self.eventManager)
        # bind_class 同类事件的统一绑定,这里是Button按键类的事件绑定-鼠标左键条件
        #self.drawpad.bind("")

    def eventManager(self, event):
        name = event.widget.winfo_name()
        print(name)

        if name == "line":
            self.drawpad.bind("<B1-Motion>", self.myline)    # <B1-Motion>按住鼠标左键移动
            #self.drawpad.bind("<B1-Motion>", self.myline)

    def myline(self, event):
        #print(event.x, event.y)
        self.drawpad.create_line(self.x, self.y, event.x, event.y, fill=self.fgcloer)





if __name__ == '__main__':
    root = Tk()
    root.geometry("1000x400+900+200")
    root.title("百战程序员的画图软件")
    app = Application(master=root)
    root.mainloop()

运行点直线的时候:报错

Exception in Tkinter callback

Traceback (most recent call last):

  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__

    return self.func(*args)

  File "H:/python DM/MYPY_0204_GUI/Pen.py", line 66, in eventManager

    self.drawpad.bind("<B1-Motion>", self.myline)    # <B1-Motion>按住鼠标左键移动

AttributeError: 'Application' object has no attribute 'drawpad'



请问老师要怎么修改!错在哪里了?

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 32638楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 32639楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637