简体   繁体   中英

Getting error when importing Tkinter and making a window

Here is my code guys:

from tkinter import *

root = Tk()
theLabel = Label(root, 'Hello World')
theLabel.pack()
root.mainloop()

Here is the error:

Traceback (most recent call last): File "C:/Users/argel/PycharmProjects/day2/rockpaper.py", line 4, in theLabel = Label(root, 'Hello World') File "C:\\Users\\argel\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\tkinter__init__.py", line 2760, in init Widget. init (self, master, 'label', cnf, kw) File "C:\\Users\\argel\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\tkinter__init__.py", line 2289, in init classes = [(k, v) for k, v in cnf.items() if isinstance(k, type)] AttributeError: 'str' object has no attribute 'items'

Thank you for your help

Just change theLabel = Label(root, 'Hello World') to theLabel = Label(root, text='Hello World') and it will work.

I have a suggestion to improve your code:

Rather than from tkinter import * use import tkinter as tk .

from tkinter import * is actually discouraged so I strongly recommend you do not use this method.

Remember you would also need to change your script slightly if you do. It would look like this:

import tkinter as tk

root = tk.Tk()
theLabel = tk.Label(root, text='Hello World')
theLabel.pack()
root.mainloop()

For more information the differences between imports please see this post.

I hope it solved your problem.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM