[英]GUI with tkinter
使用 Tkinter 开始使用 GUI,但它没有运行
from tkinter import *
root = Tk()
thelabel = Label(root, "hello")
thelabel.pack()
root.mainloop()
我收到以下错误:
Traceback (most recent call last):
File "guidemo1.py", line 4, in <module>
thelabel = Label(root, "hello")
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2766,
in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2295, in __init__
classes = [(k, v) for k, v in cnf.items() if isinstance(k, type)]
AttributeError: 'str' object has no attribute 'items'
Label
的文档说第二个参数是一个列表而不是一个字符串。 您可以跳过第二个位置参数并使用关键字参数text
:
thelabel = Label(root, text = "hello")
代替
thelabel = Label(root, "hello")
您应该为标签使用“文本”参数
theLabel = Label(root, text="hello")
Label
没有正确完成,代码必须是:
import tkinter
root = tk.Tk()
thelabel = tkinter.Label(root, text="hello")
thelabel.pack()
root.mainloop()
此外,您也可以使用from tkinter import*
,我就是这样做的,如果您这样做,也将标签更改为Label(root, text="hello")
并将 root 更改为Tk()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.