一、代码如下:
"""测试tkinter的label组件的基本用法,使用面向对象的方法"""
from tkinter import *
class Application(Frame):
def __init__(self,master=None):
super().__init__(master)
self.master = master
self.pack()
self.createWidget()
def createWidget(self):
"""创建组件"""
self.btn01 = Button(self,text="登录",width=6,height=3,anchor=E,command=self.login)
self.btn01.pack()
self.label01 = Label(self,text="第一个标签",width=10,height=2,bg="black",fg="white")
self.label01.pack()
self.label02 = Label(self,text="csh",width=10,height=2,bg="blue",fg="white",font=("黑体",30))
self.label02.pack()
# 显示图像
global photo #定义全局变量photo,若是局部变量,本方法执行完毕后,图像对象会销毁而不显示
photo = PhotoImage(file="d:/PythonProject/Examplegif.gif")
self.label03 = Label(self,image=photo)
self.label03.pack()
self.label04 = Label(self,text="河北省\n黄骅港\n崔树辉",justify="left",width=30,height=10,bg="red",fg="yellow",font=("微软雅黑",10))
self.label04.pack()
if __name__ == "__main__":
root = Tk()
root.geometry("600x400+200+300")
app = Application(master=root)
app.mainloop()
二、报错信息:
Traceback (most recent call last):
File "D:/MyPythonProject/venv/a/module_A2.py", line 34, in <module>
app = Application(master=root)
File "D:/MyPythonProject/venv/a/module_A2.py", line 10, in __init__
self.createWidget()
File "D:/MyPythonProject/venv/a/module_A2.py", line 14, in createWidget
self.btn01 = Button(self,text="登录",width=6,height=3,anchor=E,command=self.login)
AttributeError: 'Application' object has no attribute 'login'
三、我刚才看了前面有同学提问了这个问题,当时老师解答是缩进原因,我检查了这段代码,没有发现缩进问题,请老师费心解答,谢谢。