AttributeError: object has no attribute 'tk'

前端 未结 1 1104
独厮守ぢ
独厮守ぢ 2021-01-24 09:59

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 i

相关标签:
1条回答
  • 2021-01-24 10:22

    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.

    0 讨论(0)
提交回复
热议问题