Вопрос

i write a hello world app in tkinter python, but i recive next message: 'module' object has no attribute 'Frame'

import _tkinter as tk

here is the error

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.pack()
        self.createWidgets()
    def createWidgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.QUIT = tk.Button(self, text="QUIT", fg="red",
                                        command=root.destroy)
        self.QUIT.pack(side="bottom")

    def say_hi(self):
        print("hi there, everyone!")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

why this are happening?

Это было полезно?

Решение 2

You should use Tkinter (tkinter if you use Python 3.x), not _tkinter:

import Tkinter as tk

According to Tkinter module documentation:

... The Tk interface is located in a binary module named _tkinter. This module contains the low-level interface to Tk, and should never be used directly by application programmers.

Другие советы

Don't call your file as tkinter.py, rename if it is necessary.

The solution here is to use the correct syntax for the associated Python version.

Tkinter >> Python 2.x

tkinter >> Python 3.x

Despite this, I had errors because I had called my file tkinter.py, and was presented with the error:

module 'tkinter' has no attribute 'Frame'

Once I'd renamed my file to something else entirely, in my case I chose tk-testing.py it was fine in both Python 2.x and Python 3.x, whilst using the correct naming conventions above.

Python 2.x

import Tkinter as tk

Python 3.x

import Tkinter as tk
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top