本入门教程中的所有案例均已测试通过,具体参考入门1中的使用方法。
15 对话框2
16 拖拽功能
17 绘图1
18 绘图2
19 绘图3
20 绘图4
21 进度条
22 游戏——贪吃蛇
功能:打开字体选择夹对话框
本案例中创建按钮,其功能用于触发事件,弹出字体选择窗口。 对应的位置会改变字体。
# encoding: utf-8
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Application, Form, StatusBar, Label
from System.Windows.Forms import ToolBar, ToolBarButton, FontDialog
from System.Windows.Forms import DialogResult
class IForm(Form):
def __init__(self):
self.Text = "FolderBrowserDialog"
self.text = Label()
self.text.Parent = self
self.text.Text = "Winforms tutorial"
self.LocateText()
toolbar = ToolBar()
toolbar.Parent = self
openb = ToolBarButton()
toolbar.Buttons.Add(openb)
toolbar.ButtonClick += self.OnClicked
self.text.AutoSize = True
self.Resize += self.OnResize
self.CenterToScreen()
def OnResize(self, sender, event):
self.LocateText()
def LocateText(self):
self.text.Top = (self.ClientSize.Height - self.text.Height) / 2
self.text.Left = (self.ClientSize.Width - self.text.Width) / 2
def OnClicked(self, sender, event):
dialog = FontDialog()
if (dialog.ShowDialog(self) == DialogResult.OK):
self.text.Font = dialog.Font
self.LocateText()
Application.Run(IForm())
效果展示:
功能:打开文件选择夹对话框
本案例中创建按钮,其功能用于触发事件,弹出文件选择窗口。 对应的色块会改变颜色。
# encoding: utf-8
import os
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Application, Form, TextBox
from System.Windows.Forms import ToolBar, ToolBarButton, OpenFileDialog
from System.Windows.Forms import DialogResult, ScrollBars, DockStyle
class IForm(Form):
def __init__(self):
self.Text = "OpenDialog"
toolbar = ToolBar()
toolbar.Parent = self
openb = ToolBarButton()
self.textbox = TextBox()
self.textbox.Parent = self
self.textbox.Multiline = True
self.textbox.ScrollBars = ScrollBars.Both
self.textbox.WordWrap = True
self.textbox.Dock = DockStyle.Fill
toolbar.Buttons.Add(openb)
toolbar.ButtonClick += self.OnClicked
self.CenterToScreen()
def OnClicked(self, sender, event):
dialog = OpenFileDialog()
dialog.Filter = "python files (*.py)|*.py"
if dialog.ShowDialog(self) == DialogResult.OK:
os.startfile(dialog.FileName)
Application.Run(IForm())
效果展示:
完整的本地电子版本参阅入门1的其他。