Tkinter AttributeError:对象没有属性“tk" [英] Tkinter AttributeError: object has no attribute 'tk'

查看:69
本文介绍了Tkinter AttributeError:对象没有属性“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()

这里是错误:

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 还很陌生,这个错误让我很难过.我真的很感激任何帮助,谢谢!

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

推荐答案

你的 Page init 方法应该调用 Frame 的 init.

Your Page init method should call Frame's 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)

这篇关于Tkinter AttributeError:对象没有属性“tk"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆