[Twisted-Python] My first wxPython App with Twisted

Thalassery Koran koranthala at gmail.com
Tue Aug 4 15:28:33 EDT 2009

    I am creating the first GUI app (wxPython based) using Twisted.
    I am facing an issue which I cannot seem to solve/understand due
to lack of trace or anything.

    I had a big app written on twisted and now I am creating a login
screen to the app.
    The app used to work without any issues. I added the login screen
and the login screen comes up.
     But then everything freezes up. I have to force quit the python
application to get anything back. Can anyone help me solve it?

    The code is as follows:

import wx
import os
import logging

from twisted.internet import wxreactor
from twisted.internet import reactor #Called only after wxreactor.install()
from twisted.internet.task import LoopingCall

import clientapp

def opj(path):
    """ Convert paths to the platform-specific separator """
    st = apply(os.path.join, tuple(path.split('/')))
    if path.startswith('/'):
        st = '/' + st
    return st

class APPApp(wx.App):
    def OnInit(self):
        self.frame = APPFrame(None, -1, " APP Login", size=(200, 150))
        self.panel = APPPanel(self.frame,-1)
        client = clientapp.setup_app()
        call = LoopingCall(client.run)
        return True

def main():
    app = APPApp(0)
    except StandardError:

class APPPanel(wx.Panel):

    The panel in which the user name and password is typed.
    @var ID_LOGIN: Login buttons id. When this button is clicked, the data
        is stored for logging.

    ID_LOGIN = wx.NewId()

    def __init__(self, parent, id):
        self.parent = parent
        wx.Panel.__init__(self, parent, id, size=(200,150))
        self.quote = wx.StaticText(self, -1, "APP Login",wx.Point(60, 10))
        wx.StaticText(self, wx.ID_ANY, "User Name ", wx.Point(20, 33))
        wx.StaticText(self, wx.ID_ANY, "Password ", wx.Point(20, 58))
        self.usernameBox = wx.TextCtrl(self, wx.ID_ANY, "",
wx.Point(80, 31), wx.Size(100, wx.ID_ANY))
        self.passwordBox = wx.TextCtrl(self, wx.ID_ANY, "",
wx.Point(80, 56), wx.Size(100, wx.ID_ANY), style=wx.TE_PASSWORD)
        self.button =wx.Button(self, self.ID_LOGIN, "Login", wx.Point(60, 88))
        wx.EVT_BUTTON(parent, self.ID_LOGIN, self.on_login)

    def on_login(self, event):
        username = self.usernameBox.GetString(0, 128)
        password = self.passwordBox.GetString(0, 128)
        if username and password:
            self.parent.on_login_click(username, password)

class APPTaskBarIcon(wx.TaskBarIcon):
    ''' TaskBar Icon

    So that the application is always alive on the taskbar.
    3 operations are possible, DoubleClick on the taskbar would bring up the
    window, right click will bring up options to restore or exit the tool.

    TBMENU_RESTORE = wx.NewId()
    TBMENU_CLOSE   = wx.NewId()

    def __init__(self, frame):
        self.frame = frame
        # Set the image
        icon = self.MakeIcon(self.frame.get_image())
        self.SetIcon(icon, "APP")
        self.imgidx = 1
        # bind some events
        self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarActivate)
        self.Bind(wx.EVT_MENU, self.OnTaskBarActivate, id=self.TBMENU_RESTORE)
        self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=self.TBMENU_CLOSE)

    def CreatePopupMenu(self):
        """ Right Click Popup Menu

        This method is called by the base class when it needs to popup
        the menu for the default EVT_RIGHT_DOWN event.

        menu = wx.Menu()
        menu.Append(self.TBMENU_RESTORE, "Restore Window")
        menu.Append(self.TBMENU_CLOSE, "Exit APP")
        return menu

    def MakeIcon(self, img):
        """ Return the generated Icon.

        This function is required because various platforms have different
        requirements for the icon size.

        if "wxMSW" in wx.PlatformInfo:
            img = img.Scale(16, 16)
        elif "wxGTK" in wx.PlatformInfo:
            img = img.Scale(22, 22)
        icon = wx.IconFromBitmap(img.ConvertToBitmap() )
        return icon

    def OnTaskBarActivate(self, evt):
        ''' Activation code - say DoubleClick etc. It raises the window '''
        if self.frame.IsIconized():
        if not self.frame.IsShown():

    def OnTaskBarClose(self, evt):
        ''' On right click and close, the frame itself is closed '''

class APPFrame(wx.Frame):
    def __init__(self, parent, id, title, size=None):
        wx.Frame.__init__(self, parent, id, title,
                          size=size, pos=wx.Point(400, 300))
        icon = self.get_icon()
        self.tbicon = APPTaskBarIcon(self)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
        self.Bind(wx.EVT_ICONIZE, self.OnIconify)

    def on_login_click(self, username, password):

    def OnCloseWindow(self, event):
        if self.tbicon is not None:

    def OnIconify(self, event):

    def get_image(self):
        img= wx.Image(opj('client.ico'), wx.BITMAP_TYPE_ICO)
        return img

    def get_icon(self):
        img = self.get_image()
        icon = wx.IconFromBitmap(img.ConvertToBitmap() )
        return icon

if __name__ == '__main__':

If somebody can help me out, I would be much obliged.

More information about the Twisted-Python mailing list