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()方法即可
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 代码
-
-
-
-
- import wx
-
-
- class LeftPanel(wx.Panel):
- def __init__(self, parent, id):
-
- wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
-
-
-
- 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))
-
-
- class RightPanel(wx.Panel):
- def __init__(self, parent, id):
- wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
-
- self.text = wx.StaticText(self, -1, '0', (40, 60))
-
-
- class Communicate(wx.Frame):
- def __init__(self, parent, id, title):
-
- wx.Frame.__init__(self, parent, id, title, size=(280, 200))
-
-
- panel = wx.Panel(self, -1)
- self.rightPanel = RightPanel(panel, -1)
-
- leftPanel = LeftPanel(panel, -1)
-
- 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)
-
- app = wx.App()
- Communicate(None, -1, 'widgets communicate')
- app.MainLoop()