本入门教程中的所有案例均已测试通过,具体参考入门1中的使用方法。
01 入门1
02 入门2
03 布局管理1
04 布局管理2
05 菜单栏1
06 菜单栏2
07 工具栏
08 基本控件1
09 基本控件2
10 基本控件3
11 基本控件4
12 高级控件1
13 高级控件2
14 对话框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
from System.Windows.Forms import TreeView, TreeNode, DockStyle
from System.Drawing import Size
class IForm(Form):
def __init__(self):
self.Text = 'TreeView'
tv = TreeView()
root = TreeNode()
root.Text = 'Languages'
child1 = TreeNode()
child1.Text = 'Python'
child2 = TreeNode()
child2.Text = 'Ruby'
child3 = TreeNode()
child3.Text = 'Java'
root.Nodes.AddRange((child1, child2, child3))
#child1.Nodes.AddRange((child2, child3))
tv.Parent = self
tv.Nodes.Add(root)
tv.Dock = DockStyle.Fill
tv.AfterSelect += self.AfterSelect
self.s b = StatusBar()
self.s b.Parent = self
self.Size = Size(220, 220)
self.CenterToScreen()
def AfterSelect(self, sender, event):
self.s b.Text = event.Node.Text
Application.Run(IForm())
效果展示:
目标:创建一个多层级的结构树。
本案例中以本地文件夹作为目录对象,使其内部的文件夹结构成为结构树。同时创建了4个按钮,功能分别是展开当前,折叠当前;展开全部;折叠全部。
# encoding: utf-8
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Application, Form, StatusBar
from System.Windows.Forms import Button, TreeView, TreeNode
from System.Windows.Forms import DockStyle, AnchorStyles
from System.Drawing import Size, Point
from System.IO import Directory, DirectoryInfo
# 本地目录路径,需根据实际情况变更。
HOME_DIR = r'E:\03-Workflows'
class IForm(Form):
def __init__(self):
self.Text = 'Directories'
self.Size = Size(400, 400)
self.tv = TreeView()
self.SuspendLayout()
self.tv.Parent = self
self.tv.Location = Point(10,10)
self.tv.Size = Size(self.ClientSize.Width - 20, self.Height - 200)
self.tv.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right
self.tv.FullRowSelect = False
self.tv.ShowLines = True
self.tv.ShowPlusMinus = True
self.tv.Scrollable = True
self.tv.AfterSelect += self.AfterSelect
expand = Button()
expand.Parent = self
expand.Location = Point(20, self.tv.Bottom + 20)
expand.Text = 'Expand'
expand.Anchor = AnchorStyles.Left | AnchorStyles.Top
expand.Click += self.OnExpand
expandAll = Button()
expandAll.Parent = self
expandAll.Location = Point(20, expand.Bottom + 5)
expandAll.Text = 'ExpandAll'
expandAll.Anchor = AnchorStyles.Left | AnchorStyles.Top
expandAll.Click += self.OnExpandAll
collapse = Button()
collapse.Parent = self
collapse.Location = Point(expandAll.Right + 5, expand.Top)
collapse.Text = 'Collapse'
collapse.Anchor = AnchorStyles.Left | AnchorStyles.Top
collapse.Click += self.OnCollapse
collapseAll = Button()
collapseAll.Parent = self
collapseAll.Location = Point(collapse.Left, collapse.Bottom + 5)
collapseAll.Text = 'CollapseAll'
collapseAll.Anchor = AnchorStyles.Left | AnchorStyles.Top
collapseAll.Click += self.OnCollapseAll
self.s b = StatusBar()
self.s b.Parent = self
self.ShowDirectories(self.tv.Nodes, HOME_DIR)
self.ResumeLayout()
self.CenterToScreen()
def AfterSelect(self, sender, event):
self.s b.Text = event.Node.Text
def ShowDirectories(self, trvNode, path):
dirInfo = DirectoryInfo(path)
if (dirInfo != None):
subDirs = dirInfo.GetDirectories()
tr = TreeNode(dirInfo.Name)
if (subDirs.Length > 0):
for dr in subDirs:
ifnot dr.Name.StartsWith("."):
self.ShowDirectories(tr.Nodes, dr.FullName)
trvNode.Add(tr)
def OnExpand(self, sender, event):
self.tv.SelectedNode.Expand()
def OnExpandAll(self, sender, event):
self.tv.ExpandAll()
def OnCollapse(self, sender, event):
self.tv.SelectedNode.Collapse()
def OnCollapseAll(self, sender, event):
self.tv.CollapseAll()
Application.Run(IForm())
效果展示:
完整的本地电子版本参阅入门1的其他。