Porting an application from one OS to another can be a time consuming process. Fortunately, wxPython takes the pain out of the process. This is only the 2nd time that I've ported my code to Linux. I usually write for Windows XP on Windows XP at work. When I originally wrote wxPyMail for work, I used Mark Hammond's PyWin32 library to get the user's full name and username to help in constructing their return address. Here's the code I used then:
try: userid = win32api.GetUserName() info = win32net.NetUserGetInfo('server', userid, 2) full_name = str(info['full_name'].lower()) name_parts = full_name.split(' ') self.emailuser = name_parts[:1] + name_parts[-1] email = self.emailuser + '@companyEmail' except: email = ''
If I had kept that code, I would need to use the wx.Platform module and test for the "WXMSW" flag, like this:
if wx.Platform == '__WXMSW__': try: userid = win32api.GetUserName() # rest of my win32 code except: # do something pass else: # put some Linux or Mac specific stuff here pass
This is probably the easiest way to go about porting code especially if you only have a couple of OS specific bits of code. If you have lots of it, then you'll probably want to stick them into their own module(s) and import them passed on your platform. I'm sure there are additional ways to accomplish this as well.
Anyway, I had taken out all that win32 stuff because that tied it down to my organization. Thus, the new code for wxPyMail actually ran just fine on Linux. I tested it on Ubuntu Hardy Heron. However, I noticed a couple of aesthetic issues. First, the frame seemed to be getting the focus so I couldn't just start typing in an email address or even my username in the wx.Dialog that pops up. And second, the labels and text boxes in my login dialog were too short in Ubuntu. They looked fine in Windows, but the labels were clipped on Ubuntu and I couldn't see all of my username when it was typed out in full.
Thus, I changed the code so that the size of the labels and textboxes are longer. I also called SetFocus() on the To field in the main application and the username text field in the login dialog. Other than that, I did a little refactoring by sticking the SMTP server in the __init__ so it's easier to find and set. I also changed my various control instantiations so that they reflected their parameters explicitly.
Also note the "shebang" line at the beginning of the file: "#!/usr/bin/env python". This tells Linux that this file can be executed using Python and it also tells Linux where the Python installation folder is. As long as python is in your path somewhere, you should be able to use this method. To check if you have python on your path, open a command prompt and type "python". If you get the python shell, then you're good to go.
You might think we're done now, but we forgot one important part. We still need to tell Linux to use wxPyMail as its default email program for mailto links. I found a How-To article on this very topic at howtogeek. We need to modify it a bit to make it work, but it's actually pretty easy once you know how. For this example, I used Ubuntu Hardy Heron (8.04) with wxPython 22.214.171.124 and Python 2.5.2.
Anyway, first you'll need to download my code. When I first wrote this application, it was on Windows. The nice people on the wxPython mailing list pointed out to me that I had line ending issues with my code, so make sure that you don't accidentally convert it back to Windows format. Thanks goes out to Robin Dunn, Christopher Barker, Cody Precord, Frank Millman and a guy named Keith.
Second, you'll need to tell Linux to execute the python script when the user clicks on a mailto link. In Ubuntu, you can do that by going to System, Preferences, Preferred Applications. Change the "Mail Reader" to "Custom" and in the "Command" field add the following:
Replace USERNAME with your username and adjust the path as needed. Make sure you include the "%s" as that represents the "mailto" string that is passed to our script. Now, open a command prompt and change directories to where you put the python script. You'll need to change its permissions to execute, so something like this should do it:
chmod +x wxPyMail.py
Now browse to a website with a mailto link and try it out! As always, feel to free to email me with questions or comments at mike [at] pythonlibrary [dot] org.
Download the Source
Copyright © 2022 Mouse Vs Python | Powered by Pythonlibrary