2017-08-11 33 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, text="Hello World") 
#    ^^^^^^^ 
l = Label(top, "Hello World") 

2

使用Label(top, text="Hello World")

あなたはtext引数を使用する必要があります。

関連する問題