简体   繁体   English

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

[英]Tkinter AttributeError: object has no attribute 'tk'

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. 我是tkinter的新手,这个错误让我感到难过。 I'd really appreciate any help, thank you! 非常感谢您的帮助,谢谢!

Your Page init method should call Frame 's init. 您的Page init方法应调用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM