Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - My first wxPython App with Twisted

 
Thread Tools Search this Thread
Old 08-05-2009, 02:29 AM   #1
Default My first wxPython App with Twisted


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, 5)
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, 8)
wx.EVT_BUTTON(parent, self.ID_LOGIN, self.on_login)

def on_login(self, event):
username = self.usernameBox.GetString(0, 12
password = self.passwordBox.GetString(0, 12
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.


koranthala
  Reply With Quote
Old 08-05-2009, 02:39 AM   #2
koranthala
 
Posts: n/a
Default Re: My first wxPython App with Twisted
On Aug 5, 6:29*am, koranthala <koranth...@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.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, 5)
> * * * *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, 8)
> * * * *wx.EVT_BUTTON(parent, self.ID_LOGIN, self.on_login)
>
> * *def on_login(self, event):
> * * * *username = self.usernameBox.GetString(0, 12
> * * * *password = self.passwordBox.GetString(0, 12
> * * * *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.


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


koranthala
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
The Strange and Twisted World of Internet Cafes Silverstrand Front Page News 0 09-15-2006 05:25 AM
wxPython frame refresh wolfbrigade Software 0 09-07-2006 09:59 PM
DVD Verdict reviews: LAST EXIT, SPIKE AND MIKE'S SICK AND TWISTED FESTIVAL OF ANIMATION: CONTAGIOUS, and more! DVD Verdict DVD Video 0 01-14-2006 09:18 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46