Reputation: 39
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.
Upvotes: 0
Views: 654
Reputation: 386020
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.
Upvotes: 2