python winform_[IronPython]第一个WinForm

  • Post author:
  • Post category:python


ide还没搞定,先手写个WinForm 尝尝鲜。

#IronPython v1.12-2.7

import clr

refs = [

‘System.Windows.Forms’,’System.Drawing’,’System.Windows’,’System.Runtime.InteropServices’,

‘System’,’System.IO’,’System.Collections’,

]

imps = [(‘from %s import *’ % i) for i in refs ]

for i in refs:

try:

clr.AddReference(i)

except Exception:

pass

for i in imps:

try:

exec(compile(i, ”, ‘exec’))

except Exception:

pass

class T1Form(Form):

btnSave = Button()

btnExit = Button()

btnOpen = Button()

txtBox = TextBox()

def __init__(self,_Text=’hello world’,_Width=500,_Height=400):

self.Text = _Text

self.Size = Size(_Width,_Height)

self.StartPosition = FormStartPosition.CenterScreen

self.__initControls();

def __initControls(self):

self.btnSave.Text = “&Save”

self.btnSave.Location = Point (10, 10);

self.btnExit.Text = “E&xit”

#btnExit.Location = Point (btnSave.Left, btnSave.Height + btnSave.Top + 10);

self.btnExit.Location = Point (self.btnSave.Left+self.btnSave.Width+10, self.btnSave.Top);

self.btnExit.Click += EventHandler(self.__btnExit_Click)

self.btnOpen.Text = “&Open”

self.btnOpen.Location = Point (self.btnExit.Left+self.btnExit.Width+10, self.btnSave.Top);

self.btnOpen.Click += EventHandler(self.__btnOpen_Click)

#txtBox.Location = Point (btnExit.Left, btnExit.Height + btnExit.Top + 10);

self.txtBox.Location = Point (self.btnSave.Left, self.btnExit.Height + self.btnExit.Top+10);

self.txtBox.Width = 470

self.txtBox.Height = 320

self.txtBox.Multiline = True

self.txtBox.ScrollBars = ScrollBars.Vertical

self.txtBox.WordWrap = True

self.Controls.Add(self.btnSave)

self.Controls.Add(self.btnExit)

self.Controls.Add(self.btnOpen)

self.Controls.Add(self.txtBox)

def __btnExit_Click(self, sender, event):

self.Close()

def __btnOpen_Click(self, sender, event):

openFileDialog1 = OpenFileDialog()

openFileDialog1.InitialDirectory = “c:\\”

openFileDialog1.Filter = “txt files (*.txt)|*.txt|All files (*.*)|*.*”

openFileDialog1.FilterIndex = 2

openFileDialog1.RestoreDirectory = True

if(openFileDialog1.ShowDialog() == DialogResult.OK):

self.txtBox.Text = ”

with open(openFileDialog1.FileName) as f:

for i in f.readlines():

self.txtBox.Text += i

def show(self):

self.ShowDialog()

def t1():

winform = T1Form()

winform.show()

if __name__==”__main__”:

t1()