[Tkinter-discuss] background threading

Nemes Andrei teh_sh_meister at yahoo.com
Tue Apr 6 23:03:39 CEST 2010


Hi everyone!
I am trying to make an application in Tkinter that goes to Tray instead of closing. I have programmed the whole interface, including notifications in Tkinter. The problem is, I could not find any library to make the tray icon itself, so I built a tray icon class using wxPython that runs on a separate thread. So basically now, I'm withdrawing the main Tk window, and then showing the tray icon.
The core problem, however, is that while the system tray icon (aka wx main loop) is active, the main program built in Tk completely freezes, including it's notifications. So, my question is, how to make the tray icon thread run in the background, and still have my main program functional?

class trayIcon(object):
    def __init__(self):
        #setup app
        self.app=wx.PySimpleApp()

        #setup icon object
        self.actualIcon = wx.Icon(icon, wx.BITMAP_TYPE_ICO)

        #setup taskbar icon
        self.tbicon = wx.TaskBarIcon()
        self.tbicon.SetIcon(self.actualIcon, name)

        #add taskbar icon event
        wx.EVT_TASKBAR_RIGHT_UP(self.tbicon, self.right)
        wx.EVT_TASKBAR_LEFT_DCLICK(self.tbicon, self.left)

        self.app.MainLoop()      

    def right(self,event):
        global shouldQuit
        self.app.ExitMainLoop()
        shouldQuit=True

    def left(self,event):
        self.app.ExitMainLoop()        
        
class Thread(threading.Thread):
    def __init__(self, main):
        threading.Thread.__init__(self)
        self.setName("wx")
        self.main=main

    def run(self):
        self.Icon=trayIcon()


and here is how I defined it in the main program (must state that this is a class method itself):

def toTray(self):
        global shouldQuit
        self.main.withdraw()
        self.myTray=Thread(self.Main)
        self.myTray.start()
        while self.myTray.isAlive():
            pass

        if shouldQuit:
            self.Quit()
        else:
            self.main.deiconify()

I'm calling the isAlive() method to wait for the wx loop to be ended through it's controls, and then show the main window again; I know this holds up the function in place, but that should not hold the entire program should it?

I must state that using wx to do the entire interface would not be a choice, as I find it a lot harder to use than Tk.

Another solution to the problem would be pointing me to a more convenient tray icon constructor (instructions how to use would be greatly appreciated) if you know any.

I hope someone can help me. Thank you for your time and support.

Regards,
Andrei Nemes


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20100406/3aec5b29/attachment.html>


More information about the Tkinter-discuss mailing list