Getting started with GUI with Tkinter but it's not running
from tkinter import *
root = Tk()
thelabel = Label(root, "hello")
thelabel.pack()
root.mainloop()
I'm getting the following error:
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'
The documentation for Label
says that the second argument is a list not a string. You could skip this second positional argument and use the keyword argument text
:
thelabel = Label(root, text = "hello")
Instead of
thelabel = Label(root, "hello")
You should have used the "text" argument for Labels
theLabel = Label(root, text="hello")
the Label
is not done correctly, the code must be:
import tkinter
root = tk.Tk()
thelabel = tkinter.Label(root, text="hello")
thelabel.pack()
root.mainloop()
also, you can use from tkinter import*
as well, i just do it like this, if you do so, also change the label to Label(root, text="hello")
and root to just Tk()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.