tkin中的AttributeError

2024-09-30 12:18:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用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()

Tags: inselfinitmaintkintercreatelinewindow
1条回答
网友
1楼 · 发布于 2024-09-30 12:18:37

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

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

相关问题 更多 >

    热门问题