[Python.NET] Form design

david Powell david.powell at kcl.ac.uk
Wed Nov 19 05:21:45 EST 2003


Skipped content of type multipart/alternative-------------- next part --------------
A non-text attachment was scrubbed...
Name: form1.xfrm
Type: application/octet-stream
Size: 3274 bytes
Desc: not available
Url : http://mail.python.org/pipermail/pythondotnet/attachments/20031119/afadcdbe/form1.obj
-------------- next part --------------
import CLR
import CLR.System.Windows.Forms as WinForms
from CLR.System.Drawing import Size, Point

class MyForm(WinForms.Form):

	def __init__(self):
		self.ClientSize=Size(424, 301)

		self.pn = WinForms.Panel()
		self.Controls.Add(self.pn)
		self.pn.Name='pn'
		self.pn.Location=Point(16,168)
		self.pn.Size=Size(136, 104)
		self.pn.TabIndex=6

		self.tc = WinForms.TabControl()
		self.Controls.Add(self.tc)
		self.tc.Name='tc'
		self.tc.TabIndex=4
		self.tc.Size=Size(216, 104)
		self.tc.Location=Point(176,176)

		self.Page1 = WinForms.TabPage()
		self.tc.Controls.Add(self.Page1)
		self.Page1.Text='Page1'
		self.Page1.Size=Size(208, 78)
		self.Page1.Location=Point(4,22)

		self.btnpage1 = WinForms.Button()
		self.Page1.Controls.Add(self.btnpage1)
		self.btnpage1.Name='btnpage1'
		self.btnpage1.Location=Point(48,16)
		self.btnpage1.Size=Size(96, 24)
		self.btnpage1.Text='button Page 1'
		self.btnpage1.TabIndex=0

		self.Page2 = WinForms.TabPage()
		self.tc.Controls.Add(self.Page2)
		self.Page2.Text='Page2'
		self.Page2.Size=Size(208, 78)
		self.Page2.Location=Point(4,22)

		self.btnPage2 = WinForms.Button()
		self.Page2.Controls.Add(self.btnPage2)
		self.btnPage2.Name='btnPage2'
		self.btnPage2.Location=Point(56,16)
		self.btnPage2.Size=Size(88, 24)
		self.btnPage2.Text='button Page2'
		self.btnPage2.TabIndex=0

		self.pb = WinForms.PictureBox()
		self.Controls.Add(self.pb)
		self.pb.Name='pb'
		self.pb.Size=Size(136, 128)
		self.pb.Location=Point(16,32)
		self.pb.BorderStyle=WinForms.BorderStyle.FixedSingle

		self.btnTextClear = WinForms.Button()
		self.Controls.Add(self.btnTextClear)
		self.btnTextClear.Name='btnTextClear'
		self.btnTextClear.Location=Point(264,16)
		self.btnTextClear.Size=Size(80, 32)
		self.btnTextClear.Text='Clear'
		self.btnTextClear.TabIndex=2

		self.textBox = WinForms.TextBox()
		self.Controls.Add(self.textBox)
		self.textBox.Name='textBox'
		self.textBox.BackColor=CLR.System.Drawing.Color.FromArgb(192,255,255)
		self.textBox.TabIndex=1
		self.textBox.Location=Point(168,64)
		self.textBox.Size=Size(160, 56)
		self.textBox.Multiline=True
		self.textBox.Text='textBox'

		self.btnTextAdd = WinForms.Button()
		self.Controls.Add(self.btnTextAdd)
		self.btnTextAdd.Name='btnTextAdd'
		self.btnTextAdd.Location=Point(168,16)
		self.btnTextAdd.Size=Size(80, 32)
		self.btnTextAdd.Text='Add'
		self.btnTextAdd.TabIndex=0
	def run(self):
		WinForms.Application.Run(self)
def main():
	MyForm().run()
if __name__ == '__main__':
	main()
-------------- next part --------------
from form1 import *

class MyFormWithEvents(MyForm):

    def __init__(self):
        MyForm.__init__(self)
        self.addEventHandlers()

    def ButtonClick1(self,sender,args):
        self.textBox.Text+="zzz"

    def ButtonClick2(self,sender,args):
        self.textBox.Text=""
        
    def addEventHandlers(self):
        #setattr(MyForm,"ButtonClick1",ButtonClick1)
	self.btnTextAdd.Click += self.ButtonClick1
	self.btnTextClear.Click += self.ButtonClick2
    
		
def main():
 
	f=MyFormWithEvents()
	f.textBox.Text="Initial value"
	
        CLR.System.Windows.Forms.Application.Run(f)

	
if __name__== '__main__':
	main()
-------------- next part --------------
#Some functions that take either
#(a) an xml form defintion from the IDE SharpDevelop
#or
#(b) a C# form definition from the IDE SharpDevelop
#
#and then produces a python-PythondotNet form defintion
#
#note that Frederik Lundh's ElementTree is needed
#http://effbot.org/downloads/

from elementtree.ElementTree import *

    
def tab(n):
    if n<=0 :
        return('')
    else:
        s=''
        for i in range(n):
            s=s+('\t')
        return(s)

def csharpToPythondotNet (filenameCsharp, filenamePythondotNet):
    #experimental
    name=''
    elc=0
    f=open(r'd:\sharp\form1.cs')
    Ls=f.readlines()
    f=open(r'd:\sharp\form1cs.py','w')
    s='import CLR'
    writeLine(f,s)
    s='from CLR.System.Drawing import *'
    writeLine(f,s)
    s='from CLR.System.Windows.Forms import *'
    writeLine(f,s)

    process=False
    for aL in Ls:
        #print aL
        if aL.find('public class')>=0 :
            k=aL.find('public class')
            k=k+len('public class')
            k1=aL.find(':')
            name=aL[k:k1].strip()
            print name
            s='class ' + name + '(CLR.System.Windows.Forms.Form):'
            writeLine(f,s)
            process=True
            tabs=1
            continue
        if process:
            a=aL.strip()
            a=a.strip('\t')
            a=a.strip('\r')
            a=a.strip('\n')
            if a.find('private System')==0:
                continue
            if a.find('}')==0:
                continue
            if a.find(r'//')==0 :
                aL=aL.replace(r'//',r'#')
                writeLine(f,aL)
                continue
            if a.find('public ' + name)==0:
                #build constructor
                s=tab(1) + 'def __init__(self):'
                writeLine(f,s)
                tabs=2
                continue
            if a.find('private void Initialize')==0 :
                s='def InitializeComponent(self):'
                s= tab(1) + s
                writeLine(f,s)
                tabs=2
                continue
            #all other lines
            a=a.replace('this.','self.')
            a=a.replace(';','')
            a=a.replace('{','')
            a=a.replace('= new','= ')
            a=a.replace('System.Windows.Forms.','CLR.System.Windows.Forms.')
            a=a.replace('System.Drawing','CLR.System.Drawing')
            a=a.replace('true','True')
            a=a.replace('false','False')
            if a=='':
                elc=elc+1
            else:
                elc=0
            if a=='':
                if elc==1:
                    writeLine(f,'')
            else:
                s=tab(tabs)+a
                writeLine(f,s)
                
    s='def main():'
    writeLine(f,s)
    s=tab(1) + 'f=' + name + '()'
    writeLine(f,s)
	
    s = tab(1) + 'CLR.System.Windows.Forms.Application.Run(f)'
    writeLine(f,s)

	
    s="if __name__== '__main__':"
    writeLine(f,s)

    s = tab(1) + 'main()'
    writeLine(f,s)

    f.close()
    
def XMLToPythondotNet(filenameXML,filenamePythondotNet):
    #the recursive function processControls is used because
    #a form can contain a tab control and a tabpage has its
    #own controls collection
        
    d={}
    d['ClientSize']='Size'
    d['Size']='Size'
    d['Location']='Point'
    d['Name']='Text'
    d['TabIndex']='Int'
    d['Multiline']='Boolean'
    d['Text']='Text'
    d['BorderStyle']='Enum'
    d['SelectedIndex']='Int'
    d['BackColor']='Color'
    d['ForeColor']='Color'
    
    f=open(filenamePythondotNet,'w')
    writeLine(f,'import CLR')
    writeLine(f,'import CLR.System.Windows.Forms as WinForms')
    writeLine(f,'from CLR.System.Drawing import Size, Point')
    
    #get tree from file
    tree=ElementTree(file=filenameXML)
    r=tree.getroot()
    
    nc=r.getiterator("System.Windows.Forms.Form")
    n=nc[0]
    el=n.find("Name")
    formName=el.get("value")
    s='class ' + formName + '(WinForms.Form):'
    writeLine(f,'')
    writeLine(f,s)
    writeLine(f,'')
    s=tab(1) + 'def __init__(self):'
    writeLine(f,s)
    el=n.find("ClientSize")
    s=tab(2) + 'self.' + propertyFormat(el,d)
    writeLine(f,s)
    
    n=n.find('Controls')
    processControls(n,'self',f,d)
            
        
    s=tab(1) + 'def run(self):'
    writeLine(f,s)
    s=tab(2) + 'WinForms.Application.Run(self)'
    writeLine(f,s)
    
    s='def main():'
    writeLine(f,s)
    s=tab(1) + formName + '().run()'
    writeLine(f,s)
    
    s="if __name__ == '__main__':"
    writeLine(f,s)
    s=tab(1) + 'main()'
    writeLine(f,s)
        
    f.close()
    
def processControls(n,parent,f,d):
    
    for c in n._children:   #these are the controls
        elname=c.find('Name')
        name=elname.get('value')
        controlType=c.tag
        controlType=controlType.replace('System.Windows.Forms','WinForms')
        s=tab(2) + 'self.' + name + ' = ' + controlType + '()'
        writeLine(f,'')
        writeLine(f,s)
        s=tab(2) + parent +'.Controls.Add(self.' + name + ')'
        writeLine(f,s)
        for cp in c._children:  #these are control properties
            if not d.has_key(cp.tag):
                continue
##            if cp.tag=='Name':
##                continue
##            if cp.tag=="Lines":
##                continue
##            if cp.tag=="TabPages":
##                continue
##            if cp.tag=="DockPadding":
##                continue
##            if cp.tag=="Controls":
##                continue
            s=tab(2) + 'self.' + name + '.' + propertyFormat(cp,d)
            writeLine(f,s)
        
        if controlType=='WinForms.TabControl':
            tcname=name         #remember tabcontrol name
            ntabpages=c.find('TabPages')
            for ntp in ntabpages._children:
                controlType=ntp.tag
                controlType=controlType.replace('System.Windows.Forms','WinForms')                
                if controlType!='WinForms.TabPage':
                    pass
                elname=ntp.find('Name')
                name=elname.get('value')
                writeLine(f,'')
                s=tab(2) + 'self.' + name + ' = ' + controlType + '()'
                writeLine(f,s)
                s=tab(2) + 'self.' + tcname +'.Controls.Add(self.' + name + ')'
                writeLine(f,s)

                for cp in ntp._children:
                    if cp.tag=='Name':
                        continue
                    if cp.tag=="Lines":
                        continue
                    if cp.tag=="TabPages":
                        continue
                    if cp.tag=="DockPadding":
                        continue
                    if cp.tag=="Controls":
                        continue
                    s=tab(2) + parent + '.' + name + '.' + propertyFormat(cp,d)
                    writeLine(f,s)

                
                ntpc=ntp.find('Controls')
                processControls(ntpc,(parent + '.' + name),f,d)
                
                
                    
            
            
##        for cp in c._children:
##            if cp.tag=='Name':
##                continue
##            if cp.tag=="Lines":
##                continue
##            if cp.tag=="TabPages":
##                continue
##            s=tab(2) + 'self.' + name + '.' + propertyFormat(cp,d)
##            writeLine(f,s)
            
        #s=tab(2)+parent+'.Controls.Add(self.' + name + ')'
        #writeLine(f,s)
    

        
def writeLine(f,s):
    f.write(s+"\n")
    
def sizeFormat(s):
    size=s
    size=size.replace('{','(')
    size=size.replace('}',')')
    size=size.replace('Width=','')
    size=size.replace('Height=','')
    return ('Size' + size)

def pointFormat(s):
    point=s
    point=point.replace('{','(')
    point=point.replace('}',')')
    point=point.replace('X=','')
    point=point.replace('Y=','')
    
    return ('Point' + point)

def colorFormat(s):
    sa=s.split(',')
    sn=sa[1][3:]
    sn+=','+ sa[2][3:]
    k=sa[3].find(']')
    sn+=',' + sa[3][3:k]
    sn="CLR.System.Drawing.Color.FromArgb(" + sn + ")"
    return sn

def propertyFormat(el,d):
    print el.tag
    if not d.has_key(el.tag):
        pass
    type=d[el.tag]
    s=el.get('value')
    if type=='Size' :
        s=sizeFormat(s)
    elif type=='Point':
        s=pointFormat(s)
    elif type=='Int':
        pass
    elif type=='Boolean':
        pass
    elif type=='Enum':
        s="WinForms." + el.tag + '.' + s
    elif type=='Color':
        s=colorFormat(s)
        
    
    else:
        s="'" + s + "'"
    s=el.tag + '=' + s
    return(s)
        
        


More information about the PythonDotNet mailing list