本入门教程中的所有案例均已测试通过,具体参考入门1中的使用方法。
09 基本控件2
11 基本控件4
12 高级控件1
13 高级控件2
14 对话框1
15 对话框2
16 拖拽功能
17 绘图1
18 绘图2
19 绘图3
20 绘图4
21 进度条
22 游戏——贪吃蛇
基本控件4
case20 工具栏控件7 TextBox目标:创建TextBox,并显示在Label标签上。
本案例中分别创建了TextBox和Label两个控件,在TextBox中可以输入字符,相应的字符会显示在Label中。
# encoding: utf-8
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Application, Form
from System.Windows.Forms import Label, TextBox
from System.Drawing import Size, Point
class IForm(Form):
def __init__(self):
self.Text = 'TextBox'
self.text = Label()
self.text.Parent = self
self.text.Text = "..."
self.text.AutoSize = True
self.text.Location = Point(60, 40)
tbox = TextBox()
tbox.Parent = self
tbox.Location = Point(60, 100)
tbox.KeyUp += self.OnKeyUp
self.Size = Size(250, 200)
self.CenterToScreen()
def OnKeyUp(self, sender, event):
self.text.Text = sender.Text
Application.Run(IForm())
效果展示:
目标:创建一个图片框,并在其中显示图片。
# encoding: utf-8
import sys
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Application, Form, PictureBox
from System.Drawing import Size, Point, Bitmap
path = r"D:\01-图片\12345.png"
class IForm(Form):
def __init__(self):
self.Text = 'PictureBox'
try:
castle = Bitmap(path)
except Exception, e:
print 'Cannot read image file'
print e.message
sys.exit(1)
pb = PictureBox()
pb.Parent = self
pb.Size = Size(castle.Width, castle.Height)
pb.Location = Point(2, 2)
pb.Image = castle
self.Size = Size(castle.Width, castle.Height)
self.CenterToScreen()
Application.Run(IForm())
效果展示:
完整的本地电子版本参阅入门1的其他。