My first wxPython App with Twisted


K

koranthala

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.run method does the verification etc along with other
items
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()
 
Ad

Advertisements

K

koranthala

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.run method does the verification etc along with other
items
       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()

Please disregard this mail. I will post a barebones version separately.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top