AttributeError: object has no attribute 'tk'

六眼飞鱼酱① 提交于 2020-05-09 06:51:06

问题


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

Here is the error i get :

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.


回答1:


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

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

Perhaps you meant to use self.frame?

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.

Since you mentioned the source code....

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

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

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

self.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.



来源:https://stackoverflow.com/questions/38162028/attributeerror-object-has-no-attribute-tk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!