Tkinter not recognizing any type of function
Question:
import tkinter
import customtkinter
tk = tkinter.Tk(className='Tkinter - TutorialKart', )
tk.geometry("500x300")
def submitFunction() :
q1 = tk.Label(text="Hello, Tkinter")
q1.pack
button_submit = tkinter.Button(tk, text ="Submit", command=submitFunction)
button_submit.config(width=20, height=2)
button_submit.pack()
tk.mainloop()
As you can see by running the code and editing the variable q1
to any other function it will not work, I get this error:
AttributeError: '_tkinter.tkapp' object has no attribute 'Label'
Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersUSERAppDataLocalProgramsPythonPython310libtkinter__init__.py", line 1921, in __call__
return self.func(*args)
File "c:UsersUSEROneDrive - Bristol Virginia Public SchoolsDesktopCurrent projectsmain.py", line 8, in submitFunction
q1 = tk.Label(text="Hello, Tkinter")
File "C:UserstpitcockAppDataLocalProgramsPythonPython310libtkinter__init__.py", line 2383, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'Label'
I tried changing text to different things but it didn’t work.
I’m using Python 3.
Answers:
what is your goal? Do you just want to make a window? If yes, first you need to import all of the classes in tkinter with from tkinter import *
. And after that you can make an object from Tk class: Favorite name = Tk()
. Then you can access all functions and classes in tkinter using this object. The code below is a simple design of a window:
from tkinter import *
window = Tk()
window.title("your favorite")
window.geometry("700x700")
.
.
.
window.mainloop()
The error is telling you what is wrong. It says that tk
doesn’t have an attribute named Label
, which is true. In your code, tk
isn’t the tkinter module, it’s the instance of Tk
that you created with this line:
tk = tkinter.Tk(className='Tkinter - TutorialKart', )
If you want to use the tkinter Label
class, it needs to come from the tkinter
module just like you do with tkinter.Tk
and tkinter.Button
:
q1 = tkinter.Label(text="Hello, Tkinter")
# ^^^^^^^
import tkinter
import customtkinter
tk = tkinter.Tk(className='Tkinter - TutorialKart', )
tk.geometry("500x300")
def submitFunction() :
q1 = tk.Label(text="Hello, Tkinter")
q1.pack
button_submit = tkinter.Button(tk, text ="Submit", command=submitFunction)
button_submit.config(width=20, height=2)
button_submit.pack()
tk.mainloop()
As you can see by running the code and editing the variable q1
to any other function it will not work, I get this error:
AttributeError: '_tkinter.tkapp' object has no attribute 'Label'
Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersUSERAppDataLocalProgramsPythonPython310libtkinter__init__.py", line 1921, in __call__
return self.func(*args)
File "c:UsersUSEROneDrive - Bristol Virginia Public SchoolsDesktopCurrent projectsmain.py", line 8, in submitFunction
q1 = tk.Label(text="Hello, Tkinter")
File "C:UserstpitcockAppDataLocalProgramsPythonPython310libtkinter__init__.py", line 2383, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'Label'
I tried changing text to different things but it didn’t work.
I’m using Python 3.
what is your goal? Do you just want to make a window? If yes, first you need to import all of the classes in tkinter with from tkinter import *
. And after that you can make an object from Tk class: Favorite name = Tk()
. Then you can access all functions and classes in tkinter using this object. The code below is a simple design of a window:
from tkinter import *
window = Tk()
window.title("your favorite")
window.geometry("700x700")
.
.
.
window.mainloop()
The error is telling you what is wrong. It says that tk
doesn’t have an attribute named Label
, which is true. In your code, tk
isn’t the tkinter module, it’s the instance of Tk
that you created with this line:
tk = tkinter.Tk(className='Tkinter - TutorialKart', )
If you want to use the tkinter Label
class, it needs to come from the tkinter
module just like you do with tkinter.Tk
and tkinter.Button
:
q1 = tkinter.Label(text="Hello, Tkinter")
# ^^^^^^^