Saturday, January 18, 2014

A way to change a system environment variable in Python program.

In Python program we can set system environment variables by using os.environ. For example,

os.environ ["DDD"] = "..." 

But there is a problem that the variable will be disappear after the Python program exits because the variable is owned by the Python program.

The following way can solve the problem to create an environment variable with Python and share it with other programs.

python ChgEnv.py %1
call Temp.bat
view raw ChgEnv.bat hosted with ❤ by GitHub
import sys
File = open ("Temp.bat", "w+")
File.write ("set DDD=%s\n" % sys.argv [1])
File.close ()
view raw ChgEnv.py hosted with ❤ by GitHub
-Count

Wednesday, January 15, 2014

A way to apply the HTML coding habit in wxPython sizer for UI layout.

We can use the size of wxPython to layout user interface but it is not easy to use. If we are familiar with HTML's table tags, such as TABLE, TR and TD, we can use them as a comment in Python program to help us design UI.

Because Python forbids to have free-style indents, we can use a trick of if-string to make indents.

Below is the sample.

def __init__ (self):
wx.Frame.__init__ (self, None, -1, self.Title)
if "<TABLE>":
self.Hs = wx.BoxSizer (wx.VERTICAL)
if "<TR>":
self.St = wx.StaticText(self, label="Your name :", pos=(20,60))
self.Hs.Add (self.St)
if "<TR>":
self.Vs = wx.BoxSizer (wx.HORIZONTAL)
if "<TD>":
self.St2 = wx.StaticText(self, label="...", pos=(20,60))
self.Vs.Add (self.St2)
if "<TD>":
self.Logger = wx.TextCtrl (self, 5, "", wx.Point (0,0), wx.Size (-1, -1), \
wx.TE_MULTILINE | wx.TE_READONLY)# | wx.TE_RICH2)
self.Logger.SetBackgroundColour((255,255,0))
self.Vs.Add (self.Logger, 1, wx.EXPAND)
self.Hs.Add (self.Vs, 1, wx.EXPAND)
self.SetSizer (self.Hs)
view raw Layout.py hosted with ❤ by GitHub
-Count