AttributeError:模块'_tkinter'没有属性'Frame'

问题描述 投票:0回答:2

im试图使用教程中复制的代码来运行一个简单的窗口。我收到标题中所述的错误,主类未获得对我的库tkinter的公认引用。代码有什么问题??我的文件名为buzz.py(我在代码文件的名称中看到了一些类似的问题。.

     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()
python tkinter window frame
2个回答
0
投票

如果使用Python 3,则需要不带tkinter_

Python 2中,您需要Tkinter带有较高的T,但也不需要_

非常重要也是缩进。


我假设您使用Python 3,所以您也可以使用

super().__init__(...) 

而不是

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

另请参见PEP 8 -- Style Guide for Python Code-import *


此代码对我有用

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

-2
投票

您应该这样做

Python 2-import Tkinter as tk

Python 3-import tkinter as tk

不是_tkinter

您可以阅读docs

© www.soinside.com 2019 - 2024. All rights reserved.