原文: http://zetcode.com/tutorials/ironpythontutorial/controls/
IronPython Mono Winforms 编程教程的这一部分将介绍基本控件。
Winforms 控件是应用的基本构建块。 Winforms 具有广泛的各种控件。 按钮,复选框,轨迹栏,标签等。程序员完成工作所需的一切。 在本教程的这一部分中,我们将描述几个有用的控件。
Label
Label是用于显示文本或图像的简单控件。 它没有得到关注。
label.py
#!/usr/bin/ipyimport sysimport clrclr.AddReference("System.Windows.Forms")clr.AddReference("System.Drawing")from System.Windows.Forms import Application, Form, Labelfrom System.Drawing import Size, Point, Fonttext = """Meet you downstairs in the bar and heardYour rolled up sleeves and your skull t-shirtYou say why did you do it with him today?And sniffed me out like I was tanquerayCause you're my fella, my guyHand me your stella and flyBy the time I'm out the doorYou tear me down like roger mooreI cheated myselfLike I knew I wouldI told ya, I was troubleYou know that I'm no goodUpstairs in bed, with my ex boyHe's in a place, but I cant get joyThinking of you in the final throws, this is when my buzzer goes"""class IForm(Form):def __init__(self):self.Text = "You know I'm No Good"font = Font("Serif", 10)lyrics = Label()lyrics.Parent = selflyrics.Text = textlyrics.Font = fontlyrics.Location = Point(10, 10)lyrics.Size = Size(290, 290)self.CenterToScreen()Application.Run(IForm())
在我们的示例中,我们在表单上显示了一些歌词。
lyrics = Label()
Label控件已创建。
text = """Meet you downstairs in the bar and heard... """
这是我们的文字。
font = Font("Serif", 10)...lyrics.Font = font
标签文本的字体设置为 10px Serif。

图:Label
CheckBox
CheckBox是具有两个状态的控件:开和关。 它是带有标签或图像的盒子。 如果选中CheckBox,则在方框中用勾号表示。 CheckBox可用于在启动时显示或隐藏启动画面,切换工具栏的可见性等。
checkbox.py
#!/usr/bin/ipyimport clrclr.AddReference("System.Windows.Forms")clr.AddReference("System.Drawing")from System.Windows.Forms import Application, Form, CheckBoxfrom System.Drawing import Size, Pointclass IForm(Form):def __init__(self):self.Text = "CheckBox"self.Size = Size(220, 170)cb = CheckBox()cb.Parent = selfcb.Location = Point(30, 30)cb.Text = "Show Title"cb.Checked = Truecb.CheckedChanged += self.OnChangedself.CenterToScreen()def OnChanged(self, sender, event):if sender.Checked:self.Text = "CheckBox"else:self.Text = ""Application.Run(IForm())
我们的代码示例根据窗口的状态显示或隐藏窗口的标题。
cb = CheckBox()
CheckBox控件已创建。
cb.Text = "Show Title"cb.Checked = True
当应用启动时,我们显示标题。 然后将CheckBox控件设置为选中状态。
cb.CheckedChanged += self.OnChanged
当我们单击CheckBox控件时,将触发CheckedChanged事件。
if sender.Checked:self.Text = "CheckBox"else:self.Text = ""
在这里,我们切换窗口的标题。

图:CheckBox
TrackBar
TrackBar是一个组件,使用户可以通过在有限的间隔内滑动旋钮来以图形方式选择一个值。 我们的示例将显示音量控制。
trackbar.py
#!/usr/bin/ipyimport sysimport clrclr.AddReference("System.Windows.Forms")clr.AddReference("System.Drawing")from System.Windows.Forms import Application, Form, PictureBoxfrom System.Windows.Forms import TrackBar, TickStylefrom System.Drawing import Size, Point, Bitmapclass IForm(Form):def __init__(self):self.Text = 'TrackBar'self.Size = Size(260, 190)tb = TrackBar()tb.Parent = selftb.Size = Size(150, 30)tb.Location = Point(30, 50)tb.TickStyle = TickStyle.Nonetb.SetRange(0, 100)tb.ValueChanged += self.OnChangedself.LoadImages()self.pb = PictureBox()self.pb.Parent = selfself.pb.Location = Point(210, 50)self.pb.Image = self.mutepself.CenterToScreen()def LoadImages(self):try:self.mutep = Bitmap("mute.png")self.minp = Bitmap("min.png")self.medp = Bitmap("med.png")self.maxp = Bitmap("max.png")except Exception, e:print "Error reading images"print e.msgsys.exit(1)def OnChanged(self, sender, event):val = sender.Valueif val == 0:self.pb.Image = self.mutepelif val > 0 and val <= 30:self.pb.Image = self.minpelif val > 30 and val < 80:self.pb.Image = self.medpelse: self.pb.Image = self.maxpApplication.Run(IForm())
在代码示例中,我们显示了TrackBar和PictureBox。 通过拖动轨迹栏,我们可以在PictureBox控件上更改图像。
tb = TrackBar()
TrackBar控件已创建。
tb.TickStyle = TickStyle.None
我们对此TrackBar没有显示任何报价。
self.pb = PictureBox()...self.pb.Image = self.mutep
PictureBox控件已创建。 它用于显示图像。 开始时,它会显示静音图像。
self.mutep = Bitmap("mute.png")self.minp = Bitmap("min.png")self.medp = Bitmap("med.png")self.maxp = Bitmap("max.png")
在这里,我们将创建四个将要使用的图像。
val = sender.Valueif val == 0:self.pb.Image = self.mutepelif val > 0 and val <= 30:self.pb.Image = self.minpelif val > 30 and val < 80:self.pb.Image = self.medpelse: self.pb.Image = self.maxp
我们确定TrackBar的值。 根据其值,我们更新PictureBox控件。

图:TrackBar
ComboBox
ComboBox是一个组合了按钮或可编辑字段和下拉列表的控件。 用户可以从下拉列表中选择一个值,该列表应用户的要求出现。 如果使组合框可编辑,则组合框将包含一个可编辑字段,用户可以在其中输入值。
combobox.py
#!/usr/bin/ipyimport clrclr.AddReference("System.Windows.Forms")clr.AddReference("System.Drawing")from System.Windows.Forms import Application, Formfrom System.Windows.Forms import ComboBox, Labelfrom System.Drawing import Size, Pointclass IForm(Form):def __init__(self):self.Text = "ComboBox"self.Size = Size(240, 240)cb = ComboBox()cb.Parent = selfcb.Location = Point(50, 30)cb.Items.AddRange(("Ubuntu","Mandriva","Red Hat","Fedora","Gentoo"))cb.SelectionChangeCommitted += self.OnChangedself.label = Label()self.label.Location = Point(50, 140)self.label.Parent = selfself.label.Text = "..."self.CenterToScreen()def OnChanged(self, sender, event):self.label.Text = sender.TextApplication.Run(IForm())
我们的代码编程示例显示了一个包含五个项目的组合框。 所选项目显示在标签控件中。
cb = ComboBox()
ComboBox控件已创建。
cb.Items.AddRange(("Ubuntu","Mandriva","Red Hat","Fedora","Gentoo"))
ComboBox控件中充满了项目。
cb.SelectionChangeCommitted += self.OnChanged
如果我们从组合框中选择一个项目,则会触发SelectionChangeCommitted事件。
def OnChanged(self, sender, event):self.label.Text = sender.Text
在这里,将从组合框中选择的文本复制到标签。

图:ComboBox
我们已经完成了 IronPython Mono Winforms 教程的这一章,专门讨论基本控件。
