- 論壇徽章:
- 3
|
回復(fù) 4# anselmiao
沒事,大家都是一步一步走過來的。
首先要在編寫界面的代碼中添加一些事件函數(shù),然后在這個事件函數(shù)中進行相應(yīng)的處理。最后用Bind將事件和事件處理函數(shù)進行綁定。一個示例如下:- #coding=utf-8
- import wx
- class Calculator(wx.Frame):
- def __init__(self):
- wx.Frame.__init__(self, None, -1, 'Calculator', size=(280, 100))
- panel = wx.Panel(self, -1)
- #添加文本框
- speedLabel = wx.StaticText(panel, -1, u"速度")
- self.speedText = wx.TextCtrl(panel, -1, "", size=(150, -1))
- timeLabel = wx.StaticText(panel, -1, u"時間")
- self.timeText = wx.TextCtrl(panel, -1, "", size=(150, -1))
- #添加按鈕
- process = wx.Button(panel, -1, u"計算")
- #顯示結(jié)果
- self.resultLabel = wx.TextCtrl(panel, -1, "", size=(150, -1))
- #使用sizer布局
- sizer=wx.FlexGridSizer(cols=2,hgap=6,vgap=6)
- sizer.AddMany([speedLabel,self.speedText,timeLabel,
- self.timeText,process,self.resultLabel])
- panel.SetSizer(sizer)
- #綁定事件
- self.Bind(wx.EVT_BUTTON, self.OnClick, process)
-
- def OnClick(self, event):
- result = int(self.speedText.GetValue()) * int(self.timeText.GetValue())
- self.resultLabel.SetValue(str(result))
- if __name__ == '__main__':
- app = wx.PySimpleApp()
- frame = Calculator()
- frame.Center()
- frame.Show()
- app.MainLoop()
復(fù)制代碼 這里,OnClick函數(shù)是自己起的名字,關(guān)鍵是Bind那里,將wx中預(yù)定義的事件類型、對應(yīng)的事件函數(shù)、控件這三者綁定到一起來。 |
|