tkinter中的AttributeError

时间:2012-12-09 20:39:39

标签: python tkinter

我正在尝试使用tkinter编写程序。我还没有完成程序,但试图运行它只是为了看看我的窗口看起来如何,我在tkinter内部出错。

我不知道现在该做什么。谁知道错了什么?

这是消息

Traceback (most recent call last):
  File "<string>", line 420, in run_nodebug
  File "<module1>", line 53, in <module>
  File "<module1>", line 50, in main
  File "<module1>", line 23, in __init__
  File "C:\Python33\lib\tkinter\__init__.py", line 2110, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "C:\Python33\lib\tkinter\__init__.py", line 2036, in __init__
    classes = [(k, v) for k, v in cnf.items() if isinstance(k, type)]
AttributeError: 'str' object has no attribute 'items'

import tkinter
import tkinter.messagebox

#---------------------- define GUI class
class CalcMPG:
    def __init__(self):
        self.main_window = tkinter.Tk()

        #-------------- create 3 frames ---------
        self.uframe= tkinter.Frame(self.main_window) #upper frame
        self.mframe= tkinter.Frame(self.main_window) #middle frame
        self.bframe= tkinter.Frame(self.main_window) #button frame

        #------------ create the 3 label widgets ------------
        self.lblgal= tkinter.Label(self.uframe, text="Enter # of gallons used")
        self.lblmiles= tkinter.Label(self.mframe, text="Enter miles travelled")

        #------------ create the 2 Entry widgets ------------
        self.entgal= tkinter.Entry(self.uframe, width=10)
        self.entmiles= tkinter.Entry(self.mframe, width=10)

        #------------ create the 2 Button widgets -----------
        self.mpgbtn = tkinter.Button(self.bframe, "Calcualte MPG")
        self.extbtn = tkinter.Button(self.bframe, "Exit")


        #-------- pack upper frame -----------
        self.lblgal.pack(side='left')
        self.entgal.pack(side='right')

        #------- pack middle frame ----------
        self.lblmiles.pack(side='left')
        self.entmiles.pack(side='right')

        #------- pack bottom frome ----------
        self.mpgbtn.pack(side= 'left')
        self.extbtn.pack(side= 'right')


        #------- pack frames --------
        self.uframe.pack(side='top')
        self.mframe.pack(side='top')
        self.bframe.pack(side='top')


        tkinter.mainloop()

#--------------- define main function  ----
def main():
    calcmpg = CalcMPG()

#--------- invoke main function -------
main()

1 个答案:

答案 0 :(得分:4)

您需要创建这样的按钮,即明确指出这些字符串值是按钮的text属性的值,就像您对标签所做的那样:

self.mpgbtn = tkinter.Button(self.bframe, text="Calculate MPG")
self.extbtn = tkinter.Button(self.bframe, text="Exit")