1 app = wx.App()
每个wxpyhon的程式都必须有一个application object
2 wxFrame
(1)wx.Frame widget 是一个最重要的容器 是其他widget的parent widget
(2)Frame有7个参数
wx.Frame(wx.Window parent, int id=-1, string title='', wx.Point pos = wx.DefaultPosition,
wx.Size size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, string name = "frame")
(3)第一个参数没有默认值 前三个是必须的 其他的可选
(4)style这个参数可以是多个的集合写法如下
window = wx.Frame(None, style=wx.MAXIMIZE_BOX | wx.RESIZE_BORDER
| wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
默认的是wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN.
(5)可以使用wxWindow的方法Move等
Method Description
Move(wx.Point point) move a window to the given position
MoveXY(int x, int y) move a window to the given position
SetPosition(wx.Point point) set the position of a window
SetDimensions(wx.Point point, wx.Size size) set the position and the size of a window
(6)可以使用wxWindow和wxTopLevelWindow的一些方法来修改Frame的默认位置大小等
比如让窗口最大化 在中间显示 调用Centere()和Maximize()方法即可
3 一个带有事件处理的简单例子
每个wxpyhon的程式都必须有一个application object
2 wxFrame
(1)wx.Frame widget 是一个最重要的容器 是其他widget的parent widget
(2)Frame有7个参数
wx.Frame(wx.Window parent, int id=-1, string title='', wx.Point pos = wx.DefaultPosition,
wx.Size size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, string name = "frame")
(3)第一个参数没有默认值 前三个是必须的 其他的可选
(4)style这个参数可以是多个的集合写法如下
window = wx.Frame(None, style=wx.MAXIMIZE_BOX | wx.RESIZE_BORDER
| wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
默认的是wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN.
(5)可以使用wxWindow的方法Move等
Method Description
Move(wx.Point point) move a window to the given position
MoveXY(int x, int y) move a window to the given position
SetPosition(wx.Point point) set the position of a window
SetDimensions(wx.Point point, wx.Size size) set the position and the size of a window
(6)可以使用wxWindow和wxTopLevelWindow的一些方法来修改Frame的默认位置大小等
比如让窗口最大化 在中间显示 调用Centere()和Maximize()方法即可
python 代码
- import wx
- class Centre(wx.Frame):
- def __init__(self, parent, id, title):
- wx.Frame.__init__(self, parent, id, title)
- self.Centre()
- self.Maximize()
- self.Show(True)
- app = wx.App()
- Centre(None, -1, 'Centre')
- app.MainLoop()
3 一个带有事件处理的简单例子

python 代码
- #!/usr/bin/python
- # communicate.py
- import wx
- #左边的面板 继承自wxPanel
- class LeftPanel(wx.Panel):
- def __init__(self, parent, id):
- #初始化面板
- wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
- #左边的文本区等于右边的文本区 所以构造的时候要先构造右边的Panel
- #这个地方是找到一个static text widget
- #直接指到RightPanel中的wx.StaticText(self, -1, '0', (40, 60))
- self.text = parent.GetParent().rightPanel.text
- #添加两个按钮 一个加号(加法操作)一个减号(减法操作)
- button1 = wx.Button(self, -1, '+', (10, 10))
- button2 = wx.Button(self, -1, '-', (10, 60))
- #和自定义的事件处理绑定
- self.Bind(wx.EVT_BUTTON, self.OnPlus, id=button1.GetId())
- self.Bind(wx.EVT_BUTTON, self.OnMinus, id=button2.GetId())
- #加法函数
- def OnPlus(self, event):
- value = int(self.text.GetLabel())
- value = value + 1
- self.text.SetLabel(str(value))
- #减法函数
- def OnMinus(self, event):
- value = int(self.text.GetLabel())
- value = value - 1
- self.text.SetLabel(str(value))
- #右边的面板 继承自wxPanel
- class RightPanel(wx.Panel):
- def __init__(self, parent, id):
- wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
- #因为LeftPanel也指到这个 所以RightPanel要先初始化
- self.text = wx.StaticText(self, -1, '0', (40, 60))
- #构造一个Frame
- class Communicate(wx.Frame):
- def __init__(self, parent, id, title):
- #Frame 初始化
- wx.Frame.__init__(self, parent, id, title, size=(280, 200))
- #为了使得上面LeftPanel的self.text = parent.GetParent().rightPanel.text准确的找到
- #让两个panel继承自同一个父panel
- panel = wx.Panel(self, -1)
- self.rightPanel = RightPanel(panel, -1)
- leftPanel = LeftPanel(panel, -1)
- #定义panel的大小和位置
- hbox = wx.BoxSizer()
- hbox.Add(leftPanel, 1, wx.EXPAND | wx.ALL, 5)
- hbox.Add(self.rightPanel, 1, wx.EXPAND | wx.ALL, 5)
- panel.SetSizer(hbox)
- self.Centre()
- self.Show(True)
- #定义wxApp并画出这个窗口
- app = wx.App()
- Communicate(None, -1, 'widgets communicate')
- app.MainLoop()