- 浏览: 2541800 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
wxpython入门(七)sizer布局和图像处理
参考书籍
http://wiki.woodpecker.org.cn/moin/WxPythonInAction
第十一章 使用sizer放置窗口部件
sizer是一个对象,它唯一的目的就是管理容器中的窗口部件的布局。sizer本身不是一个容器或一个窗口部件。它只是一个屏幕布局的算法。所有的sizer都是抽象类wx.Sizer的一个子类的实例。
wxPython中预定义的sizer:
Grid:一个十分基础的网格布局。当你要放置的窗口部件都是同样的尺寸且整齐地放入一个规则的网格中是使用它。
Flex grid:对grid sizer稍微做了些改变,当窗口部件有不同的尺寸时,可以有更好的结果。
Grid bag:grid sizer系列中最灵活的成员。使得网格中的窗口部件可以更随意的放置。
Box:在一条水平或垂直线上的窗口部件的布局。当尺寸改变时,在控制窗口部件的的行为上很灵活。通常用于嵌套的样式。可用于几乎任何类型的布局。
Static box:一个标准的box sizer。带有标题和环线。
基本的sizer:grid
块状窗口,examples:
import wx
class BlockWindow(wx.Panel):
def __init__(self,parent,ID=-1,label="",pos=wx.DefaultPosition, size=(100, 25)):
wx.Panel.__init__(self,parent,ID,pos,size,wx.RAISED_BORDER, label)
self.label = label
self.SetBackgroundColour("white")
self.SetMinSize(size)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
sz = self.GetClientSize()
dc = wx.PaintDC(self)
w,h = dc.GetTextExtent(self.label)
dc.SetFont(self.GetFont())
dc.DrawText(self.label, (sz.width-w)/2, (sz.height-h)/2)
最简单的grid sizer,examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four five six seven eight nine".split()
class GridSizerFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Basic Grid Sizer")
sizer = wx.GridSizer(rows=3, cols=3, hgap=5, vgap=5)#创建grid sizer
for label in labels:
bw = BlockWindow(self, label=label)
sizer.Add(bw, 0, 0)#添加窗口部件到sizer
self.SetSizer(sizer)#把sizer与框架关联起来
self.Fit()
app = wx.PySimpleApp()
GridSizerFrame().Show()
app.MainLoop()
设置grid的sizer,examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four five six seven eight nine".split()
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "GridSizer Test")
sizer = wx.GridSizer(rows=3, cols=3, hgap=5, vgap=5)
for label in labels:
bw = BlockWindow(self, label=label)
sizer.Add(bw, 0, 0)
center = self.FindWindowByName("five")
center.SetMinSize((150,50))
self.SetSizer(sizer)
self.Fit()
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()
添加边框设置,examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four five six seven eight nine".split()
#边框标记
flags = {"one": wx.BOTTOM, "two": wx.ALL, "three": wx.TOP,
"four": wx.LEFT, "five": wx.ALL, "six": wx.RIGHT,
"seven": wx.BOTTOM | wx.TOP, "eight": wx.ALL,
"nine": wx.LEFT | wx.RIGHT}
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "GridSizer Borders")
sizer = wx.GridSizer(rows=3, cols=3, hgap=5, vgap=5)
for label in labels:
bw = BlockWindow(self, label=label)
flag = flags.get(label, 0)
sizer.Add(bw, 0, flag, 10)#添加指定边框的窗口部件
self.SetSizer(sizer)
self.Fit()
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()
flex grid sizer
基本上和grid相同,flex grid sizer是grid sizer的一个更灵活的版本,增强功能如下:
1、每行和每列可以有各自的尺寸。
2、默认情况下,当尺寸调整时,它不改变它的单元格的尺寸。如果需要的话,你可以指定哪行或哪列应该增长。
3、它可以在两个方向之一灵活地增长,意思是你可以为个别的子元素指定比列量,并且你可以指定固定方向上的行为。
examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four five six seven eight nine".split()
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "FlexGridSizer")
sizer = wx.FlexGridSizer(rows=3, cols=3, hgap=5, vgap=5)
for label in labels:
bw = BlockWindow(self, label=label)
sizer.Add(bw, 0, 0)
center = self.FindWindowByName("five")
center.SetMinSize((150,50))
self.SetSizer(sizer)
self.Fit()
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()
grid bag sizer
grid bag sizer是对flex grid sizer进一步的增强,增强内容如下:
1、能够将一个窗口部件添加到一个特定的单元格。
2、能够使一个窗口部件跨越几个单元格(就像HTML表单中的表格所能做的一样)
examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four five six seven eight nine".split()
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "GridBagSizer Test")
sizer = wx.GridBagSizer(hgap=5, vgap=5)
for col in range(3):
for row in range(3):
bw = BlockWindow(self, label=labels[row*3 + col])
sizer.Add(bw, pos=(row,col))
# 跨行
bw = BlockWindow(self, label="span 3 rows")
sizer.Add(bw, pos=(0,3), span=(3,1), flag=wx.EXPAND)
# 跨列
bw = BlockWindow(self, label="span all columns")
sizer.Add(bw, pos=(3,0), span=(1,4), flag=wx.EXPAND)
# 使最后的行和列可增长
sizer.AddGrowableCol(3)
sizer.AddGrowableRow(3)
self.SetSizer(sizer)
self.Fit()
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()
box类型
一个box sizer是一个垂直列或水平行,窗口部件在其中从左至右或从上到下布置在一条线上。
wx.BoxSizer的构造函数:
wx.BoxSizer(orient)
参数orient代表该sizer的方向,它的取值可以是wx.VERTICAL或wx.HORIZONTAL。examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four".split()
class TestFrame(wx.Frame):
title = "none"
def __init__(self):
wx.Frame.__init__(self, None, -1, self.title)
sizer = self.CreateSizerAndWindows()
self.SetSizer(sizer)
self.Fit()
class VBoxSizerFrame(TestFrame):
title = "VerticalBoxSizer"
def CreateSizerAndWindows(self):
sizer = wx.BoxSizer(wx.VERTICAL)
for label in labels:
bw = BlockWindow(self,label=label,size=(200,30))
sizer.Add(bw, flag=wx.EXPAND)
return sizer
class HBoxSizerFrame(TestFrame):
title = "HorizontalBoxSizer"
def CreateSizerAndWindows(self):
sizer = wx.BoxSizer(wx.HORIZONTAL)
for label in labels:
bw = BlockWindow(self, label=label, size=(75,30))
sizer.Add(bw, flag=wx.EXPAND)
return sizer
class VBoxSizerStretchableFrame(TestFrame):
title = "StretchableBoxSizer"
def CreateSizerAndWindows(self):
sizer = wx.BoxSizer(wx.VERTICAL)
for label in labels:
bw = BlockWindow(self, label=label, size=(200,30))
sizer.Add(bw, flag=wx.EXPAND)
# Add an item that takes all the free space
bw = BlockWindow(self, label="getsAllFreeSpace", size=(200,30))
sizer.Add(bw, 1, flag=wx.EXPAND)
return sizer
class VBoxSizerMultiProportionalFrame(TestFrame):
title = "ProportionalBoxSizer"
def CreateSizerAndWindows(self):
sizer = wx.BoxSizer(wx.VERTICAL)
for label in labels:
bw = BlockWindow(self, label=label, size=(200,30))
sizer.Add(bw, flag=wx.EXPAND)
# Add an item that takes one share of the free space
bw = BlockWindow(self,label="gets 1/4 of the free space",size=(200,30))
sizer.Add(bw, 1, flag=wx.EXPAND)
# Add an item that takes 2 shares of the free space
bw = BlockWindow(self,label="gets 3/4 of the free space",size=(200,30))
sizer.Add(bw, 3, flag=wx.EXPAND)
return sizer
app = wx.PySimpleApp()
frameList = [VBoxSizerFrame, HBoxSizerFrame,
VBoxSizerStretchableFrame,
VBoxSizerMultiProportionalFrame]
for klass in frameList:
frame = klass()
frame.Show()
app.MainLoop()
static box类型
一个static box sizer合并了box sizer和静态框(static box),静态框在sizer的周围提供了一个漂亮的边框和文本标签。
wx.StaticBoxSizer是wx.BoxSizer的子类。它的构造函数要求的参数是静态框和方向:
wx.StaticBoxSizer(box, oient)
box = wx.StaticBox(self.panel, -1, boxlabel)
examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four five six seven eight nine".split()
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "StaticBoxSizerTest")
self.panel = wx.Panel(self)
# make three static boxes with windows positioned inside them
box1 = self.MakeStaticBoxSizer("Box1", labels[0:3])
box2 = self.MakeStaticBoxSizer("Box2", labels[3:6])
box3 = self.MakeStaticBoxSizer("Box3", labels[6:9])
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(box1, 0, wx.ALL, 10)
sizer.Add(box2, 0, wx.ALL, 10)
sizer.Add(box3, 0, wx.ALL, 10)
self.panel.SetSizer(sizer)
sizer.Fit(self)
def MakeStaticBoxSizer(self, boxlabel, itemlabels):
# first the static box
box = wx.StaticBox(self.panel, -1, boxlabel)
# then the sizer
sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
# then add items to it like normal
for label in itemlabels:
bw = BlockWindow(self.panel, label=label)
sizer.Add(bw, 0, wx.ALL, 2)
return sizer
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()
实际综合使用示例
examples:
import wx
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Real World Test")
panel = wx.Panel(self)
# First create the controls
topLbl = wx.StaticText(panel, -1, "AccountInformation")
#1 创建窗口部件
topLbl.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
nameLbl = wx.StaticText(panel,-1,"Name:")
name = wx.TextCtrl(panel,-1,"");
addrLbl = wx.StaticText(panel,-1,"Address:")
addr1 = wx.TextCtrl(panel,-1,"");
addr2 = wx.TextCtrl(panel,-1,"");
cstLbl = wx.StaticText(panel,-1,"City,State,Zip:")
city = wx.TextCtrl(panel,-1,"",size=(150,-1));
state = wx.TextCtrl(panel,-1,"",size=(50,-1));
zip = wx.TextCtrl(panel,-1,"",size=(70,-1));
phoneLbl = wx.StaticText(panel,-1,"Phone:")
phone = wx.TextCtrl(panel,-1,"");
emailLbl = wx.StaticText(panel,-1,"Email:")
email = wx.TextCtrl(panel,-1,"");
saveBtn = wx.Button(panel,-1,"Save")
cancelBtn = wx.Button(panel,-1,"Cancel")
# Now do the layout.
# mainSizer is the top-level one that manages everything
#2 垂直的sizer
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(topLbl, 0, wx.ALL, 5)
mainSizer.Add(wx.StaticLine(panel), 0,
wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
# addrSizer is a grid that holds all of the address info
#3 地址列
addrSizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
addrSizer.AddGrowableCol(1)
addrSizer.Add(nameLbl,0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add(name,0,wx.EXPAND)
addrSizer.Add(addrLbl,0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add(addr1, 0, wx.EXPAND)
#4 带有空白空间的行
addrSizer.Add((10,10)) # some empty space
addrSizer.Add(addr2, 0, wx.EXPAND)
addrSizer.Add(cstLbl, 0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
# the city, state, zip fields are in a sub-sizer
#5 水平嵌套
cstSizer = wx.BoxSizer(wx.HORIZONTAL)
cstSizer.Add(city, 1)
cstSizer.Add(state, 0, wx.LEFT|wx.RIGHT, 5)
cstSizer.Add(zip)
addrSizer.Add(cstSizer, 0, wx.EXPAND)
#6 电话和电子邮箱
addrSizer.Add(phoneLbl,0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add(phone,0,wx.EXPAND)
addrSizer.Add(emailLbl,0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add(email, 0, wx.EXPAND)
# now add the addrSizer to the mainSizer
#7 添加Flex sizer
mainSizer.Add(addrSizer, 0, wx.EXPAND|wx.ALL, 10)
# The buttons sizer will put them in a row with resizeable
# gaps between and on either side of the buttons
#8 按钮行
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
btnSizer.Add((20,20), 1)
btnSizer.Add(saveBtn)
btnSizer.Add((20,20), 1)
btnSizer.Add(cancelBtn)
btnSizer.Add((20,20), 1)
mainSizer.Add(btnSizer, 0, wx.EXPAND|wx.BOTTOM, 10)
panel.SetSizer(mainSizer)
# Fit the frame to the needs of the sizer. The frame will
# automatically resize the panel as needed. Also prevent the
# frame from getting smaller than this size.
mainSizer.Fit(self)
mainSizer.SetSizeHints(self)
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()
第十二章 处理基本图像
使用图像工作
与平台无关的图像处理由类wx.Image管理,而与平台有关的图像处理由类wx.Bitmap管理。实际上,意思就是外部文件格式由wx.Image装载和保存,而wx.Bitmap负责将图像显示到屏幕。
wx.Image的构造函数:
wx.Image(name, type=wx.BITMAP_TYPE_ANY, index=-1)
载入并缩放简单图像,examples:
import wx
filenames = ["image.bmp", "image.gif", "image.jpg", "image.png" ]
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Loading Images")
p = wx.Panel(self)
fgs = wx.FlexGridSizer(cols=2, hgap=10, vgap=10)
for name in filenames:
#1 从文件载入图像
img1 = wx.Image(name, wx.BITMAP_TYPE_ANY)
# Scale the oiginal to another wx.Image
w = img1.GetWidth()
h = img1.GetHeight()
img2 = img1.Scale(w/2, h/2)#2 缩小图像
#3 转换它们为静态位图部件
sb1 = wx.StaticBitmap(p, -1, wx.BitmapFromImage(img1))
sb2 = wx.StaticBitmap(p, -1, wx.BitmapFromImage(img2))
# and put them into the sizer
fgs.Add(sb1)
fgs.Add(sb2)
p.SetSizerAndFit(fgs)
self.Fit()
app = wx.PySimpleApp()
frm = TestFrame()
frm.Show()
app.MainLoop()
图片处理(略)
参考书籍
http://wiki.woodpecker.org.cn/moin/WxPythonInAction
第十一章 使用sizer放置窗口部件
sizer是一个对象,它唯一的目的就是管理容器中的窗口部件的布局。sizer本身不是一个容器或一个窗口部件。它只是一个屏幕布局的算法。所有的sizer都是抽象类wx.Sizer的一个子类的实例。
wxPython中预定义的sizer:
Grid:一个十分基础的网格布局。当你要放置的窗口部件都是同样的尺寸且整齐地放入一个规则的网格中是使用它。
Flex grid:对grid sizer稍微做了些改变,当窗口部件有不同的尺寸时,可以有更好的结果。
Grid bag:grid sizer系列中最灵活的成员。使得网格中的窗口部件可以更随意的放置。
Box:在一条水平或垂直线上的窗口部件的布局。当尺寸改变时,在控制窗口部件的的行为上很灵活。通常用于嵌套的样式。可用于几乎任何类型的布局。
Static box:一个标准的box sizer。带有标题和环线。
基本的sizer:grid
块状窗口,examples:
import wx
class BlockWindow(wx.Panel):
def __init__(self,parent,ID=-1,label="",pos=wx.DefaultPosition, size=(100, 25)):
wx.Panel.__init__(self,parent,ID,pos,size,wx.RAISED_BORDER, label)
self.label = label
self.SetBackgroundColour("white")
self.SetMinSize(size)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
sz = self.GetClientSize()
dc = wx.PaintDC(self)
w,h = dc.GetTextExtent(self.label)
dc.SetFont(self.GetFont())
dc.DrawText(self.label, (sz.width-w)/2, (sz.height-h)/2)
最简单的grid sizer,examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four five six seven eight nine".split()
class GridSizerFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Basic Grid Sizer")
sizer = wx.GridSizer(rows=3, cols=3, hgap=5, vgap=5)#创建grid sizer
for label in labels:
bw = BlockWindow(self, label=label)
sizer.Add(bw, 0, 0)#添加窗口部件到sizer
self.SetSizer(sizer)#把sizer与框架关联起来
self.Fit()
app = wx.PySimpleApp()
GridSizerFrame().Show()
app.MainLoop()
设置grid的sizer,examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four five six seven eight nine".split()
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "GridSizer Test")
sizer = wx.GridSizer(rows=3, cols=3, hgap=5, vgap=5)
for label in labels:
bw = BlockWindow(self, label=label)
sizer.Add(bw, 0, 0)
center = self.FindWindowByName("five")
center.SetMinSize((150,50))
self.SetSizer(sizer)
self.Fit()
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()
添加边框设置,examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four five six seven eight nine".split()
#边框标记
flags = {"one": wx.BOTTOM, "two": wx.ALL, "three": wx.TOP,
"four": wx.LEFT, "five": wx.ALL, "six": wx.RIGHT,
"seven": wx.BOTTOM | wx.TOP, "eight": wx.ALL,
"nine": wx.LEFT | wx.RIGHT}
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "GridSizer Borders")
sizer = wx.GridSizer(rows=3, cols=3, hgap=5, vgap=5)
for label in labels:
bw = BlockWindow(self, label=label)
flag = flags.get(label, 0)
sizer.Add(bw, 0, flag, 10)#添加指定边框的窗口部件
self.SetSizer(sizer)
self.Fit()
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()
flex grid sizer
基本上和grid相同,flex grid sizer是grid sizer的一个更灵活的版本,增强功能如下:
1、每行和每列可以有各自的尺寸。
2、默认情况下,当尺寸调整时,它不改变它的单元格的尺寸。如果需要的话,你可以指定哪行或哪列应该增长。
3、它可以在两个方向之一灵活地增长,意思是你可以为个别的子元素指定比列量,并且你可以指定固定方向上的行为。
examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four five six seven eight nine".split()
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "FlexGridSizer")
sizer = wx.FlexGridSizer(rows=3, cols=3, hgap=5, vgap=5)
for label in labels:
bw = BlockWindow(self, label=label)
sizer.Add(bw, 0, 0)
center = self.FindWindowByName("five")
center.SetMinSize((150,50))
self.SetSizer(sizer)
self.Fit()
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()
grid bag sizer
grid bag sizer是对flex grid sizer进一步的增强,增强内容如下:
1、能够将一个窗口部件添加到一个特定的单元格。
2、能够使一个窗口部件跨越几个单元格(就像HTML表单中的表格所能做的一样)
examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four five six seven eight nine".split()
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "GridBagSizer Test")
sizer = wx.GridBagSizer(hgap=5, vgap=5)
for col in range(3):
for row in range(3):
bw = BlockWindow(self, label=labels[row*3 + col])
sizer.Add(bw, pos=(row,col))
# 跨行
bw = BlockWindow(self, label="span 3 rows")
sizer.Add(bw, pos=(0,3), span=(3,1), flag=wx.EXPAND)
# 跨列
bw = BlockWindow(self, label="span all columns")
sizer.Add(bw, pos=(3,0), span=(1,4), flag=wx.EXPAND)
# 使最后的行和列可增长
sizer.AddGrowableCol(3)
sizer.AddGrowableRow(3)
self.SetSizer(sizer)
self.Fit()
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()
box类型
一个box sizer是一个垂直列或水平行,窗口部件在其中从左至右或从上到下布置在一条线上。
wx.BoxSizer的构造函数:
wx.BoxSizer(orient)
参数orient代表该sizer的方向,它的取值可以是wx.VERTICAL或wx.HORIZONTAL。examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four".split()
class TestFrame(wx.Frame):
title = "none"
def __init__(self):
wx.Frame.__init__(self, None, -1, self.title)
sizer = self.CreateSizerAndWindows()
self.SetSizer(sizer)
self.Fit()
class VBoxSizerFrame(TestFrame):
title = "VerticalBoxSizer"
def CreateSizerAndWindows(self):
sizer = wx.BoxSizer(wx.VERTICAL)
for label in labels:
bw = BlockWindow(self,label=label,size=(200,30))
sizer.Add(bw, flag=wx.EXPAND)
return sizer
class HBoxSizerFrame(TestFrame):
title = "HorizontalBoxSizer"
def CreateSizerAndWindows(self):
sizer = wx.BoxSizer(wx.HORIZONTAL)
for label in labels:
bw = BlockWindow(self, label=label, size=(75,30))
sizer.Add(bw, flag=wx.EXPAND)
return sizer
class VBoxSizerStretchableFrame(TestFrame):
title = "StretchableBoxSizer"
def CreateSizerAndWindows(self):
sizer = wx.BoxSizer(wx.VERTICAL)
for label in labels:
bw = BlockWindow(self, label=label, size=(200,30))
sizer.Add(bw, flag=wx.EXPAND)
# Add an item that takes all the free space
bw = BlockWindow(self, label="getsAllFreeSpace", size=(200,30))
sizer.Add(bw, 1, flag=wx.EXPAND)
return sizer
class VBoxSizerMultiProportionalFrame(TestFrame):
title = "ProportionalBoxSizer"
def CreateSizerAndWindows(self):
sizer = wx.BoxSizer(wx.VERTICAL)
for label in labels:
bw = BlockWindow(self, label=label, size=(200,30))
sizer.Add(bw, flag=wx.EXPAND)
# Add an item that takes one share of the free space
bw = BlockWindow(self,label="gets 1/4 of the free space",size=(200,30))
sizer.Add(bw, 1, flag=wx.EXPAND)
# Add an item that takes 2 shares of the free space
bw = BlockWindow(self,label="gets 3/4 of the free space",size=(200,30))
sizer.Add(bw, 3, flag=wx.EXPAND)
return sizer
app = wx.PySimpleApp()
frameList = [VBoxSizerFrame, HBoxSizerFrame,
VBoxSizerStretchableFrame,
VBoxSizerMultiProportionalFrame]
for klass in frameList:
frame = klass()
frame.Show()
app.MainLoop()
static box类型
一个static box sizer合并了box sizer和静态框(static box),静态框在sizer的周围提供了一个漂亮的边框和文本标签。
wx.StaticBoxSizer是wx.BoxSizer的子类。它的构造函数要求的参数是静态框和方向:
wx.StaticBoxSizer(box, oient)
box = wx.StaticBox(self.panel, -1, boxlabel)
examples:
import wx
from blockwindow import BlockWindow
labels = "one two three four five six seven eight nine".split()
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "StaticBoxSizerTest")
self.panel = wx.Panel(self)
# make three static boxes with windows positioned inside them
box1 = self.MakeStaticBoxSizer("Box1", labels[0:3])
box2 = self.MakeStaticBoxSizer("Box2", labels[3:6])
box3 = self.MakeStaticBoxSizer("Box3", labels[6:9])
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(box1, 0, wx.ALL, 10)
sizer.Add(box2, 0, wx.ALL, 10)
sizer.Add(box3, 0, wx.ALL, 10)
self.panel.SetSizer(sizer)
sizer.Fit(self)
def MakeStaticBoxSizer(self, boxlabel, itemlabels):
# first the static box
box = wx.StaticBox(self.panel, -1, boxlabel)
# then the sizer
sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
# then add items to it like normal
for label in itemlabels:
bw = BlockWindow(self.panel, label=label)
sizer.Add(bw, 0, wx.ALL, 2)
return sizer
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()
实际综合使用示例
examples:
import wx
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Real World Test")
panel = wx.Panel(self)
# First create the controls
topLbl = wx.StaticText(panel, -1, "AccountInformation")
#1 创建窗口部件
topLbl.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
nameLbl = wx.StaticText(panel,-1,"Name:")
name = wx.TextCtrl(panel,-1,"");
addrLbl = wx.StaticText(panel,-1,"Address:")
addr1 = wx.TextCtrl(panel,-1,"");
addr2 = wx.TextCtrl(panel,-1,"");
cstLbl = wx.StaticText(panel,-1,"City,State,Zip:")
city = wx.TextCtrl(panel,-1,"",size=(150,-1));
state = wx.TextCtrl(panel,-1,"",size=(50,-1));
zip = wx.TextCtrl(panel,-1,"",size=(70,-1));
phoneLbl = wx.StaticText(panel,-1,"Phone:")
phone = wx.TextCtrl(panel,-1,"");
emailLbl = wx.StaticText(panel,-1,"Email:")
email = wx.TextCtrl(panel,-1,"");
saveBtn = wx.Button(panel,-1,"Save")
cancelBtn = wx.Button(panel,-1,"Cancel")
# Now do the layout.
# mainSizer is the top-level one that manages everything
#2 垂直的sizer
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(topLbl, 0, wx.ALL, 5)
mainSizer.Add(wx.StaticLine(panel), 0,
wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
# addrSizer is a grid that holds all of the address info
#3 地址列
addrSizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
addrSizer.AddGrowableCol(1)
addrSizer.Add(nameLbl,0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add(name,0,wx.EXPAND)
addrSizer.Add(addrLbl,0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add(addr1, 0, wx.EXPAND)
#4 带有空白空间的行
addrSizer.Add((10,10)) # some empty space
addrSizer.Add(addr2, 0, wx.EXPAND)
addrSizer.Add(cstLbl, 0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
# the city, state, zip fields are in a sub-sizer
#5 水平嵌套
cstSizer = wx.BoxSizer(wx.HORIZONTAL)
cstSizer.Add(city, 1)
cstSizer.Add(state, 0, wx.LEFT|wx.RIGHT, 5)
cstSizer.Add(zip)
addrSizer.Add(cstSizer, 0, wx.EXPAND)
#6 电话和电子邮箱
addrSizer.Add(phoneLbl,0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add(phone,0,wx.EXPAND)
addrSizer.Add(emailLbl,0,wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add(email, 0, wx.EXPAND)
# now add the addrSizer to the mainSizer
#7 添加Flex sizer
mainSizer.Add(addrSizer, 0, wx.EXPAND|wx.ALL, 10)
# The buttons sizer will put them in a row with resizeable
# gaps between and on either side of the buttons
#8 按钮行
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
btnSizer.Add((20,20), 1)
btnSizer.Add(saveBtn)
btnSizer.Add((20,20), 1)
btnSizer.Add(cancelBtn)
btnSizer.Add((20,20), 1)
mainSizer.Add(btnSizer, 0, wx.EXPAND|wx.BOTTOM, 10)
panel.SetSizer(mainSizer)
# Fit the frame to the needs of the sizer. The frame will
# automatically resize the panel as needed. Also prevent the
# frame from getting smaller than this size.
mainSizer.Fit(self)
mainSizer.SetSizeHints(self)
app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()
第十二章 处理基本图像
使用图像工作
与平台无关的图像处理由类wx.Image管理,而与平台有关的图像处理由类wx.Bitmap管理。实际上,意思就是外部文件格式由wx.Image装载和保存,而wx.Bitmap负责将图像显示到屏幕。
wx.Image的构造函数:
wx.Image(name, type=wx.BITMAP_TYPE_ANY, index=-1)
载入并缩放简单图像,examples:
import wx
filenames = ["image.bmp", "image.gif", "image.jpg", "image.png" ]
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Loading Images")
p = wx.Panel(self)
fgs = wx.FlexGridSizer(cols=2, hgap=10, vgap=10)
for name in filenames:
#1 从文件载入图像
img1 = wx.Image(name, wx.BITMAP_TYPE_ANY)
# Scale the oiginal to another wx.Image
w = img1.GetWidth()
h = img1.GetHeight()
img2 = img1.Scale(w/2, h/2)#2 缩小图像
#3 转换它们为静态位图部件
sb1 = wx.StaticBitmap(p, -1, wx.BitmapFromImage(img1))
sb2 = wx.StaticBitmap(p, -1, wx.BitmapFromImage(img2))
# and put them into the sizer
fgs.Add(sb1)
fgs.Add(sb2)
p.SetSizerAndFit(fgs)
self.Fit()
app = wx.PySimpleApp()
frm = TestFrame()
frm.Show()
app.MainLoop()
图片处理(略)
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 468NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 328Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 376Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 465NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 413Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 330Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 242GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 443GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 320GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 285Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 302Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 278NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 256Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 565NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 256Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 361Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 363Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
### wxPython入门知识点详解 #### 1. 欢迎来到wxPython - **简介**:wxPython是一种广泛使用的Python库,它允许开发者利用wxWidgets C++库来创建跨平台的图形用户界面(GUI)。wxPython是免费且开源的,支持...
wxPython入门教程前3章 wxPython入门教程前3章 wxPython入门教程前3章 wxPython入门教程前3章
这个demo做得相当好(直接运行即可),仔细看过之后你会发觉在wxPython中用sizer调整窗口部件的布局是如此的简单,灵活以及强大.值得推荐
本文将详细介绍wxPython中的两种主要布局方式:绝对定位和sizers,并重点讲解如何使用sizers来实现更加灵活和响应式的布局。 #### 二、绝对定位(Absolute Positioning) **绝对定位**是一种直接指定窗口内各个组件...
几本很不错的关于python gui的wxPython的书,包括“wxPython in Action(中文版)” 活学活用wxPython “《wxPython in Action》Noel Rappin, Robin Dunn著 2006年版”
总结来说,《活学活用wxPython的有图版第十一章》通过有图示例的方式,让开发者能够直观地学习和理解wxPython中sizer的使用和布局管理。通过对基本概念的解释、具体方法的演示和实际案例的分析,本章内容对希望提高...
Python 窗口应用主要涉及的是图形用户界面(GUI)的...总的来说,wxPython提供了丰富的组件和事件处理机制,使得开发者可以方便地创建复杂的窗口应用程序。学习和熟练掌握wxPython,将极大地提升Python GUI开发的能力。
无论是对于初学者还是有经验的开发者,wxPython都是一个值得学习和使用的框架。如果你对桌面应用开发感兴趣,尤其是希望在多种操作系统上运行应用,那么wxPython是一个理想的选择。通过不断学习和实践,你将能够利用...
综上所述,《wxPython in Action》这本书系统地介绍了wxPython的基础知识和高级主题,涵盖了从入门到精通所需的所有知识点。无论是初学者还是有一定经验的开发者,都可以从中获益匪浅。通过学习本书,开发者能够掌握...
5. **高级特性**:除了基本功能外,wxPython还包括图表、图像处理、多线程支持等高级特性。这些功能可以让你创建出更丰富、功能更强大的应用程序。 6. **示例解析**:通过`wxPython-demo-4.0.3`中的示例,你可以...
2. **图像处理**:wxPython支持显示和操作位图、JPEG、PNG等图像格式。 3. **打印与预览**:wxPython提供了打印支持,包括打印文档、设置页边距和预览等功能。 4. **国际化(Internationalization, i18n)**:...
使用sizer是wxPython中一个重要的布局技术,它能够灵活地管理窗体中的控件布局,而操作基本图像则涉及到GUI中的图形处理。此外,wxPython还提供了打印框架,允许用户打印窗体内容,并且可以通过扩展的其他功能来增强...
4. **图像和媒体支持**: 展示如何在WxPython中加载和显示图像,甚至处理音频和视频。 5. **高级控件和布局**: 示例可能涉及滑块、列表框、树形控件等,以及更复杂的布局设计。 6. **事件处理和回调**: 实现更复杂的...
【wxPython Demo】是wxPython库附带的一系列示例和教程,用于展示如何使用wxPython的各种控件、布局管理器和其他功能。通过这些演示,开发者可以直观地了解如何创建窗口、按钮、菜单、对话框等元素,并了解它们在...
这个测试程序有助于我们理解wxPython的界面设计,基本的控件和事件调用都有,这个代码可以让初学者了解,界面的设计过程。建议观看……
2. Core模块包含基础类,如Object类,Sizer布局管理器,事件处理和几何图形类。 3. GDI模块用于在组件上进行绘图,包括字体、颜色、画笔、画刷和图像操作。 4. Misc模块包括日志、配置、系统设置和其他辅助功能。 5....
8. **图像和媒体支持**:wxPython支持加载和显示图像,甚至可以处理音频和视频,这使得创建多媒体应用成为可能。 9. **国际化(i18n)**:wxPython提供了国际化的支持,允许开发者轻松地将应用翻译成多种语言。 ...
在“数字图像处理大作业GUI设计.pdf”文件中,可能会详细讲解如何利用这些技术和工具来设计和实现GUI,包括代码示例、界面布局建议以及可能遇到的问题和解决策略。通过完成这个大作业,学习者不仅能深化对数字图像...
Python WxPython开源扫雷游戏PyMine为开源扫雷游戏PyMine 使用Python语言和WxPython UI框架。本例移植自开源例程JMine 请在程序所在目录使用python PyMine.py启动例程需要先安装Python 3.11和wxPython 4.2Python ...
本书涵盖了从基础到高级的多个主题,包括创建窗口和控件、布局管理、事件处理、图像显示、对话框和文件操作等核心概念。通过1-18章的系统讲解,读者可以逐步构建起对wxPython全面的理解。 在第一章中,读者将被引入...