[Twisted-Python] My first wxPython App with Twisted

Kevin Horn kevin.horn at gmail.com
Tue Aug 4 16:00:55 EDT 2009


On Tue, Aug 4, 2009 at 2:28 PM, Thalassery Koran <koranthala at gmail.com>wrote:

> Hi,
>    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
> wxreactor.install()
> 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)
>        call.start(.51)
>        return True
>
>
> def main():
>    app = APPApp(0)
>    reactor.registerWxApp(app)
>    try:
>        reactor.run()
>    except StandardError:
>        raise
>
>
> class APPPanel(wx.Panel):
>    """ APP GUI 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.usernameBox.SetFocus()
>        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):
>        wx.TaskBarIcon.__init__(self)
>        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():
>            self.frame.Iconize(False)
>        if not self.frame.IsShown():
>            self.frame.Show(True)
>        self.frame.Raise()
>
>    def OnTaskBarClose(self, evt):
>        ''' On right click and close, the frame itself is closed '''
>        wx.CallAfter(self.frame.Close)
>
>
> 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.SetIcon(icon)
>        self.tbicon = APPTaskBarIcon(self)
>        self.Show(True)
>        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
>        self.Bind(wx.EVT_ICONIZE, self.OnIconify)
>
>    def on_login_click(self, username, password):
>        self.Hide()
>
>    def OnCloseWindow(self, event):
>        if self.tbicon is not None:
>            self.tbicon.Destroy()
>        event.Skip()
>
>    def OnIconify(self, event):
>        self.Hide()
>
>    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__':
>    main()
>
>
> --------------
> If somebody can help me out, I would be much obliged.
>
>
Well, it's hard to say without looking at your Twisted code, but what
exactly is the LoopingCall for?  It seems like it would be run regardless of
whether the user had logged in or not.

I'm guessing, but maybe client.run() shouldn't be run until you have a
username and password?

Might be a place to start looking, anyway.  It looks like you've set things
up properly in main() an App.OnInit(), but I haven't worked with Twisted +
wx in a while (pre-wxreactor), so I'm not 100% sure...

I guess it boils down to what exactly client.run() does...

Kevin Horn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20090804/a2ba56d8/attachment-0001.htm 


More information about the Twisted-Python mailing list