python - tkinter 中的属性错误

标签 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()

最佳答案

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

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

关于python - tkinter 中的属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13791583/

相关文章:

linux - 在 Tkinter 脚本中运行终端命令

Python2.6 回溯/Ubuntu wxPython

python - 根据一段时间内的平均值对面板数据框中的项目进行分类

python - 使用日期获取 DataFrame 中的异常值

python - 为什么我的按钮的命令在我创建按钮时立即执行,而不是在我单击它时执行?

python - 无法使用 cx_Freeze 在 Mac OS X Yosemite (10.10) 上构建独立应用程序

python - 在转换为 Dart 时维护 Python 行号

python - 在两个日期之间合并大型 Pandas 数据框的有效方法

python - 使用 itemcget 在 tkinter 中获取单个列表框项目属性?

python - 收到错误 AttributeError : '_tkinter.tkapp' object has no attribute 'getitems'