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

查看:547
本文介绍了AttributeError:对象没有属性"tk"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了很多,但是找不到解决方案.我正在尝试使用tkinter创建一个注册表,以后我将连接到数据库.这是代码:

I have searched quite a bit,but I couldn't find a solution to this. I'm trying to create a registration form using tkinter which later on i shall connect to a database. Here is the code :

from Tkinter import *


class MWindow(object):

    def __init__(self,master):

        self.frame=Frame(master)
        self.frame.pack()

        self.title= Label(self,text = "Login")
        self.title.grid(row=0,column=1)

        self.userid_label = Label(self,text ="Username: ")
        self.userid_label.grid(row=1,column=0)

        self.userid_entry= Entry(self)
        self.userid_entry.grid(row=1,column=1)

        self.password_label = Label(self,text ="Password: ")
        self.password_label.grid(row=2,column=0)

        self.password_entry= Entry(self)
        self.password_entry.grid(row=2,column=1)

        self.signin = Button (self,text = "Login",command=logging_in)
        self.signin.grid(row=5,column=1)

        self.signup = Button (self,text = "Sign Up",command=signing_up)
        self.signin.grid(row=5,column=2)

    def logging_in(self):
        pass
    def signing_up(self):
        pass

root= Tk()
root.attributes('-fullscreen',True)
root.resizable(width=False, height=False)
root.title("My Registration Form")
app=MWindow(root)
root.mainloop()

这是我得到的错误:

回溯(最近通话最近):

中的文件"form.py",第41行 app = MWindow(root)
init
中的文件"form.py",第11行 self.title =标签(self,text =登录")
init
中的文件"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",第2591行 窗口小部件.初始化(自我,母版,标签",cnf,kw)
init
中的文件"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",第2081行 BaseWidget._setup(self,master,cnf)
_setup中的文件"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",第2059行
self.tk = master.tk
AttributeError:"MWindow"对象没有属性"tk"

Traceback (most recent call last):
File "form.py", line 41, in
app=MWindow(root)
File "form.py", line 11, in init
self.title= Label(self,text = "Login")
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2591, in init
Widget.init(self, master, 'label', cnf, kw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2081, in init
BaseWidget._setup(self, master, cnf)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2059, in _setup
self.tk = master.tk
AttributeError: 'MWindow' object has no attribute 'tk'

我试图去库文件来了解问题所在,但是作为一个初学者,我做不到很多.对发生问题的原因和原因进行一些解释将非常有帮助.

I tried going to the library files to understand what's wrong,but being a beginner i can't make much of it. Some explanation of what's going wrong and why would be very helpful.

推荐答案

您要将self作为主/父级传递给小部件.

You're passing self in as the master / parent to your widgets.

例如-Entry(self, ...)但是,您的类MWindow并非继承自Tkinter小部件.

e.g - Entry(self, ...) But, your class MWindow doesn't inherit from a Tkinter widget.

也许您打算使用self.frame?

如果您确实要使用 self ,则可以执行以下操作:

If you really want to use self you could do this:

import Tkinter as tk

...

class MWindow(tk.Frame):

   def __init__(self, master, *args, **kwargs):

       tk.Frame.__init__(self, master, *args, **kwargs)
       abutton = tk.Button(self, ....)

如果这令人困惑,那么这是一个很好的答案.

If this is confusing, then here's a pretty good answer.

自从您提到源代码....

查看Tk()类.其中包含以下行:

Look at the Tk() class. Which contains the following line:

self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) 

现在,检查所有Widget继承自的BaseWidget类.这包含以下行:

Now, check out the BaseWidget class which all Widget's inherit from. This contains the following line:

self.tk = master.tk 

您有一个基本根窗口Tk(),该窗口具有属性tk,并且该集合的每个子级都有一个属性tk作为master的tk属性.对于嵌套的小部件,依此类推,因为小部件的父级可以只是另一个小部件,所以它不必一定是根窗口.

You have you're base root window Tk() which has the attribute tk and every child of this set's an attribute tk to be the master's tk attribute. So on and so forth for nested widgets, since the parent of a widget could just be another widget it doesn't have to be the root window of course.

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

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