5

我正在尝试用 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()
4

1 回答 1

5

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

self.mpgbtn = tkinter.Button(self.bframe, text="Calculate MPG")
self.extbtn = tkinter.Button(self.bframe, text="Exit")
于 2012-12-09T20:42:51.243 回答