本入门教程中的所有案例均已测试通过,具体参考入门1中的使用方法。
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
from System.Windows.Forms import Keys, MenuStrip, ToolStripMenuItem
from System.Drawing import Size
class IForm(Form):
def __init__(self):
self.Text = 'Simple Menu'
self.Size = Size(250, 200)
ms = MenuStrip()
ms.Parent = self
filem = ToolStripMenuItem("&File")
exit = ToolStripMenuItem("&Exit", None, self.OnExit)
# exit的快捷键
exit.ShortcutKeys = Keys.Control | Keys.X
# filem下添加eixt子菜单
filem.DropDownItems.Add(exit)
ms.Items.Add(filem)
self.MainMenuStrip = ms
self.CenterToScreen()
def OnExit(self, sender, event):
self.Close()
IForm().ShowDialog()
退出可以通过点击退出按钮或者快捷键 Ctrl+X
效果展示:
目标:编写多级子菜单
本案例展示如何编写一级及多级子菜单。
# 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 MenuStrip, ToolStripMenuItem
from System.Drawing import Size
class IForm(Form):
def __init__(self):
self.Text = 'Simple Menu'
self.Size = Size(380, 200)
ms = MenuStrip()
ms.Parent = self
filem = ToolStripMenuItem("&File")
# 添加exit功能事件
exit = ToolStripMenuItem("&Exit", None, self.OnExit)
importm = ToolStripMenuItem()
importm.Text = "Import"
# File添加下级菜单
filem.DropDownItems.Add(importm)
temp = ToolStripMenuItem()
temp.Text = "Import newsfeed list..."
importm.DropDownItems.Add(temp)
temp = ToolStripMenuItem()
temp.Text = "Import bookmarks..."
importm.DropDownItems.Add(temp)
temp = ToolStripMenuItem()
temp.Text = "Import mail..."
importm.DropDownItems.Add(temp)
filem.DropDownItems.Add(exit)
ms.Items.Add(filem)
self.MainMenuStrip = ms
self.CenterToScreen()
def OnExit(self, sender, event):
self.Close()
IForm().ShowDialog()
效果展示
完整的本地电子版本参阅入门1中的其他。