Pieter De Vleugel
Pieter De Vleugel

Reputation: 9

AttributeError: module '_tkinter' has no attribute 'Frame'

im trying to run a simple window using some copied code from a tutorial. i get an error as described in title, the main class is not getting a recognized reference to my library tkinter.. whats wrong with the code?? my file name is buzz.py (i saw some similar problems where the problem was in the name of the file of the code..

     import _tkinter as tk
        from _tkinter import *
        class Example(tk.Frame):
            def __init__(self, parent):
            tk.Frame.__init__(self, parent)
            # create a prompt, an input box, an output label,
            # and a button to do the computation
            self.prompt = tk.Label(self, text="Enter a number:", anchor="w")
            self.entry = tk.Entry(self)
            self.submit = tk.Button(self, text="Submit", command = self.calculate)
            self.output = tk.Label(self, text="")

            # lay the widgets out on the screen.
            self.prompt.pack(side="top", fill="x")
            self.entry.pack(side="top", fill="x", padx=20)
            self.output.pack(side="top", fill="x", expand=True)
            self.submit.pack(side="right")

        def calculate(self):
            # get the value from the input widget, convert
            # it to an int, and do a calculation
            try:
                i = int(self.entry.get())
                result = "%s*2=%s" % (i, i*2)
            except ValueError:
                result = "Please enter digits only"

            # set the output widget to have our result
            self.output.configure(text=result)

    # if this is run as a program (versus being imported),
    # create a root window and an instance of our example,
    # then start the event loop

        if __name__ == "__main__":
            root = tk.Tk()
            Example(root).pack(fill="both", expand=True)
            root.mainloop()

Upvotes: 0

Views: 1302

Answers (1)

furas
furas

Reputation: 143187

If you use Python 3 then you need tkinter without _.

In Python 2 you would need Tkinter with upper T but also without _.

Very imporatant are also indentations.


I assume you use Python 3 so you can also use

super().__init__(...) 

instead of

tk.Frame.__init__(self, ...)

See also PEP 8 -- Style Guide for Python Code - import * is not preferred.


This code works for me

import tkinter as tk # Python 3
# from tkinter import * # PEP8: `import *` is not preferred

class Example(tk.Frame):

    def __init__(self, parent):
        super().__init__(parent)
        self.prompt = tk.Label(self, text="Enter a number:", anchor="w")
        self.entry = tk.Entry(self)
        self.submit = tk.Button(self, text="Submit", command = self.calculate)
        self.output = tk.Label(self, text="")

        self.prompt.pack(side="top", fill="x")
        self.entry.pack(side="top", fill="x", padx=20)
        self.output.pack(side="top", fill="x", expand=True)
        self.submit.pack(side="right")

    def calculate(self):
        try:
            i = int(self.entry.get())
            result = "%s*2=%s" % (i, i*2)
        except ValueError:
            result = "Please enter digits only"

        self.output.configure(text=result)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

Upvotes: 1

Related Questions