2015-01-14 80 views
2

我查了一下,但找不到我错误的答案。下面是代码:Tkinter AttributeError:对象没有属性'tk'

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

以下是错误:

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' 

我是相当新的Tkinter的,而这个错误我难住了。我非常感谢任何帮助,谢谢!

回答

8

您的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) 
+0

非常感谢,但是一般使用'super'究竟是什么? – PlatypusVenom

+1

@PlatypusVenom http://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods – Scironic

+1

['super'](https://docs.python.org/3/library/functions.html #super)通常用于访问属于给定对象的父类的方法。在这里,'super(Page,self)'返回一个Frame类似的'self'的代理,并且调用'__init __()'调用'Frame .__ init __()'。 – Kevin

相关问题