Im trying to make a tkinter window but this shows up for some reason, i really dont know what i did wrong
root.title('RNG')
root.geometry('300x300')
button = Button(root, text = 'talk', bd = '5',)
button.pack(side = 'top')
text = Text(master=BOTTOM)
text.pack(side=BOTTOM)
the error:
File "/home/valtok/PycharmProjects/tests/waifu simulator/RNG.py", line 65, in <module>
text = Text (master=BOTTOM)
File "/usr/lib/python3.8/tkinter/__init__.py", line 3554, in __init__
Widget.__init__(self, master, 'text', cnf, kw)
File "/usr/lib/python3.8/tkinter/__init__.py", line 2561, in __init__
BaseWidget._setup(self, master, cnf)
File "/usr/lib/python3.8/tkinter/__init__.py", line 2530, in _setup
self.tk = master.tk
AttributeError: 'str' object has no attribute 'tk'
Instead of this:
text = Text(master=BOTTOM)
Write this:
text = Text(master=root)
Related
This question already has answers here:
AttributeError: 'str' object has no attribute 'tk' in tkinter Label
(2 answers)
Closed 10 months ago.
Here is my code:
window = tk.Tk()
version = tk.Label(text="hi",
fg="orange",
bg="black",
width=20,
height=10,
)
version.pack()
enter = tk.Button(text="START",fg="blue",width=20,height=7)
enter.pack()
def click(event):
version.forget()
enter.forget()
name = tk.Label("name")
entry = tk.Entry()
name.pack()
entry.pack()
enter.bind("<Button-1>", click)
window.mainloop()
As you can see, I wanted to make a Label and an Entry appear once the START button is clicked.
When I click the button, though, it returns this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
File "/Users/name/Desktop/project/gui/main.py", line 19, in click
name = tk.Label("name")
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 3148, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 2566, in __init__
BaseWidget._setup(self, master, cnf)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 2535, in _setup
self.tk = master.tk
AttributeError: 'str' object has no attribute 'tk'
I don't know what is happening here.
Per the error statement, the origin of the problem is this line:
File "/Users/name/Desktop/project/gui/main.py", line 19, in click
name = tk.Label("name")
The first expected argument of a Label is the "master", or parent window.
This question already has answers here:
_tkinter.TclError: image "..." doesn't exist
(3 answers)
Closed 4 years ago.
Here is part of my code called within a function:
#Labels and Window layout
lsfpy = Tk()
lsfpy.title("Helicopters Sydney")
lsfpy.resizable(False, False)
Label(lsfpy, text="Locations in Sydney").grid(row=0)
Label(lsfpy, text="To").grid(column = 1, row=1, sticky=N)
Label(lsfpy, text="From").grid(column = 1, row=2, sticky = W)
Label(lsfpy, text="").grid(column = 1, row=3)
Label(lsfpy, text="Date").grid(column = 1, row=4, sticky=SW)
Label(lsfpy, text="Time").grid(column = 1, row=5, sticky=SW)
#Map
photo = PhotoImage(file = 'GUI Files/Map/Sydmap.gif')
photo = photo.subsample(2)
lbl = Label(lsfpy,image = photo)
lbl.grid(column=0, row=3)
When I run it, I get this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1702, in __call__
return self.func(*args)
File "/Users/62633/Documents/2018/SDD/Webdrone Sydney/Freight.py", line 22, in calculateandnext
saveandgotomapf(tp,am1,am2,am3,am4,am5)
File "/Users/62633/Documents/2018/SDD/Webdrone Sydney/Freight.py", line 55, in saveandgotomapf
locationfreight(fdpy)
File "/Users/62633/Documents/2018/SDD/Webdrone Sydney/Locationfreight.py", line 192, in locationfreight
lbl = Label(lsfpy,image = photo)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2763, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2296, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage4" doesn't exist
When I comment out
photo = photo.subsample(2)
The error slightly changes to:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1702, in __call__
return self.func(*args)
File "/Users/62633/Documents/2018/SDD/Webdrone Sydney/Freight.py", line 22, in calculateandnext
saveandgotomapf(tp,am1,am2,am3,am4,am5)
File "/Users/62633/Documents/2018/SDD/Webdrone Sydney/Freight.py", line 55, in saveandgotomapf
locationfreight(fdpy)
File "/Users/62633/Documents/2018/SDD/Webdrone Sydney/Locationfreight.py", line 192, in locationfreight
lbl = Label(lsfpy,image = photo)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2763, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2296, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage3" doesn't exist
If I copy the code snippet into a new file, there are no problems.
What is causing these errors?
In your recent edit, you mentioned the code was in a function, that made all the difference.
The PhotoImage is not kept by tkinter, so you must keep a reference to it before Python's garbage collector gobbles up the image after the function returns. When it does, tkinter couldn't find your image anymore, thus your error saying that the image doesn't exist.
As recommended if effbot, you can do:
photo = PhotoImage(file = 'GUI Files/Map/Sydmap.gif')
photo = photo.subsample(2)
lbl = Label(lsfpy,image = photo)
lbl.image = photo
lbl.grid(column=0, row=3)
You must keep a reference to the image object in your Python program,
either by storing it in a global variable, or by attaching it to
another object.
Note: When a PhotoImage object is garbage-collected by
Python (e.g. when you return from a function which stored an image in
a local variable), the image is cleared even if it’s being displayed
by a Tkinter widget. To avoid this, the program must keep an extra
reference to the image object. A simple way to do this is to assign
the image to a widget attribute, like this:
label = Label(image=photo)
label.image = photo # keep a reference! label.pack()
I am writing my own Library, so I can use some functions later faster and easier. At the moment, I am working with python's GUI Library Tkinter. (from tkinter include *)
def guiFrameNew(title, width, height):
guitmp = Tk();
return guitmp;
def guiTextboxReadonlyNew(frame, width, text):
guitmp = Entry(Frame, state="readonly", textvariable=text, width=width);
guitmp.pack();
return guitmp;
def guiFrameRun(frame):
frame.mainloop();
This all is in one file (file_one.py).
In an other file (file_two.py) i included this file:
include file_one as f
Code in file_two is:
main = f.guiFrameNew("Test", 0, 0);
main_tbro = f.guiTextboxReadonlyNew(main, 20, "Some Text");
f.guiFrameRun(main);
Yes, I know that I don't need the values Title, width, height in def guiFrameNew because the function does not create a frame.
After I started the file_two.py the python Interpreter says:
> File "file_two", line 5, in <module>
> main_tbro = f.guiTextboxReadonlyNew(main, 20, "Some Text"); File "/Users/MyUsername/Documents/py/file_two.py", line 190, in
> guiTextboxReadonlyNew
> guitmp = Entry(Frame, state="readonly", textvariable=text, width=width); File
> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
> line 2447, in __init__
> Widget.__init__(self, master, 'entry', cnf, kw) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
> line 2027, in __init__
> BaseWidget._setup(self, master, cnf) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
> line 2005, in _setup
> self.tk = master.tk AttributeError: class Frame has no attribute 'tk'
I don't know why, because the function def guiTextboxReadonlyNew(...) is similar to the function
def guiTextboxNew(frame, width):
guitmp = Entry(frame, width=width);
guitmp.pack();
return guitmp;
and def guiTextboxNew(...) works!
What is wrong in my file?
Assumming by include you mean import (which is really the case, since you are able to import the module file_one).
The Entry() takes a frame object as the first argument, not the Frame class. You should do -
def guiTextboxReadonlyNew(frame, width, text):
guitmp = Entry(frame, state="readonly", textvariable=text, width=width)
guitmp.pack()
return guitmp
Also, there is not really any need for ; (semi-colon) in python after statements.
I actually have a big problem.
I already have a tkinter window and I want to open an other.
import Tkinter
from Tkinter import *
import threading, time
from PIL import Image, ImageTk
from record_pd import *
class Gui_Record(Tkinter.Tk):
def __init__(self, tkroot):
self.root = Tk()
self.root.title("Enregistreur")
#self.root.geometry()
self.root.geometry("%dx%d+%d+%d" % (500, 70, 400, 300))
self.root.c = Canvas(tkroot, bg='black')
self.root.c.pack(fill=BOTH, expand=YES)
self.initialize()
self.recorder = RecordPd(tkroot)
self.recorder.init_recorder()
def initialize(self):
#self.root.grid()
self.root.resizable(False, False)
self.Imgtmp = PhotoImage(file="img/record.png")
self.imgclear = PhotoImage(file="img/clear.png")
self.root.title = Tkinter.Label(self.root, text="Enregistreur Orgue Sensoriel", bg="black", fg="white", font=("Helvetica", 16))
self.root.title.pack()
self.root.button = Tkinter.Button(self, command=self.OnButtonClick, bg="black", bd=0)
self.root.button.config(highlightthickness=0)
self.root.button.config(activebackground="black")
self.root.button.config(image=self.Imgtmp)
self.root.button.pack()
self.root.bind("<Destroy>", self._onDestroy)
self.resume = True
self.activate = False
def setTkroot(self, tkroot):
self.tkroot = tkroot
def _onDestroy(self, e):
self.resume = False
self.recorder.stop_recording()
def OnButtonClick(self):
if (self.activate == False):
self.resume = True
self.recorder.open_wav()
self.recorder.start_recording()
thread = threading.Thread(target=self.threadClignoter)
thread.start()
self.activate = True
print("In recording..")
else:
self.stopThread()
self.recorder.stop_recording()
self.activate = False
def threadClignoter(self):
isVisible = True
while self.resume:
if isVisible:
clr = self.imgclear
else:
clr = self.Imgtmp
self.root.button.config(image=clr)
isVisible = not isVisible
time.sleep(0.5)
def stopThread(self):
print("Record done.")
self.resume = False
self.root.button.config(image=self.Imgtmp)
When I call my object I do:
rec = Gui_Record(self.tkroot)
rec.mainloop()
When I launch a single window it's okay. But when i add my new window to my parent window it happened that:
traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
File "/home/naqued/Documents/assembla/backup/naqued-s-space/stido/gui_stido.py", line 139, in launch_recorder
app = Gui_Record(self.tkroot)
File "/home/naqued/Documents/assembla/backup/naqued-s-space/stido/record_gui.py", line 18, in __init__
self.initialize()
File "/home/naqued/Documents/assembla/backup/naqued-s-space/stido/record_gui.py", line 35, in initialize
self.root.button = Tkinter.Button(self, command=self.OnButtonClick, bg="black", bd=0)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2128, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2049, in __init__
BaseWidget._setup(self, master, cnf)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2022, in _setup
if not master:
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1848, in __getattr__
return getattr(self.tk, attr
.... ... ... ... .. .. ...
return getattr(self.tk, attr)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1848, in __getattr__
return getattr(self.tk, attr)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1848, in __getattr__
return getattr(self.tk, attr)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1848, in __getattr__
return getattr(self.tk, attr)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1848, in __getattr__
return getattr(self.tk, attr)
RuntimeError: maximum recursion depth exceeded while calling a Python object
I don't do recursion on my code.
I don't know what happened and didn't find anything in the web.
You are creating a class that inherits from Tk, but it also creates a new instance of Tk, and even though you don't show it you're also creating another root at some point (the object being passed in as tkroot) I'm not sure if that's the only problem, but it's definitely a problem.
Since this is a secondary window, you shouldn't be inheriting from Tkinter.Tk. Instead, inherit from Tkinter.Toplevel
You also have the problem that even though this creates a new window as a child of tkroot, some of the internal widgets are being created as children of tkroot so they won't appear in this window.
You also need to fix your imports -- you shouldn't be doing a global import from Tk and also importing Tk as a module.
You're likely going to have other problems. Tkinter doesn't work well with threads. I've heard that it sometimes works on linux, but in general you should never call any GUI function from any thread other than the one in which the widget was created.
I am a newbie to python,I tried a Gui Application,but its resulting in error like:
Errors:
Traceback (most recent call last):
File "C:\Python27\aqw.py", line 22, in <module>
app = myproject(None,None)
File "C:\Python27\aqw.py", line 8, in __init__
self.button()
File "C:\Python27\aqw.py", line 13, in button
button = Tkinter.Button(self,text=u"Succedd !")
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2106, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2027, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2000, in _setup
if not master:
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1826, in __getattr__
return getattr(self.tk, attr)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1826, in __getattr__
return getattr(self.tk, attr)
Please help me to fix my code!
My code:
import Tkinter
from Tkinter import *
class myproject(Tkinter.Tk):
def __init__(self,parent, master):
self.button()
self.checkbox()
self.radiobutton()
def button(self):
#add quit button
button = Tkinter.Button(self,text=u"Succedd !")
button.grid(column=3,row=1)
def checkbox(self):
checkbox = Checkbutton(self, text = "Music", variable = CheckVar2)
checkbox.grid(column=3,row=1)
def radiobutton(self):
radiobutton = Tkinter.Radiobutton(self, text="Option 2", variable=var, value=2)
app = myproject(None,None)
app.mainloop()
Please help!Answers will be appreciated!
You need to call super class' __init__ method:
class myproject(Tkinter.Tk):
def __init__(self, parent, master):
Tkinter.Tk.__init__(self) # <----
self.button()
self.checkbox()
self.radiobutton()
...
In addition to that, there's undefined variables CheckVar2, var.