2015-07-11 174 views
0

我是Tkinter的初學者。我正在嘗試製作電話簿GUI應用程序。Python - AttributeError:'str'對象沒有屬性'items'

所以,我只是在開始的一步,這是我的源代碼:

#This is my python 'source.py' for learning purpose 

from tkinter import Tk 
from tkinter import Button 
from tkinter import LEFT 
from tkinter import Label 
from tkinter import Frame 
from tkinter import Pack 

wn = Tk() 
f = Frame(wn) 

b1 = Button(f, "One") 
b2 = Button(f, "Two") 
b3 = Button(f, "Three") 

b1.pack(side=LEFT) 
b2.pack(side=LEFT) 
b3.pack(side=LEFT) 

l = Label(wn, "This is my label!") 

l.pack() 
l.pack() 

wn.mainloop() 

正如我跑,我的程序提供了以下錯誤:

/usr/bin/python3.4 /home/rajendra/PycharmProjects/pythonProject01/myPackage/source.py 
Traceback (most recent call last): 
    File "/home/rajendra/PycharmProjects/pythonProject01/myPackage/source.py", line 13, in <module> 
    b1 = Button(f, "One") 
    File "/usr/lib/python3.4/tkinter/__init__.py", line 2164, in __init__ 
    Widget.__init__(self, master, 'button', cnf, kw) 
    File "/usr/lib/python3.4/tkinter/__init__.py", line 2090, in __init__ 
    classes = [(k, v) for k, v in cnf.items() if isinstance(k, type)] 
AttributeError: 'str' object has no attribute 'items' 

Process finished with exit code 1 

任何人都可以請讓我知道這裏有什麼問題嗎?

幫助會很高興!

回答

2

你需要說tkinter,那些是什麼"One","Two"等。

Button(f, text="One") 
Label(wn, text="This is my label!") 

要回答你爲什麼要這樣做,你應該檢查函數和參數如何在python中工作。

而且,你可能要收拾你Frame,因爲你上的所有按鈕,你可以使用"left"代替tkinter.LEFT

+0

因此,我做到了,但'不luck' :( –

+1

謝謝..最後的工作!: )我會在2分鐘後接受你的回答。 :) –

+0

@Bjarnestroustoup你不完全是Bjarne,如果你沒有發現你自己:D – nbro

相關問題