Archive for September, 2006
Another wxPython Little Trick
In what is probably a somewhat unusual manuever, I needed to change the background color of a static text object (an entry field’s label) to a highlight color when the entry field has focus and then back to normal again when the entry field loses focus. I subclassed the TextCtrl and then gave it an instance attribute for storing the label control so that it could affect the label for me. Within my handler for the wx.EVT_SET_FOCUS I have the following code:
def _onFocus(self,evt): self._labelCtrl.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENUHILIGHT)) self._labelCtrl.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)) self._labelCtrl.Refresh()
And then to handle the loss of focus (wx.EVT_KILL_FOCUS) I handle it like this:
def _onLoseFocus(self,evt): self._labelCtrl.SetBackgroundColour(None) self._labelCtrl.SetForegroundColour(None) self._labelCtrl.Refresh()
Originally, I tried to store the original colors using the GetBackgroundColour() method but apparently between my control being on a notebook tab page and then the drawing of the control, the color reported back was not completely accurate because upon losing focus, it turned a slightly different color (darker like a regular panel instead of tab panel). Of course, this is on my XP machine, I have not looked to see what happens under Linux or Win2K. That’s when I thought to try good ‘ole None and poof, problems solved because that apparently resets it to whatever the system wants to use. Also of note, is the Refresh() method. Without the Refresh() it appears to not redraw the control with the new colors.
wxPython Grid Control Row Selection
(for reference) I was attempting to create a grid control in wxPython that acted more like a list box (i.e. the selection was a row at a time rather than a cell at a time). The first thing that comes to mind is:
self.SetSelectionMode(wx.grid.Grid.SelectRows)
This works great for mouse events but for keyboard events (up, down, page up/down) it doesn’t work… it reverts back to cell-based selection. So, I then bound an event handler right after that previous line like this:
self.Bind(wx.grid.EVT_GRID_SELECT_CELL,self._onSelectCell)
All the above is in the __init__ method of my grid control subclass. Then, of course, I define the actual event handler:
def _onSelectCell(self,evt): self.SelectRow(evt.GetRow()) evt.Skip()
Basically, this just uses the row for the cell that is being selected and forces it to select the entire row and then the evt.Skip() allows it to finish it’s normal processing (which appears to be necessary). The only issue is that you can still sort of see the selected cell but at least it’s essentially working like I want it to.
Technorati Tags: python, wxpython, grid
Python xmlrpclib timeout
For my reference, and the reference of anyone who needs to find it, while playing around with the xmlrpclib module, I found myself needing to be able to adjust the timeout. As near as I can tell, if you don’t set a timeout value, it will run forever or at least a long time (I have never been willing to wait long enough to find out). The trick to giving xmlrpclib a timeout on calls to the server is importing and using the socket module. The following code example essentially covers what you can do:
import xmlrpclib import socket s = xmlrpclib.Server('http://myRemoteServerName/myEntryPoint') s.myProcedureName('hello input') #this call will not timeout... socket.setdefaulttimeout(10) #set the timeout to 10 seconds s.myProcedureName('hello input') #now the call will timout after 10 seconds socket.setdefaulttimeout(None) #set it back to no timeout value (if you want to)
The kicker here is just setting the defaulttimeout value in the socket module with the setdefaulttimeout method. If the RPC call times out, it will throw a timeout exception that you’ll need to be able to handle. But at least that’s better than the alternative of not ever timing out.
Technorati Tags: python, xmlrpc, xmlrpclib
Twisted Logging
For my own reference, since I couldn’t find what I was looking for online. It appears that the example in the Twisted Network Programming Essentials book regarding logging is missing something (or else I am). I was, of course, attempting to create a new service.Service that I would be using for Error Logging. I created my ErrLogService and ErrorLog classes the way it was in the book, but no additional log file appeared. So, I started looking around at it and I realized that nothing actually starts the service even though it was declared as a child of the application.
application = service.Application('SPO Server', uid=1000,gid=1000) dbConnection = adbapi.ConnectionPool(DB_DRIVER,**DB_ARGS) spoWebService = internet.TCPServer(8000, webserver.Site(RootResource(dbConnection))) spoWebService.setName("SPO_Web_Service") spoWebService.setServiceParent(application) errLogService = ErrLogService("spoerrors.log","./logs") errLogService.setName("SPO_Error_Log") errLogService.setServiceParent(application) errLogService.startServce()
Without that last line, (errLogService.startService()), it doesn’t work, but with it, it works exactly as described in the book. (side note: I am using twistd to start the application)
Technorati Tags: python, twisted