Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError in tkinter

Tags:

python

tkinter

I am trying to write a program with tkinter. I have not finished the program but trying to run it just to see how my window will look, I get an error inside of tkinter.

I am not sure what to do now. Anybody know what it wrong?

Here is the message

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()
like image 894
danny Frediani Avatar asked Dec 09 '12 20:12

danny Frediani


People also ask

What is an AttributeError in Python?

AttributeError can be defined as an error that is raised when an attribute reference or assignment fails. For example, if we take a variable x we are assigned a value of 10. In this process suppose we want to append another value to that variable. It's not possible.

How do I fix AttributeError in Python?

Attribute errors in Python are raised when an invalid attribute is referenced. To solve these errors, first check that the attribute you are calling exists. Then, make sure the attribute is related to the object or data type with which you are working.

What is Tk StringVar ()?

StringVar() is a class from tkinter. It's used so that you can easily monitor changes to tkinter variables if they occur through the example code provided: def callback(*args): print "variable changed!" var = StringVar() var.trace("w", callback) var. set("hello")

How do you use Customtkinter?

To use CustomTkinter, just place the /customtkinter folder from this repository next to your program, and then you can do import customtkinter .


1 Answers

You need to create the buttons like this, i.e. specify explicitely that those string values are the values of the text property of the button, just as you did with the labels:

self.mpgbtn = tkinter.Button(self.bframe, text="Calculate MPG")
self.extbtn = tkinter.Button(self.bframe, text="Exit")
like image 171
poke Avatar answered Sep 24 '22 13:09

poke