- 浏览: 2488574 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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入门(三)重构、类
参考书籍
http://wiki.woodpecker.org.cn/moin/WxPythonInAction
第四章 用PyCrust使得wxPython更易处理
如何与wxPython程序交互
例4.1 简单的Python交互式会话
C:\Documents and Settings\sillycat>python
ActivePython 2.6.4.10 (ActiveState Software Inc.) based on
Python 2.6.4 (r264:75706, Jan 22 2010, 16:41:54) [MSC v.1500 32 bit (Intel)] on
win32Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 2
4
>>> 7 * 6
42
PyCrust配置了标准的Python shell
图形化的python工具,如IDLE看起来很像命令行Python shell,但是它有额外的特性如调用提示。PyCrust的目的之一就是提供所有现存的Python shell的特性。
如何将PyCrust应用于wxPython应用程序
在我的测试目录下,新建py文件WxPythonWindow.py,建立一个空的窗口:
import wx
class Frame(wx.Frame):
pass
class App(wx.App):
def OnInit(self):
self.frame = Frame(parent=None, id=-1, title='Spare')
self.frame.Show()
self.SetTopWindow(self.frame)
return True
if __name__ == '__main__':
app = App()
app.MainLoop()
在相同目录下,建立PyWrap.py文件:
__author__ = "Patrick K. O'Brien pobrien@orbtech.com "
__cvsid__ = "$Id: PyCrust.txt,v 1.15 2005/03/29 23:39:27 robind Exp $"
__revision__ = "$Revision: 1.15 $"[11:-2]
import os
import sys
import wx
from wx.py.crust import CrustFrame
def wrap(app):
wx.InitAllImageHandlers()
frame = CrustFrame()
frame.SetSize((750, 525))
frame.Show(True)
frame.shell.interp.locals['app'] = app
app.MainLoop()
def main(modulename=None):
sys.path.insert(0, os.curdir)
if not modulename:
if len(sys.argv) < 2:
print "Please specify a module name."
raise SystemExit
modulename = sys.argv[1]
if modulename.endswith('.py'):
modulename = modulename[:-3]
module = __import__(modulename)
# Find the App class.
App = None
d = module.__dict__
for item in d.keys():
try:
if issubclass(d[item], wx.App):
App = d[item]
except (NameError, TypeError):
pass
if App is None:
print "No App class was found."
raise SystemExit
app = App()
wrap(app)
if __name__ == '__main__':
main()
执行命令:
D:\work\easyfinance\finance\com\sillycat\finance>python PyWrap.py WxPythonWindow.py
打开了,本来的Spare窗口,和PyCrust窗口,然后在PyCrust窗口输入如下命令,改变spare的属性和状态,在PyCrust的命令行下执行的过程如下:
import wx
app.frame.panel = wx.Panel(parent=app.frame)
app.frame.panel.SetBackgroundColour('White')
True
app.frame.panel.Refresh()
#改变了背景为白色
app.frame.StatusBar = app.frame.CreateStatusBar(number=3)
app.frame.StatusBar.SetStatusText("left",0)
app.frame.StatusBar.SetStatusText("center",1)
app.frame.StatusBar.SetStatusText("right",2)
#增加了状态栏
app.frame.MenuBar = wx.MenuBar()
menu = wx.Menu()
app.frame.MenuBar.Append(menu,"Primary")
True
app.frame.SetMenuBar(app.frame.MenuBar)
menu.Append(wx.NewId(),"One","first menu item")
<wx._core.MenuItem; proxy of <Swig Object of type 'wxMenuItem *' at 0x1ca1b28> >
menu.Append(wx.NewId(),"Two","second menu item")
<wx._core.MenuItem; proxy of <Swig Object of type 'wxMenuItem *' at 0x1d08de8> >
#增加了菜单
在平时我们编写程序时,我们可以利用到PyCrust里面的模块,比如shell,filling等,但是我目前用不到。
第五章 绘制蓝图
重构帮助我们改进代码
examples:
import wx
class RefactorExample(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Refactor Example',size=(340, 200))
panel = wx.Panel(self, -1)
panel.SetBackgroundColour("White")
prevButton = wx.Button(panel, -1, "PREV", pos=(80, 0))
self.Bind(wx.EVT_BUTTON, self.OnPrev, prevButton)
nextButton = wx.Button(panel, -1, "NEXT", pos=(160, 0))
self.Bind(wx.EVT_BUTTON, self.OnNext, nextButton)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
menuBar = wx.MenuBar()
menu1 = wx.Menu()
openMenuItem = menu1.Append(-1, "open", "Open")
self.Bind(wx.EVT_MENU, self.OnOpen, openMenuItem)
quitMenuItem = menu1.Append(-1, "quit", "Quit")
self.Bind(wx.EVT_MENU, self.OnCloseWindow, quitMenuItem)
menuBar.Append(menu1, "menu1")
menu2 = wx.Menu()
copyItem = menu2.Append(-1, "copy", "Copy")
self.Bind(wx.EVT_MENU, self.OnCopy, copyItem)
cutItem = menu2.Append(-1, "cut", "Cut")
self.Bind(wx.EVT_MENU, self.OnCut, cutItem)
pasteItem = menu2.Append(-1, "Paste", "Paste")
self.Bind(wx.EVT_MENU, self.OnPaste, pasteItem)
menuBar.Append(menu2, "menu2")
self.SetMenuBar(menuBar)
static = wx.StaticText(panel,wx.NewId(),"First Name",pos=(10, 50))
static.SetBackgroundColour("White")
text = wx.TextCtrl(panel, wx.NewId(),"",size=(100, -1),pos=(80, 50))
static2 = wx.StaticText(panel, wx.NewId(), "Last Name",pos=(10, 80))
static2.SetBackgroundColour("White")
text2 = wx.TextCtrl(panel, wx.NewId(), "", size=(100, -1),pos=(80, 80))
firstButton = wx.Button(panel, -1, "FIRST")
self.Bind(wx.EVT_BUTTON,self.OnFirst, firstButton)
menu2.AppendSeparator()
optItem = menu2.Append(-1, "display", "Display Options")
self.Bind(wx.EVT_MENU, self.OnOptions, optItem)
lastButton = wx.Button(panel, -1, "LAST", pos=(240, 0))
self.Bind(wx.EVT_BUTTON, self.OnLast, lastButton)
# Just grouping the empty event handlers together
def OnPrev(self, event): pass
def OnNext(self, event): pass
def OnLast(self, event): pass
def OnFirst(self, event): pass
def OnOpen(self, event): pass
def OnCopy(self, event): pass
def OnCut(self, event): pass
def OnPaste(self, event): pass
def OnOptions(self, event): pass
def OnCloseWindow(self, event):
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = RefactorExample(parent=None, id=-1)
frame.Show()
app.MainLoop()
重构注意
example 按钮栏作为一个单独的方法:
def createButtonBar(self):
firstButton = wx.Button(panel, -1, "FIRST")
self.Bind(wx.EVT_BUTTON, self.OnFirst, firstButton)
prevButton = wx.Button(panel, -1, "PREV", pos=(80, 0))
self.Bind(wx.EVT_BUTTON, , self.OnPrev, prevButton)
nextButton = wx.Button(panel, -1, "NEXT", pos=(160, 0))
self.Bind(wx.EVT_BUTTON, self.OnNext, nextButton)
lastButton = wx.Button(panel, -1, "LAST", pos=(240, 0))
self.Bind(wx.EVT_BUTTON, self.OnLast, lastButton)
提炼公用方法,examples:
def createButtonBar(self, panel):
self.buildOneButton(panel, "First", self.OnFirst)
self.buildOneButton(panel, "PREV", self.OnPrev, (80, 0))
self.buildOneButton(panel, "NEXT", self.OnNext, (160, 0))
self.buildOneButton(panel, "Last", self.OnLast, (240, 0))
def buildOneButton(self, parent, label, handler, pos=(0,0)):
button = wx.Button(parent, -1, label, pos)
self.Bind(wx.EVT_BUTTON, handler, button)
return button
让方法和数据分离,example:
def buttonData(self):
return (("First", self.OnFirst),
(" PREV", self.OnPrev),
("NEXT ", self.OnNext),
("Last", self.OnLast))
def createButtonBar(self, panel, yPos=0):
xPos = 0
for eachLabel, eachHandler in self.buttonData():
pos = (xPos, yPos)
button = self.buildOneButton(panel, eachLabel, eachHandler, pos)
xPos += button.GetSize().width
def buildOneButton(self, parent, label, handler, pos=(0,0)):
button = wx.Button(parent, -1, label, pos)
self.Bind(wx.EVT_BUTTON, handler, button)
return button
最后优化完成的代码,的确不同凡响examples:
import wx
class RefactorExample(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Refactor Example',size=(340, 200))
panel = wx.Panel(self, -1)
panel.SetBackgroundColour("White")
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.createMenuBar() #简化的init方法
self.createButtonBar(panel)
self.createTextFields(panel)
def menuData(self): #菜单数据
return (("&File",
("&Open", "Open in status bar", self.OnOpen),
("&Quit", "Quit", self.OnCloseWindow)),
("&Edit",
("&Copy", "Copy", self.OnCopy),
("&Cut", "Cut", self.OnCut),
("&Paste", "Paste", self.OnPaste),
("", "", ""), #横线
("&Options", "DisplayOptions", self.OnOptions)))
def createMenuBar(self): #创建菜单
menuBar = wx.MenuBar()
for eachMenuData in self.menuData():
menuLabel = eachMenuData[0]
menuItems = eachMenuData[1:]
menuBar.Append(self.createMenu(menuItems), menuLabel)
self.SetMenuBar(menuBar)
def createMenu(self, menuData):
menu = wx.Menu()
for eachLabel, eachStatus, eachHandler in menuData:
if not eachLabel:
menu.AppendSeparator() #间隔横线
continue
menuItem = menu.Append(-1, eachLabel, eachStatus)
self.Bind(wx.EVT_MENU, eachHandler, menuItem)
return menu
def buttonData(self): #按钮栏数据
return (("First", self.OnFirst),
("<<PREV", self.OnPrev),
("NEXT>>", self.OnNext),
("Last", self.OnLast))
def createButtonBar(self, panel, yPos = 0):#创建按钮
xPos = 0
for eachLabel, eachHandler in self.buttonData():
pos = (xPos, yPos)
button = self.buildOneButton(panel, eachLabel,eachHandler, pos)
xPos += button.GetSize().width
def buildOneButton(self, parent, label, handler, pos=(0,0)):
button = wx.Button(parent, -1, label, pos)
self.Bind(wx.EVT_BUTTON, handler, button)
return button
def textFieldData(self): #文本数据
return (("First Name", (10, 50)),
("Last Name", (10, 80)))
def createTextFields(self, panel):#创建文本
for eachLabel, eachPos in self.textFieldData():
self.createCaptionedText(panel, eachLabel, eachPos)
def createCaptionedText(self, panel, label, pos):
static = wx.StaticText(panel, wx.NewId(), label, pos)
static.SetBackgroundColour("White")
textPos = (pos[0] + 75, pos[1])
wx.TextCtrl(panel, wx.NewId(), "", size=(100, -1), pos=textPos)
# 空的事件处理器放在一起
def OnPrev(self, event): pass
def OnNext(self, event): pass
def OnLast(self, event): pass
def OnFirst(self, event): pass
def OnOpen(self, event): pass
def OnCopy(self, event): pass
def OnCut(self, event): pass
def OnPaste(self, event): pass
def OnOptions(self, event): pass
def OnCloseWindow(self, event):
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = RefactorExample(parent=None, id=-1)
frame.Show()
app.MainLoop()
一个wxPython模型:PyGridTableBase
填充网格(没有使用模型),examples:
import wx
import wx.grid
class SimpleGrid(wx.grid.Grid):
def __init__(self, parent):
wx.grid.Grid.__init__(self, parent, -1)
self.CreateGrid(9, 2)
self.SetColLabelValue(0, "First")
self.SetColLabelValue(1, "Last")
self.SetRowLabelValue(0, "CF")
self.SetCellValue(0, 0, "Bob")
self.SetCellValue(0, 1, "Dernier")
self.SetRowLabelValue(1, "2B")
self.SetCellValue(1, 0, "Ryne")
self.SetCellValue(1, 1, "Sandberg")
self.SetRowLabelValue(2, "LF")
self.SetCellValue(2, 0, "Gary")
self.SetCellValue(2, 1, "Matthews")
self.SetRowLabelValue(3, "1B")
self.SetCellValue(3, 0, "Leon")
self.SetCellValue(3, 1, "Durham")
self.SetRowLabelValue(4, "RF")
self.SetCellValue(4, 0, "Keith")
self.SetCellValue(4, 1, "Moreland")
self.SetRowLabelValue(5, "3B")
self.SetCellValue(5, 0, "Ron")
self.SetCellValue(5, 1, "Cey")
self.SetRowLabelValue(6, "C")
self.SetCellValue(6, 0, "Jody")
self.SetCellValue(6, 1, "Davis")
self.SetRowLabelValue(7, "SS")
self.SetCellValue(7, 0, "Larry")
self.SetCellValue(7, 1, "Bowa")
self.SetRowLabelValue(8, "P")
self.SetCellValue(8, 0, "Rick")
self.SetCellValue(8, 1, "Sutcliffe")
class TestFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "A Grid",size=(275, 275))
grid = SimpleGrid(self)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = TestFrame(None)
frame.Show(True)
app.MainLoop()
PyGridTableBase的方法
wx.grid.PyGridTableBase的必须的方法
1.GetNumberRows():返回一个表明grid中行数的整数
2.GetNumberCols():返回一个表明grid中列数的整数
3.IsEmptyCell(row, col):如果索引(row,col)所表示的单元是空的话,返回True
4.GetValue(row, col):返回显示在单元(row,col)中的值
5.SetValue(row, col,value):设置单元(row,col)中的值。如果你想要只读模式,你仍必须包含这个方法,但是你可以在该函数中使用pass
生成自PyGridTableBase模型的一个表,examples:
import wx
import wx.grid
class LineupTable(wx.grid.PyGridTableBase):
data = (("CF", "Bob", "Dernier"), ("2B", "Ryne", "Sandberg"),
("LF", "Gary", "Matthews"), ("1B", "Leon", "Durham"),
("RF", "Keith", "Moreland"), ("3B", "Ron", "Cey"),
("C", "Jody", "Davis"), ("SS", "Larry", "Bowa"),
("P", "Rick", "Sutcliffe"))
colLabels = ("Last", "First")
def __init__(self):
wx.grid.PyGridTableBase.__init__(self)
def GetNumberRows(self):
return len(self.data)
def GetNumberCols(self):
return len(self.data[0]) - 1
def GetColLabelValue(self, col):
return self.colLabels[col]
def GetRowLabelValue(self, row):
return self.data[row][0]
def IsEmptyCell(self, row, col):
return False
def GetValue(self, row, col):
return self.data[row][col + 1]
def SetValue(self, row, col, value):
pass
class SimpleGrid(wx.grid.Grid):
def __init__(self, parent):
wx.grid.Grid.__init__(self, parent, -1)
self.SetTable(LineupTable()) #设置表
class TestFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "A Grid",size=(275, 275))
grid = SimpleGrid(self)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = TestFrame(None)
frame.Show(True)
app.MainLoop()
二维表通用表
建立一个通用表文件generictable.py,同时在右键项目-----》Properties----->External Libraries里面,Add source folder,把当前py文件的路径加上,generictable.py文件内容如下:
import wx
import wx.grid
class GenericTable(wx.grid.PyGridTableBase):
def __init__(self, data, rowLabels=None, colLabels=None):
wx.grid.PyGridTableBase.__init__(self)
self.data = data
self.rowLabels = rowLabels
self.colLabels = colLabels
def GetNumberRows(self):
return len(self.data)
def GetNumberCols(self):
return len(self.data[0])
def GetColLabelValue(self, col):
if self.colLabels:
return self.colLabels[col]
def GetRowLabelValue(self, row):
if self.rowLabels:
return self.rowLabels[row]
def IsEmptyCell(self, row, col):
return False
def GetValue(self, row, col):
return self.data[row][col]
def SetValue(self, row, col, value):
pass
显示表单的例子如下,examples:
import wx
import wx.grid
import generictable
data = (("Bob", "Dernier"), ("Ryne", "Sandberg"),
("Gary", "Matthews"), ("Leon", "Durham"),
("Keith", "Moreland"), ("Ron", "Cey"),
("Jody", "Davis"), ("Larry", "Bowa"),
("Rick", "Sutcliffe"))
colLabels = ("Last", "First")
rowLabels = ("CF", "2B", "LF", "1B", "RF", "3B", "C", "SS", "P")
class SimpleGrid(wx.grid.Grid):
def __init__(self, parent):
wx.grid.Grid.__init__(self, parent, -1)
tableBase = generictable.GenericTable(data, rowLabels,
colLabels)
self.SetTable(tableBase)
class TestFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "A Grid",size=(275, 275))
grid = SimpleGrid(self)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = TestFrame(None)
frame.Show(True)
app.MainLoop()
自定义模型
抽象的对象abstractmodel.py:
class AbstractModel(object):
def __init__(self):
self.listeners = []
def addListener(self, listenerFunc):
self.listeners.append(listenerFunc)
def removeListener(self, listenerFunc):
self.listeners.remove(listenerFunc)
def update(self):
for eachFunc in self.listeners:
eachFunc(self)
使用自定义对象的类examples:
import wx
import abstractmodel
class SimpleName(abstractmodel.AbstractModel):
def __init__(self, first="", last=""):
abstractmodel.AbstractModel.__init__(self)
self.set(first, last)
def set(self, first, last):
self.first = first
self.last = last
self.update() #1 更新
class ModelExample(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Flintstones',size=(340, 200))
panel = wx.Panel(self)
panel.SetBackgroundColour("White")
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.textFields = {}
self.createTextFields(panel)
#2 创建模型
self.model = SimpleName()
self.model.addListener(self.OnUpdate)
self.createButtonBar(panel)
def buttonData(self):
return (("Fredify", self.OnFred),
("Wilmafy", self.OnWilma),
("Barnify", self.OnBarney),
("Bettify", self.OnBetty))
def createButtonBar(self, panel, yPos = 0):
xPos = 0
for eachLabel, eachHandler in self.buttonData():
pos = (xPos, yPos)
button = self.buildOneButton(panel, eachLabel, eachHandler, pos)
xPos += button.GetSize().width
def buildOneButton(self, parent, label, handler, pos=(0,0)):
button = wx.Button(parent, -1, label, pos)
self.Bind(wx.EVT_BUTTON, handler, button)
return button
def textFieldData(self):
return (("First Name", (10, 50)),("Last Name", (10, 80)))
def createTextFields(self, panel):
for eachLabel, eachPos in self.textFieldData():
self.createCaptionedText(panel, eachLabel, eachPos)
def createCaptionedText(self, panel, label, pos):
static = wx.StaticText(panel, wx.NewId(), label, pos)
static.SetBackgroundColour("White")
textPos = (pos[0] + 75, pos[1])
self.textFields[label] = wx.TextCtrl(panel, wx.NewId(),
"", size=(100, -1), pos=textPos,style=wx.TE_READONLY)
def OnUpdate(self, model): #3 设置文本域
self.textFields["First Name"].SetValue(model.first)
self.textFields["Last Name"].SetValue(model.last)
#-------------------------------------------
#4 响应按钮敲击的处理器
def OnFred(self, event):
self.model.set("Fred", "Flintstone")
def OnBarney(self, event):
self.model.set("Barney", "Rubble")
def OnWilma(self, event):
self.model.set("Wilma", "Flintstone")
def OnBetty(self, event):
self.model.set("Betty", "Rubble")
#---------------------------------------------
def OnCloseWindow(self, event):
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = ModelExample(parent=None, id=-1)
frame.Show()
app.MainLoop()
对GUI程序进行单元测试
暂时先略过
参考书籍
http://wiki.woodpecker.org.cn/moin/WxPythonInAction
第四章 用PyCrust使得wxPython更易处理
如何与wxPython程序交互
例4.1 简单的Python交互式会话
C:\Documents and Settings\sillycat>python
ActivePython 2.6.4.10 (ActiveState Software Inc.) based on
Python 2.6.4 (r264:75706, Jan 22 2010, 16:41:54) [MSC v.1500 32 bit (Intel)] on
win32Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 2
4
>>> 7 * 6
42
PyCrust配置了标准的Python shell
图形化的python工具,如IDLE看起来很像命令行Python shell,但是它有额外的特性如调用提示。PyCrust的目的之一就是提供所有现存的Python shell的特性。
如何将PyCrust应用于wxPython应用程序
在我的测试目录下,新建py文件WxPythonWindow.py,建立一个空的窗口:
import wx
class Frame(wx.Frame):
pass
class App(wx.App):
def OnInit(self):
self.frame = Frame(parent=None, id=-1, title='Spare')
self.frame.Show()
self.SetTopWindow(self.frame)
return True
if __name__ == '__main__':
app = App()
app.MainLoop()
在相同目录下,建立PyWrap.py文件:
__author__ = "Patrick K. O'Brien pobrien@orbtech.com "
__cvsid__ = "$Id: PyCrust.txt,v 1.15 2005/03/29 23:39:27 robind Exp $"
__revision__ = "$Revision: 1.15 $"[11:-2]
import os
import sys
import wx
from wx.py.crust import CrustFrame
def wrap(app):
wx.InitAllImageHandlers()
frame = CrustFrame()
frame.SetSize((750, 525))
frame.Show(True)
frame.shell.interp.locals['app'] = app
app.MainLoop()
def main(modulename=None):
sys.path.insert(0, os.curdir)
if not modulename:
if len(sys.argv) < 2:
print "Please specify a module name."
raise SystemExit
modulename = sys.argv[1]
if modulename.endswith('.py'):
modulename = modulename[:-3]
module = __import__(modulename)
# Find the App class.
App = None
d = module.__dict__
for item in d.keys():
try:
if issubclass(d[item], wx.App):
App = d[item]
except (NameError, TypeError):
pass
if App is None:
print "No App class was found."
raise SystemExit
app = App()
wrap(app)
if __name__ == '__main__':
main()
执行命令:
D:\work\easyfinance\finance\com\sillycat\finance>python PyWrap.py WxPythonWindow.py
打开了,本来的Spare窗口,和PyCrust窗口,然后在PyCrust窗口输入如下命令,改变spare的属性和状态,在PyCrust的命令行下执行的过程如下:
import wx
app.frame.panel = wx.Panel(parent=app.frame)
app.frame.panel.SetBackgroundColour('White')
True
app.frame.panel.Refresh()
#改变了背景为白色
app.frame.StatusBar = app.frame.CreateStatusBar(number=3)
app.frame.StatusBar.SetStatusText("left",0)
app.frame.StatusBar.SetStatusText("center",1)
app.frame.StatusBar.SetStatusText("right",2)
#增加了状态栏
app.frame.MenuBar = wx.MenuBar()
menu = wx.Menu()
app.frame.MenuBar.Append(menu,"Primary")
True
app.frame.SetMenuBar(app.frame.MenuBar)
menu.Append(wx.NewId(),"One","first menu item")
<wx._core.MenuItem; proxy of <Swig Object of type 'wxMenuItem *' at 0x1ca1b28> >
menu.Append(wx.NewId(),"Two","second menu item")
<wx._core.MenuItem; proxy of <Swig Object of type 'wxMenuItem *' at 0x1d08de8> >
#增加了菜单
在平时我们编写程序时,我们可以利用到PyCrust里面的模块,比如shell,filling等,但是我目前用不到。
第五章 绘制蓝图
重构帮助我们改进代码
examples:
import wx
class RefactorExample(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Refactor Example',size=(340, 200))
panel = wx.Panel(self, -1)
panel.SetBackgroundColour("White")
prevButton = wx.Button(panel, -1, "PREV", pos=(80, 0))
self.Bind(wx.EVT_BUTTON, self.OnPrev, prevButton)
nextButton = wx.Button(panel, -1, "NEXT", pos=(160, 0))
self.Bind(wx.EVT_BUTTON, self.OnNext, nextButton)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
menuBar = wx.MenuBar()
menu1 = wx.Menu()
openMenuItem = menu1.Append(-1, "open", "Open")
self.Bind(wx.EVT_MENU, self.OnOpen, openMenuItem)
quitMenuItem = menu1.Append(-1, "quit", "Quit")
self.Bind(wx.EVT_MENU, self.OnCloseWindow, quitMenuItem)
menuBar.Append(menu1, "menu1")
menu2 = wx.Menu()
copyItem = menu2.Append(-1, "copy", "Copy")
self.Bind(wx.EVT_MENU, self.OnCopy, copyItem)
cutItem = menu2.Append(-1, "cut", "Cut")
self.Bind(wx.EVT_MENU, self.OnCut, cutItem)
pasteItem = menu2.Append(-1, "Paste", "Paste")
self.Bind(wx.EVT_MENU, self.OnPaste, pasteItem)
menuBar.Append(menu2, "menu2")
self.SetMenuBar(menuBar)
static = wx.StaticText(panel,wx.NewId(),"First Name",pos=(10, 50))
static.SetBackgroundColour("White")
text = wx.TextCtrl(panel, wx.NewId(),"",size=(100, -1),pos=(80, 50))
static2 = wx.StaticText(panel, wx.NewId(), "Last Name",pos=(10, 80))
static2.SetBackgroundColour("White")
text2 = wx.TextCtrl(panel, wx.NewId(), "", size=(100, -1),pos=(80, 80))
firstButton = wx.Button(panel, -1, "FIRST")
self.Bind(wx.EVT_BUTTON,self.OnFirst, firstButton)
menu2.AppendSeparator()
optItem = menu2.Append(-1, "display", "Display Options")
self.Bind(wx.EVT_MENU, self.OnOptions, optItem)
lastButton = wx.Button(panel, -1, "LAST", pos=(240, 0))
self.Bind(wx.EVT_BUTTON, self.OnLast, lastButton)
# Just grouping the empty event handlers together
def OnPrev(self, event): pass
def OnNext(self, event): pass
def OnLast(self, event): pass
def OnFirst(self, event): pass
def OnOpen(self, event): pass
def OnCopy(self, event): pass
def OnCut(self, event): pass
def OnPaste(self, event): pass
def OnOptions(self, event): pass
def OnCloseWindow(self, event):
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = RefactorExample(parent=None, id=-1)
frame.Show()
app.MainLoop()
重构注意
example 按钮栏作为一个单独的方法:
def createButtonBar(self):
firstButton = wx.Button(panel, -1, "FIRST")
self.Bind(wx.EVT_BUTTON, self.OnFirst, firstButton)
prevButton = wx.Button(panel, -1, "PREV", pos=(80, 0))
self.Bind(wx.EVT_BUTTON, , self.OnPrev, prevButton)
nextButton = wx.Button(panel, -1, "NEXT", pos=(160, 0))
self.Bind(wx.EVT_BUTTON, self.OnNext, nextButton)
lastButton = wx.Button(panel, -1, "LAST", pos=(240, 0))
self.Bind(wx.EVT_BUTTON, self.OnLast, lastButton)
提炼公用方法,examples:
def createButtonBar(self, panel):
self.buildOneButton(panel, "First", self.OnFirst)
self.buildOneButton(panel, "PREV", self.OnPrev, (80, 0))
self.buildOneButton(panel, "NEXT", self.OnNext, (160, 0))
self.buildOneButton(panel, "Last", self.OnLast, (240, 0))
def buildOneButton(self, parent, label, handler, pos=(0,0)):
button = wx.Button(parent, -1, label, pos)
self.Bind(wx.EVT_BUTTON, handler, button)
return button
让方法和数据分离,example:
def buttonData(self):
return (("First", self.OnFirst),
(" PREV", self.OnPrev),
("NEXT ", self.OnNext),
("Last", self.OnLast))
def createButtonBar(self, panel, yPos=0):
xPos = 0
for eachLabel, eachHandler in self.buttonData():
pos = (xPos, yPos)
button = self.buildOneButton(panel, eachLabel, eachHandler, pos)
xPos += button.GetSize().width
def buildOneButton(self, parent, label, handler, pos=(0,0)):
button = wx.Button(parent, -1, label, pos)
self.Bind(wx.EVT_BUTTON, handler, button)
return button
最后优化完成的代码,的确不同凡响examples:
import wx
class RefactorExample(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Refactor Example',size=(340, 200))
panel = wx.Panel(self, -1)
panel.SetBackgroundColour("White")
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.createMenuBar() #简化的init方法
self.createButtonBar(panel)
self.createTextFields(panel)
def menuData(self): #菜单数据
return (("&File",
("&Open", "Open in status bar", self.OnOpen),
("&Quit", "Quit", self.OnCloseWindow)),
("&Edit",
("&Copy", "Copy", self.OnCopy),
("&Cut", "Cut", self.OnCut),
("&Paste", "Paste", self.OnPaste),
("", "", ""), #横线
("&Options", "DisplayOptions", self.OnOptions)))
def createMenuBar(self): #创建菜单
menuBar = wx.MenuBar()
for eachMenuData in self.menuData():
menuLabel = eachMenuData[0]
menuItems = eachMenuData[1:]
menuBar.Append(self.createMenu(menuItems), menuLabel)
self.SetMenuBar(menuBar)
def createMenu(self, menuData):
menu = wx.Menu()
for eachLabel, eachStatus, eachHandler in menuData:
if not eachLabel:
menu.AppendSeparator() #间隔横线
continue
menuItem = menu.Append(-1, eachLabel, eachStatus)
self.Bind(wx.EVT_MENU, eachHandler, menuItem)
return menu
def buttonData(self): #按钮栏数据
return (("First", self.OnFirst),
("<<PREV", self.OnPrev),
("NEXT>>", self.OnNext),
("Last", self.OnLast))
def createButtonBar(self, panel, yPos = 0):#创建按钮
xPos = 0
for eachLabel, eachHandler in self.buttonData():
pos = (xPos, yPos)
button = self.buildOneButton(panel, eachLabel,eachHandler, pos)
xPos += button.GetSize().width
def buildOneButton(self, parent, label, handler, pos=(0,0)):
button = wx.Button(parent, -1, label, pos)
self.Bind(wx.EVT_BUTTON, handler, button)
return button
def textFieldData(self): #文本数据
return (("First Name", (10, 50)),
("Last Name", (10, 80)))
def createTextFields(self, panel):#创建文本
for eachLabel, eachPos in self.textFieldData():
self.createCaptionedText(panel, eachLabel, eachPos)
def createCaptionedText(self, panel, label, pos):
static = wx.StaticText(panel, wx.NewId(), label, pos)
static.SetBackgroundColour("White")
textPos = (pos[0] + 75, pos[1])
wx.TextCtrl(panel, wx.NewId(), "", size=(100, -1), pos=textPos)
# 空的事件处理器放在一起
def OnPrev(self, event): pass
def OnNext(self, event): pass
def OnLast(self, event): pass
def OnFirst(self, event): pass
def OnOpen(self, event): pass
def OnCopy(self, event): pass
def OnCut(self, event): pass
def OnPaste(self, event): pass
def OnOptions(self, event): pass
def OnCloseWindow(self, event):
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = RefactorExample(parent=None, id=-1)
frame.Show()
app.MainLoop()
一个wxPython模型:PyGridTableBase
填充网格(没有使用模型),examples:
import wx
import wx.grid
class SimpleGrid(wx.grid.Grid):
def __init__(self, parent):
wx.grid.Grid.__init__(self, parent, -1)
self.CreateGrid(9, 2)
self.SetColLabelValue(0, "First")
self.SetColLabelValue(1, "Last")
self.SetRowLabelValue(0, "CF")
self.SetCellValue(0, 0, "Bob")
self.SetCellValue(0, 1, "Dernier")
self.SetRowLabelValue(1, "2B")
self.SetCellValue(1, 0, "Ryne")
self.SetCellValue(1, 1, "Sandberg")
self.SetRowLabelValue(2, "LF")
self.SetCellValue(2, 0, "Gary")
self.SetCellValue(2, 1, "Matthews")
self.SetRowLabelValue(3, "1B")
self.SetCellValue(3, 0, "Leon")
self.SetCellValue(3, 1, "Durham")
self.SetRowLabelValue(4, "RF")
self.SetCellValue(4, 0, "Keith")
self.SetCellValue(4, 1, "Moreland")
self.SetRowLabelValue(5, "3B")
self.SetCellValue(5, 0, "Ron")
self.SetCellValue(5, 1, "Cey")
self.SetRowLabelValue(6, "C")
self.SetCellValue(6, 0, "Jody")
self.SetCellValue(6, 1, "Davis")
self.SetRowLabelValue(7, "SS")
self.SetCellValue(7, 0, "Larry")
self.SetCellValue(7, 1, "Bowa")
self.SetRowLabelValue(8, "P")
self.SetCellValue(8, 0, "Rick")
self.SetCellValue(8, 1, "Sutcliffe")
class TestFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "A Grid",size=(275, 275))
grid = SimpleGrid(self)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = TestFrame(None)
frame.Show(True)
app.MainLoop()
PyGridTableBase的方法
wx.grid.PyGridTableBase的必须的方法
1.GetNumberRows():返回一个表明grid中行数的整数
2.GetNumberCols():返回一个表明grid中列数的整数
3.IsEmptyCell(row, col):如果索引(row,col)所表示的单元是空的话,返回True
4.GetValue(row, col):返回显示在单元(row,col)中的值
5.SetValue(row, col,value):设置单元(row,col)中的值。如果你想要只读模式,你仍必须包含这个方法,但是你可以在该函数中使用pass
生成自PyGridTableBase模型的一个表,examples:
import wx
import wx.grid
class LineupTable(wx.grid.PyGridTableBase):
data = (("CF", "Bob", "Dernier"), ("2B", "Ryne", "Sandberg"),
("LF", "Gary", "Matthews"), ("1B", "Leon", "Durham"),
("RF", "Keith", "Moreland"), ("3B", "Ron", "Cey"),
("C", "Jody", "Davis"), ("SS", "Larry", "Bowa"),
("P", "Rick", "Sutcliffe"))
colLabels = ("Last", "First")
def __init__(self):
wx.grid.PyGridTableBase.__init__(self)
def GetNumberRows(self):
return len(self.data)
def GetNumberCols(self):
return len(self.data[0]) - 1
def GetColLabelValue(self, col):
return self.colLabels[col]
def GetRowLabelValue(self, row):
return self.data[row][0]
def IsEmptyCell(self, row, col):
return False
def GetValue(self, row, col):
return self.data[row][col + 1]
def SetValue(self, row, col, value):
pass
class SimpleGrid(wx.grid.Grid):
def __init__(self, parent):
wx.grid.Grid.__init__(self, parent, -1)
self.SetTable(LineupTable()) #设置表
class TestFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "A Grid",size=(275, 275))
grid = SimpleGrid(self)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = TestFrame(None)
frame.Show(True)
app.MainLoop()
二维表通用表
建立一个通用表文件generictable.py,同时在右键项目-----》Properties----->External Libraries里面,Add source folder,把当前py文件的路径加上,generictable.py文件内容如下:
import wx
import wx.grid
class GenericTable(wx.grid.PyGridTableBase):
def __init__(self, data, rowLabels=None, colLabels=None):
wx.grid.PyGridTableBase.__init__(self)
self.data = data
self.rowLabels = rowLabels
self.colLabels = colLabels
def GetNumberRows(self):
return len(self.data)
def GetNumberCols(self):
return len(self.data[0])
def GetColLabelValue(self, col):
if self.colLabels:
return self.colLabels[col]
def GetRowLabelValue(self, row):
if self.rowLabels:
return self.rowLabels[row]
def IsEmptyCell(self, row, col):
return False
def GetValue(self, row, col):
return self.data[row][col]
def SetValue(self, row, col, value):
pass
显示表单的例子如下,examples:
import wx
import wx.grid
import generictable
data = (("Bob", "Dernier"), ("Ryne", "Sandberg"),
("Gary", "Matthews"), ("Leon", "Durham"),
("Keith", "Moreland"), ("Ron", "Cey"),
("Jody", "Davis"), ("Larry", "Bowa"),
("Rick", "Sutcliffe"))
colLabels = ("Last", "First")
rowLabels = ("CF", "2B", "LF", "1B", "RF", "3B", "C", "SS", "P")
class SimpleGrid(wx.grid.Grid):
def __init__(self, parent):
wx.grid.Grid.__init__(self, parent, -1)
tableBase = generictable.GenericTable(data, rowLabels,
colLabels)
self.SetTable(tableBase)
class TestFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "A Grid",size=(275, 275))
grid = SimpleGrid(self)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = TestFrame(None)
frame.Show(True)
app.MainLoop()
自定义模型
抽象的对象abstractmodel.py:
class AbstractModel(object):
def __init__(self):
self.listeners = []
def addListener(self, listenerFunc):
self.listeners.append(listenerFunc)
def removeListener(self, listenerFunc):
self.listeners.remove(listenerFunc)
def update(self):
for eachFunc in self.listeners:
eachFunc(self)
使用自定义对象的类examples:
import wx
import abstractmodel
class SimpleName(abstractmodel.AbstractModel):
def __init__(self, first="", last=""):
abstractmodel.AbstractModel.__init__(self)
self.set(first, last)
def set(self, first, last):
self.first = first
self.last = last
self.update() #1 更新
class ModelExample(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Flintstones',size=(340, 200))
panel = wx.Panel(self)
panel.SetBackgroundColour("White")
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.textFields = {}
self.createTextFields(panel)
#2 创建模型
self.model = SimpleName()
self.model.addListener(self.OnUpdate)
self.createButtonBar(panel)
def buttonData(self):
return (("Fredify", self.OnFred),
("Wilmafy", self.OnWilma),
("Barnify", self.OnBarney),
("Bettify", self.OnBetty))
def createButtonBar(self, panel, yPos = 0):
xPos = 0
for eachLabel, eachHandler in self.buttonData():
pos = (xPos, yPos)
button = self.buildOneButton(panel, eachLabel, eachHandler, pos)
xPos += button.GetSize().width
def buildOneButton(self, parent, label, handler, pos=(0,0)):
button = wx.Button(parent, -1, label, pos)
self.Bind(wx.EVT_BUTTON, handler, button)
return button
def textFieldData(self):
return (("First Name", (10, 50)),("Last Name", (10, 80)))
def createTextFields(self, panel):
for eachLabel, eachPos in self.textFieldData():
self.createCaptionedText(panel, eachLabel, eachPos)
def createCaptionedText(self, panel, label, pos):
static = wx.StaticText(panel, wx.NewId(), label, pos)
static.SetBackgroundColour("White")
textPos = (pos[0] + 75, pos[1])
self.textFields[label] = wx.TextCtrl(panel, wx.NewId(),
"", size=(100, -1), pos=textPos,style=wx.TE_READONLY)
def OnUpdate(self, model): #3 设置文本域
self.textFields["First Name"].SetValue(model.first)
self.textFields["Last Name"].SetValue(model.last)
#-------------------------------------------
#4 响应按钮敲击的处理器
def OnFred(self, event):
self.model.set("Fred", "Flintstone")
def OnBarney(self, event):
self.model.set("Barney", "Rubble")
def OnWilma(self, event):
self.model.set("Wilma", "Flintstone")
def OnBetty(self, event):
self.model.set("Betty", "Rubble")
#---------------------------------------------
def OnCloseWindow(self, event):
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = ModelExample(parent=None, id=-1)
frame.Show()
app.MainLoop()
对GUI程序进行单元测试
暂时先略过
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 433NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 295Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 378Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 337Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 419NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 364Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 294Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 211GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 393GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 278GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 266Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 266Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 254Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 240Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 238NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 228Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 519NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 232Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 323Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 323Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
Part1 wxPython入门 , wxPython入门 , wxPython入门里面有实例讲解
wxPython入门教程前3章 wxPython入门教程前3章 wxPython入门教程前3章 wxPython入门教程前3章
wxpython教程:wxPython 入门教程 疯狂代码 http://CrazyCoder.cn/ ĵ:http:/CrazyCoder.cn/Python/Article69303.html 这篇文章是有关 wxPython但 wxPython 实际是两件事物组合体:Python 脚本语言和 GUI 功能 ...
几本很不错的关于python gui的wxPython的书,包括“wxPython in Action(中文版)” 活学活用wxPython “《wxPython in Action》Noel Rappin, Robin Dunn著 2006年版”
python窗⼝应⽤:wxpython超快速⼊门教程 ⽂章⽬录 创建窗⼝程序 初步 截⾄⽬前为⽌,我们写的所有应⽤皆在终端运⾏。但⼀般来说,我们在接触编程之前所使⽤的软件⼤多具备友好的交互界⾯。此外,我们虽 然已经写出...
这篇文章是关于 wxPython,但 wxPython 实际是两件事物的组合体:Python 脚本语言和 GUI 功能的 wxWindows 库(关于 wxWindows 的介绍,请参阅 developerWorks上的 “细述 wxWindows” )。wxWindows 库是为了...
wxPython Application Development Cookbook wxPython 开发入门
wxpython中文教程wxpython中文教程wxpython中文教程wxpython中文教程wxpython中文教程wxpython中文教程wxpython中文教程wxpython中文教程wxpython中文教程
wxpython python开发图形界面
Boa是基于wxpython的可视化IDE,可以实现界面的可视化拖拽,视频教程是老外讲的,讲的很清楚很直观,但是没有中文字幕,供初学者入门,靠这个两天搞定了不少事情,由于大小限制,分成了四部分上传,视频都不大,花2个...
wxPython_API.chm Python+中文手册.pdf Python入门指南 2.5b.pdf python教程.chm wxPIA_book wxPython_Tutorial wxpython-in-action-zh.pdf
Boa是基于wxpython的可视化IDE,可以实现界面的可视化拖拽,视频教程是老外讲的,讲的很清楚很直观,但是没有中文字幕,供初学者入门,靠这个两天搞定了不少事情,由于大小限制,分成了四部分上传,视频都不大,花2个...
wxPython中文教程 简单入门加实例 教程里说得很详细,对于刚开始学习WXPython的人很有帮助
wxPython开发参考书籍
超全 wxpython tutorial 教程,有助于新手入门 wxpython。
python2.4相对应的wxPython,还有很多库,一次性下载,不用费心去找了。
几个关于wxpython的入门实例,可以参考学习,代码可以直接在python shell里运行,去掉每个实例的注释就可运行
wxpython grid使用部分
wxPython是Python编程语言的一个GUI工具箱。他使得Python程序员...现今支持的平台有:32位微软Windows操作系统、大多数Unix或类Unix系统、苹果Mac OS X。 由于使用Python作为编程语言,wxPython编写简单、易于理解。
CustomTreeCtrl是一个wxpython的树控件类,在使用的过程中对于拖放功能一直不是很理解,最近看了以上文章和参考了一些其他例子的代码,对于wxPython实现该功能做一些说明。