Python Forum

Full Version: New to Tkinter -- Does not recognize "Frame" as attribute.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am a novice with Python, and with Tk in particular. I have been taking an online course and am just getting into GUI using tkinter. I copied the attached code from the manual for the online course. The code is supposed to print a number, then either double it or halve it depending on the button pressed.

I am getting an error message indicating that "Frame" is not an attribute of tkinter. I must be missing something, but I can't find anything in the documentation that helps.

Any assistance would be appreciated.


import tkinter

class Application(tkinter.Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()

        self.increase_button = tkinter.Button(self)
        self.increase_button["text"] = "Increase"
        self.increase_button["command"] = self.increase_value
        self.increase_button.pack(side="right")

        self.increase_button = tkinter.Button(self)
        self.increase_button["text"] = "Decrease"
        self.increase_button["command"] = self.decrease_value
        self.increase_button.pack(side="left")

    def increase_value(self):
        global mainval
        mainval *= 2
        print (mainval)

    def decrease_value(self):
        global mainval
        mainval /= 2
        print (mainval)

mainval = 1.0
root = tkinter.Tk()
app = Application(master=root)
app.mainloop()
Error:
C:\Users\carys\AppData\Local\Programs\Python\Python37\python.exe "C:/Users/carys/My first project/tkinter.py" Traceback (most recent call last): File "C:/Users/carys/My first project/tkinter.py", line 1, in <module> import tkinter File "C:\Users\carys\My first project\tkinter.py", line 3, in <module> class Application(tkinter.Frame): AttributeError: module 'tkinter' has no attribute 'Frame' Process finished with exit code 1
The answer is simple: don't name your program tkinter.py. Rename it to something else. If there is still a file such as tkinter.pyc or tkinter.pyo in the same directory, remove it as well.
Thanks ever so much. I should have figured that out, but I'm still kind of a novice.