from tkinter import *
class Application(Frame):
''' GUI程序的类的写法 '''
def __init__(self,master=None):
super().__init__(master) # 这是父类的定义,而不是父类的对象
self.master=master
self.pack()
self.createWidget()
def createWidget(self):
''' 创建组件 '''
self.label1=Label(self, test="超霸",width=10,height=2,
bg="blue",fg="white")
self.label1.pack()
self.label2=Label(self, test="恐龙",width=10,height=2,
bg="black",fg="white",font=("黑体",30))
self.label1.pack()
if __name__=="__main__":
root=Tk()
root.geometry("300x300+300+300")
root.title("GUI程序面向对象的测试")
app=Application(master=root)
root.mainloop()
明明跟上网课的写的一模一样,但是老是报错
Traceback (most recent call last):
File "D:\pythonproject\test.py", line 23, in
app=Application(master=root)
File "D:\pythonproject\test.py", line 9, in init
self.createWidget()
File "D:\pythonproject\test.py", line 12, in createWidget
self.label1=Label(self, test="超霸",width=10,height=2,
File "D:\python\lib\tkinter_init.py", line 3177, in init
Widget.init(self, master, 'label', cnf, kw)
File "D:\python\lib\tkinter_init.py", line 2601, in init
self.tk.call(
_tkinter.TclError: unknown option "-test"
进程已结束,退出代码为 1
怎么解决?
这是因为在创建Label组件时,你使用了错误的参数名称,应该是text而不是test。
修改代码如下:
self.label1 = Label(self, text="超霸",width=10,height=2,
bg="blue",fg="white")
self.label2 = Label(self, text="恐龙",width=10,height=2,
bg="black",fg="white",font=("黑体",30))
参数错了,是text