'Frame' has no attribute 'tk' - Python Thinter
'Frame' has no attribute 'tk' - Python Thinter
I try resolve my problem with this message from PyCharm and Idle.
'type object 'Frame' has no attribute 'tk''
What this mean? Why Python don't show new window with 3 Buttons?
Thank you very much for your help!
from tkinter import*
class Application(Frame):
def __init__(self, master):
""" Frame """
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""Buttons"""
#Buttons1
self.bttn1 = Button("Test1")
self.bttn1.grid()
#Buttons2
self.aaaa2 = Button(self)
self.aaaa2.grid()
self.aaaa2.configure(text="Test2")
#Buttons3
self.bttn3 = Button (self)
self.bttn3.grid()
self.bttn3["text"] = "Test3"
#Main part of the program
root = Tk()
root.title("Name Window")
root.geometry("210x85")
Application(Frame)
root.mainloop()
1 Answer
1
Your code has a class called Application
which sublcasses tkinter.Frame
. On initialization, tkinter.Frame
accepts a master
argument, which it then inspects for the attribute tk
.
Application
tkinter.Frame
tkinter.Frame
master
tk
When you initialize the class Application
, it takes the argument you apply (Frame
) and passes it to its superclass tkinter.Frame
(which I already explained inspects the object for a tk
attribute). Frame.tk
does not exist, so it raises an error.
Application
Frame
tkinter.Frame
tk
Frame.tk
An alternative, simpler example for edification:
class MyClass():
def __init__(self,value):
self.key = value.key
MyClass(None)
The above code raises the same error (obviously with different names) because it tries to get the value of None.key
and None
does not have an attribute called key
.
None.key
None
key
As for how you display your frame with 3 buttons, you need to pass root
as the master of your new Application
instance. This will add the instance to root
(the TopLevel/Tk Window) and then you need to set the geometry manager for that Application
instance (pack, grid, or place).
root
Application
root
Application
Furthermore, you are going to run into the same exact error with bttn1
because you are passing "Test1"
as the master of tkinter.Button
(and strings don't have an attribute tk
).
bttn1
"Test1"
tkinter.Button
tk
Finally, this question has been asked a lot (especially on this site), so please in the future make a google search before you ask.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment