Python Tkinter AttributeError:对象没有属性“tk”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27946030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:30:00  来源:igfitidea点击:

Tkinter AttributeError: object has no attribute 'tk'

pythonpython-3.xtkinter

提问by PlatypusVenom

I've looked around a bit, but I can't find an answer to my error. Here is the code:

我环顾四周,但找不到我的错误的答案。这是代码:

import tkinter as tk

root=tk.Tk()

class Page(tk.Frame):
    '''Enables switching between pages of a window.'''
    def __init__(self):
        self.widgets={}
        self.grid(column=0,row=0)

page=Page()

tk.mainloop()

Here is the error:

这是错误:

Traceback (most recent call last):  
  File "C:\Documents and Settings\Desktop\Python Scripts\Tkinter.py", line 11, in <module>  
    page=Page()  
  File "C:\Documents and Settings\Desktop\Python Scripts\Tkinter.py", line , in __init__  
    self.grid(column=0,row=0)  
  File "C:\Python34\lib\tkinter\__init__.py", line 2055, in grid_configure  
    self.tk.call(  
AttributeError: 'Page' object has no attribute 'tk'

I'm fairly new to tkinter, and this error has me stumped. I'd really appreciate any help, thank you!

我对 tkinter 还很陌生,这个错误让我很难过。我真的很感激任何帮助,谢谢!

采纳答案by Kevin

Your Pageinit method should call Frame's init.

您的Pageinit 方法应该调用Frame的 init。

class Page(tk.Frame):
    '''Enables switching between pages of a window.'''
    def __init__(self):
        super(Page, self).__init__()
        self.widgets={}
        self.grid(column=0,row=0)