2017-08-11 74 views
0

以下是顯示一個空窗口的簡單的Tkinter程序:AttributeError的:「STR」對象沒有屬性「關鍵字」

# firstTkinter2.py 
from Tkinter import Tk, Label 
top = Tk() 
l = Label(top, "Hello World") 
l.pack() 
# Give the window a title. 
top.title("My App") 
# Change the minimum size. 
top.minsize(400, 400) 
# Change the background colour. 
top.configure(bg = "green") 
# Run the widget. 
top.mainloop() 

我運行上面的代碼,但它遇到一個錯誤:

[email protected]:~/Documents/python programs/tkinter$ python tk2.py 
Traceback (most recent call last): 
    File "tk2.py", line 4, in <module> 
    l = Label(top, "Hello World") 
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2600, in __init__ 
    Widget.__init__(self, master, 'label', cnf, kw) 
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2094, in __init__ 
    for k in cnf.keys(): 
AttributeError: 'str' object has no attribute 'keys' 

我該如何解決這個錯誤並讓它正常運行?

回答

3

嘗試更換:

l = Label(top, "Hello World") 

l = Label(top, text="Hello World") 
#    ^^^^^^^ 
相關問題