会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132372个问题
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2281楼

"""使用面向对象测试GUI程序"""
from tkinter import *
from tkinter import messagebox


class Application(Frame):
    """一个经典的GUI程序"""
    def __init__(self, master=None):
        super().__init__(master)  # super代表父类的定义而不是父类的对象
        self.master = master
        self.pack()
        self.createWidget()

    def createWidget(self):
        """创建新的组件"""
        # 测试Label对象
        self.lab01 = Label(self, text='lxh', width=10, height=2, bg='pink', fg='white')
        self.lab01.pack()

        # 显示图像 tkinter只支持gif
        global phooto   # 把photo声明称为全局变量,避免销毁图像
        photo = PhotoImage(file='')
        self.lab02 = Label(self, image=photo)
        self.lab02.pack()

        # 显示多行文本
        self.label03 = Label(self, text="北京李欣辉\n百战程序员\n小李好帅,就是做饭不行",
                             borderwidth=5, relief="groove", justify="left")
        self.label03.pack()

        # 按键对象
        self.btn01 = Button(self)
        self.btn01["text"] = "点击送一朵小发发"
        self.btn01.pack()
        self.btn01["command"] = self.songhua

        # 退出按键
        self.btnQuit = Button(self, text="Quit", command=root.destroy)
        self.btnQuit.pack()

    def songhua(self):
        messagebox.showinfo("送一朵小发发~")


if __name__ == "__main__":
    root = Tk()
    root.geometry("500x400+500+200")
    root.title("一个经典的GUI程序类的测试")
    app = Application(master=root)
    root.mainloop()

image.png老师,点击送花的按钮会报错是为什么?上一节课的代码我没动,就是加了一下Label标签就报错了image.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2282楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2283楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2284楼
Python 全系列/第二阶段:Python 深入与提高/坦克大战 2288楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 2290楼

"""开发画图软件"""
import tkinter as tk

# 窗口的宽度和高度
win_width=900
win_height=450
class Application(tk.Frame):
    def __init__(self, master = None):
        super().__init__(master)
        self.master = master
        self.bgcolor = '#000000'
        self.x = 0
        self.y = 0
        self.fgcolor = '#ff0000'
        self.lastDraw = 0
        self.pack()
        self.createWidget()

    def createWidget(self):
        # 创建绘图区
        self.drawpad = tk.Canvas(self, width=win_width, height=win_height * 0.9, bg=self.bgcolor)
        self.drawpad.pack()

        # 创建按钮
        btn_start = tk.Button(self, text='开始', width='10', name='start');btn_start.pack(side='left', padx='10', pady='5')
        btn_pen = tk.Button(self, text='画笔', width='10', name='pen');btn_pen.pack(side='left', padx='10', pady='5')
        btn_rect = tk.Button(self, text='矩形', width='10', name='rect');btn_rect.pack(side='left', padx='10', pady='5')
        btn_clear = tk.Button(self, text='清屏', width='10', name='clear');btn_clear.pack(side='left', padx='10', pady='5')
        btn_erasor = tk.Button(self, text='橡皮擦', width='10', name='erasor');btn_erasor.pack(side='left', padx='10', pady='5')
        btn_line = tk.Button(self, text='直线', width='10', name='line');btn_line.pack(side='left', padx='10', pady='5')
        btn_lineArrow = tk.Button(self, text='箭头直线', width='10', name='lineArrow');btn_lineArrow.pack(side='left', padx='10', pady='5')
        btn_color = tk.Button(self, text='颜色', width='10', name='color');btn_color.pack(side='left', padx='10', pady='5')

    def eventManager(self,event):
        name = event.widget.winfo_name()
        print(name)
        if name == "line":
            self.drawpad.bind('<B1-Motion>',self.myline)

    def myline(self, event):
        self.drawpad.delete(self.lastDraw)
        self.lastDraw = self.drawpad.create_line(self.x, self.y, event.x, event.y, fill=self.fgcolor)


if __name__ == "__main__":
    root = tk.Tk()
    root.title('画画本')
    root.geometry(str(win_width)+'x'+str(win_height))
    app = Application(master = root)
    root.mainloop()

老师,代码写到这里,有点问题:

  1. 全程照着打,为啥,我点击直线没有反应

  2. 如下图,为啥要在事件管理中写呢,直接在对应button后面 ,绑定事件不就可以了嘛

    image.png

  3. 问题2截图中第二三行什么意思,老师木有说道,直接就有了

  4. 如下图,这个fill是什么意思

    image.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2291楼

"""开发画图软件的菜单 """
#coding = "utf-8"

from tkinter import *
from tkinter.filedialog import *  # 引用对话框
from tkinter.colorchooser import *  # 引用颜色选择器
# 窗口的宽度和高度
win_width = 900
win_height = 450

class Application(Frame):
    def __init__ (self,master=None,bgcolor="#000000"):
        super().__init__(master)
        self.master=master
        self.bgcolor=bgcolor
        self.x= 0
        self.y= 0
        self.fgcolor="#ff0000"
        self.lastDraw = 0    #表示最后绘制的图形id
        self.startDrawFlag = False
        self.pack()
        self.createWidget()

    def createWidget(self):
        # 创建绘图区
        drawpad =Canvas(root,width=win_width,height=win_height*0.9,bg=self.bgcolor)
        drawpad.pack()

        # 创建按钮
        btn_srart = Button(root,text="开始",name="start")
        btn_srart.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_clear = Button(root,text="清屏",name="clear")
        btn_clear.pack(side="left",padx="10")
        btn_erasor = Button(root,text="橡皮擦",name="erasor")
        btn_erasor.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_pen.bind_class("Button","<1>",self.eventManager)

    def eventManager(self, event):
        name = event.widget.winfo_name()
        print(name)
        if name == "line":
            self.drawpadbind("<B1-Motion>",self.myline)

    def myline(self,event):
        self.drawpad.create_line(self.x,self.y,fill=self.fgcolor)



if __name__ == '__main__':
    root = Tk()
    root.geometry(str(win_width)+"x"+str(win_height)+"+200+300")
    app = Application(master=root)
    root.title("画图软件")
    root.mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
  File "D:\software\Anaconda\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\admin\Desktop\code\深入与提高\画图软件开发1.py", line 54, in eventManager
    self.drawpadbind("<B1-Motion>",self.myline)
AttributeError: 'Application' object has no attribute 'drawpadbind'
line

这是什么原因导致的?

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2292楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2293楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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