本入门教程中的所有案例均已测试通过,具体参考入门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")
clr.AddReference("System")
from System.Windows.Forms import Application, Form, Button, Panel
from System.Windows.Forms import DockStyle, AnchorStyles
from System.Drawing import Size, Point
WIDTH = 250
HEIGHT = 150
BUTTONS_SPACE = 15
PANEL_SPACE = 10
CLOSE_SPACE = 15
class IForm(Form):
def __init__(self):
self.Text = 'Buttons'
self.Size = Size(WIDTH, HEIGHT)
ok = Button()
PANEL_HEIGHT = ok.Height + PANEL_SPACE
panel = Panel()
panel.Height = PANEL_HEIGHT
panel.Dock = DockStyle.Bottom
panel.Parent = self
x = ok.Width * 2 + BUTTONS_SPACE
y = (PANEL_HEIGHT - ok.Height) / 2
ok.Text = "Ok"
ok.Parent = panel
ok.Location = Point(WIDTH-x, y)
ok.Anchor = AnchorStyles.Right
close = Button()
x = close.Width
close.Text = "Close"
close.Parent = panel
close.Location = Point(WIDTH-x-CLOSE_SPACE, y)
close.Anchor = AnchorStyles.Right
self.CenterToScreen()
IForm().ShowDialog()
效果展示:
目标:综合的案例,涉及菜单栏,按钮和骨架多种控件
设计包含:
1、菜单栏,下拉菜单等;
2、主体展示;
3、控制按钮,播放的进度等。
4、状态栏文字显示
# encoding: utf-8
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import Application, Form, Button, Panel
from System.Windows.Forms import DockStyle, AnchorStyles, StatusBar
from System.Windows.Forms import TrackBar, MainMenu, MenuItem
from System.Windows.Forms import FlatStyle, TickStyle, Shortcut
from System.Drawing import Size, Point, Bitmap, Color
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"
path4 = r"D:\01-图片\icon-png\TrackBar-4.png"
class IForm(Form):
def __init__(self):
self.Text = 'Player'
self.Size = Size(350, 280)
mainMenu = MainMenu()
filem = mainMenu.MenuItems.Add("&File")
playm = mainMenu.MenuItems.Add("&Play")
view = mainMenu.MenuItems.Add("&View")
tools = mainMenu.MenuItems.Add("&Tools")
favourites = mainMenu.MenuItems.Add("&Favourites")
help = mainMenu.MenuItems.Add("&Help")
filem.MenuItems.Add(MenuItem("E&xit",self.OnExit, Shortcut.CtrlX))
self.Menu = mainMenu
panel = Panel()
panel.Parent = self
panel.BackColor = Color.Black
panel.Dock = DockStyle.Fill
buttonPanel = Panel()
buttonPanel.Parent = self
buttonPanel.Height = 40
buttonPanel.Dock = DockStyle.Bottom
pause = Button()
pause.FlatStyle = FlatStyle.Popup
pause.Parent = buttonPanel
pause.Location = Point(5, 10)
pause.Size = Size(25, 25)
pause.Image = Bitmap(path1)
play = Button()
play.FlatStyle = FlatStyle.Popup
play.Parent = buttonPanel
play.Location = Point(35, 10)
play.Size = Size(25, 25)
play.Image = Bitmap(path2)
forward = Button()
forward.FlatStyle = FlatStyle.Popup
forward.Parent = buttonPanel
forward.Location = Point(80, 10)
forward.Size = Size(25, 25)
forward.Image = Bitmap(path3)
backward = Button()
backward.FlatStyle = FlatStyle.Popup
backward.Parent = buttonPanel
backward.Location = Point(110, 10)
backward.Size = Size(25, 25)
backward.Image = Bitmap(path4)
tb = TrackBar()
tb.Parent = buttonPanel
tb.TickStyle = TickStyle.None
tb.Size = Size(150, 25)
tb.Location = Point(200, 10)
tb.Anchor = AnchorStyles.Right
audio = Button()
audio.FlatStyle = FlatStyle.Popup
audio.Parent = buttonPanel
audio.Size = Size(25, 25)
audio.Image = Bitmap(path1)
audio.Location = Point(170, 10)
audio.Anchor = AnchorStyles.Right
s b = StatusBar()
s b.Parent = self
s b.Text = "Ready"
self.CenterToScreen()
def OnExit(self, sender, event):
self.Close()
IForm().ShowDialog()
效果展示:
完整的本地电子版本参阅入门1中的其他。