Hey, I have a file that sets up several UI elements created with Tkinter (see below). When I create a progress bar (createProgressBar), which runs as a seperate thread, before creating any other element then everything is fine and runs as it should. If I create any element (i.e infoDialog) before creating a progress bar then the thread never closes. Can anyone help me figure this out?
Thanks
-bigssa
import Tkinter
import threading
import time
import sys
import tkMessageBox
import ScrolledText
class UIElements:
"""
User Interface Elements
"""
#---------------------------------------------------------------------------
def __init__ (self):
self.running = 0
#---------------------------------------------------------------------------
def createProgressBar (self, title):
# create the progress bar
if self.running == 1:
self.destroyProgressBar()
pb = Tkinter.Tk()
pb.title(title)
self.Bar = AsyncProgressBar(pb)
self.Bar.start()
self.running = 1
return self.Bar
#---------------------------------------------------------------------------
def destroyProgressBar (self):
# destroy the progress bar
self.Bar.StopProgressBar()
self.Bar.join()
self.running = 0
#---------------------------------------------------------------------------
def infoDialog (self, title, message):
# showinfo
if self.running == 1:
self.destroyProgressBar()
root = Tkinter.Tk()
root.withdraw()
tkMessageBox.showinfo(title, message)
root.destroy()
#---------------------------------------------------------------------------
def warningDialog(self, title, message):
# showwarning
if self.running == 1:
self.destroyProgressBar()
root = Tkinter.Tk()
root.withdraw()
tkMessageBox.showwarning(title, message)
root.destroy()
#---------------------------------------------------------------------------
def errorDialog(self, title, message):
# showerror
if self.running == 1:
self.destroyProgressBar()
root = Tkinter.Tk()
root.withdraw()
tkMessageBox.showerror(title, message)
root.destroy()
#---------------------------------------------------------------------------
def questionDialog(self, title, message):
# askyesno
if self.running == 1:
self.destroyProgressBar()
root = Tkinter.Tk()
root.withdraw()
return tkMessageBox.askyesno(title, message)
root.destroy()
#---------------------------------------------------------------------------
def messageDialog (self, title, message):
# multiline, scrollable message window with ScrolledText
if self.running == 1:
self.destroyProgressBar()
print message
#------------------------------------------------------------------------------#
class AsyncProgressBar(threading.Thread):
def __init__(self, Parent, Height=20, Width=200,
ForegroundColor=None,BackgroundColor=None,Progress=0):
threading.Thread.__init__(self)
self.Parent=Parent
self.Height=Height
self.Width=Width
self.BarCanvas = Tkinter.Canvas(self.Parent,
width=Width,height=Height,
background=BackgroundColor,borderwidth=1,
relief=Tkinter.SUNKEN)
if (BackgroundColor):
self.BarCanvas["backgroundcolor"]=BackgroundColor
self.BarCanvas.pack(padx=5,pady=2)
self.RectangleID=self.BarCanvas.create_rectangle(\
0,0,0,Height)
if (ForegroundColor==None):
ForegroundColor="black"
self.BarCanvas.itemconfigure(\
self.RectangleID,fill=ForegroundColor)
self.SetProgressPercent(Progress)
#---------------------------------------------------------------------------
def run(self):
self.IncrementProgress()
self.Parent.mainloop()
#---------------------------------------------------------------------------
def SetProgressPercent(self,NewLevel):
self.Progress=NewLevel
self.Progress=min(100,self.Progress)
self.Progress=max(0,self.Progress)
self.DrawProgress()
#---------------------------------------------------------------------------
def DrawProgress(self):
ProgressPixel=(self.Progress/100.0)*self.Width
self.BarCanvas.coords(self.RectangleID,
0,0,ProgressPixel,self.Height)
#---------------------------------------------------------------------------
def StopProgressBar(self):
self.Parent.destroy()
#---------------------------------------------------------------------------
def IncrementProgress(self):
OldLevel=self.Progress
if (OldLevel>99): OldLevel=0
self.SetProgressPercent(OldLevel+1)
self.Parent.after(1,self.IncrementProgress)
Thanks
-bigssa
import Tkinter
import threading
import time
import sys
import tkMessageBox
import ScrolledText
class UIElements:
"""
User Interface Elements
"""
#---------------------------------------------------------------------------
def __init__ (self):
self.running = 0
#---------------------------------------------------------------------------
def createProgressBar (self, title):
# create the progress bar
if self.running == 1:
self.destroyProgressBar()
pb = Tkinter.Tk()
pb.title(title)
self.Bar = AsyncProgressBar(pb)
self.Bar.start()
self.running = 1
return self.Bar
#---------------------------------------------------------------------------
def destroyProgressBar (self):
# destroy the progress bar
self.Bar.StopProgressBar()
self.Bar.join()
self.running = 0
#---------------------------------------------------------------------------
def infoDialog (self, title, message):
# showinfo
if self.running == 1:
self.destroyProgressBar()
root = Tkinter.Tk()
root.withdraw()
tkMessageBox.showinfo(title, message)
root.destroy()
#---------------------------------------------------------------------------
def warningDialog(self, title, message):
# showwarning
if self.running == 1:
self.destroyProgressBar()
root = Tkinter.Tk()
root.withdraw()
tkMessageBox.showwarning(title, message)
root.destroy()
#---------------------------------------------------------------------------
def errorDialog(self, title, message):
# showerror
if self.running == 1:
self.destroyProgressBar()
root = Tkinter.Tk()
root.withdraw()
tkMessageBox.showerror(title, message)
root.destroy()
#---------------------------------------------------------------------------
def questionDialog(self, title, message):
# askyesno
if self.running == 1:
self.destroyProgressBar()
root = Tkinter.Tk()
root.withdraw()
return tkMessageBox.askyesno(title, message)
root.destroy()
#---------------------------------------------------------------------------
def messageDialog (self, title, message):
# multiline, scrollable message window with ScrolledText
if self.running == 1:
self.destroyProgressBar()
print message
#------------------------------------------------------------------------------#
class AsyncProgressBar(threading.Thread):
def __init__(self, Parent, Height=20, Width=200,
ForegroundColor=None,BackgroundColor=None,Progress=0):
threading.Thread.__init__(self)
self.Parent=Parent
self.Height=Height
self.Width=Width
self.BarCanvas = Tkinter.Canvas(self.Parent,
width=Width,height=Height,
background=BackgroundColor,borderwidth=1,
relief=Tkinter.SUNKEN)
if (BackgroundColor):
self.BarCanvas["backgroundcolor"]=BackgroundColor
self.BarCanvas.pack(padx=5,pady=2)
self.RectangleID=self.BarCanvas.create_rectangle(\
0,0,0,Height)
if (ForegroundColor==None):
ForegroundColor="black"
self.BarCanvas.itemconfigure(\
self.RectangleID,fill=ForegroundColor)
self.SetProgressPercent(Progress)
#---------------------------------------------------------------------------
def run(self):
self.IncrementProgress()
self.Parent.mainloop()
#---------------------------------------------------------------------------
def SetProgressPercent(self,NewLevel):
self.Progress=NewLevel
self.Progress=min(100,self.Progress)
self.Progress=max(0,self.Progress)
self.DrawProgress()
#---------------------------------------------------------------------------
def DrawProgress(self):
ProgressPixel=(self.Progress/100.0)*self.Width
self.BarCanvas.coords(self.RectangleID,
0,0,ProgressPixel,self.Height)
#---------------------------------------------------------------------------
def StopProgressBar(self):
self.Parent.destroy()
#---------------------------------------------------------------------------
def IncrementProgress(self):
OldLevel=self.Progress
if (OldLevel>99): OldLevel=0
self.SetProgressPercent(OldLevel+1)
self.Parent.after(1,self.IncrementProgress)