本入门教程中的所有案例均已测试通过,具体参考入门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 游戏——贪吃蛇
目标:制作检查菜单栏
本案例中,展示了检查菜单的用法,在勾选时,状态栏显示 Ready
,不勾选时,不显示。
# 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 Shortcut, MainMenu, MenuItem
from System.Drawing import Size
class IForm(Form):
def __init__(self):
self.Text = 'Simple Menu'
self.s b = StatusBar()
self.s b.Parent = self
self.s b.Text = "Ready"
mainMenu = MainMenu()
filem = mainMenu.MenuItems.Add("&File")
# Exit事件和快捷键的另一种方式
filem.MenuItems.Add(MenuItem("E&xit", self.OnExit, Shortcut.CtrlX))
view = mainMenu.MenuItems.Add("&View")
self.viewStatusBar = MenuItem("View StatusBar")
self.viewStatusBar.Checked = True
self.viewStatusBar.Click += self.ToggleStatusBar
view.MenuItems.Add(self.viewStatusBar)
self.Menu = mainMenu
self.Size = Size(250, 200)
self.CenterToScreen()
def OnExit(self, sender, event):
self.Close()
def ToggleStatusBar(self, sender, event):
check = self.viewStatusBar.Checked
if (check):
self.s b.Visible = False
self.viewStatusBar.Checked = False
else:
self.s b.Visible = True
self.viewStatusBar.Checked = True
IForm().ShowDialog()
效果展示 :
目标:菜单栏的子菜单栏前增加图标
本案例是在菜单栏的子菜单栏前增加图标,分隔符等。
# encoding: utf-8
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Application, Form, MenuStrip
from System.Windows.Forms import ToolStripMenuItem, ToolStripSeparator
from System.Drawing import Size, Image
path1 = r"D:\01-图片\icon-png\TrackBar-1.png"
path2 = r"D:\01-图片\icon-png\TrackBar-2.png"
path3 = r"D:\01-图片\icon-png\TrackBar-3.png"
class IForm(Form):
def __init__(self):
self.Text = 'MenuStrip'
self.Size = Size(250, 200)
menuStrip = MenuStrip()
titem1 = ToolStripMenuItem("File")
menuStrip.Items.Add(titem1)
titem2 = ToolStripMenuItem("Tools")
menuStrip.Items.Add(titem2)
subm1 = ToolStripMenuItem("New")
subm1.Image = Image.FromFile(path1)
titem1.DropDownItems.Add(subm1)
subm2 = ToolStripMenuItem("Open")
subm2.Image = Image.FromFile(path2)
titem1.DropDownItems.Add(subm2)
titem1.DropDownItems.Add(ToolStripSeparator())
subm3 = ToolStripMenuItem("Exit")
subm3.Image = Image.FromFile(path3)
titem1.DropDownItems.Add(subm3)
subm3.Click += self.OnExit
self.Controls.Add(menuStrip)
self.MainMenuStrip = menuStrip
self.CenterToScreen()
def OnExit(self, sender, event):
self.Close()
IForm().ShowDialog()
效果展示:
完整的本地电子版本参阅入门1的其他。