之前,我們已經使用BOA Constructor撰寫過幾個簡單的視窗程式介面,計算機的小程式讓我們知道如何控制視窗上的按鈕,只繪畫直線的那支程式讓我們簡單的學會如何在視窗上面畫圖,接下來我們將學習一些簡單的IO控制,如何使用文字方塊來得知使用者輸入的資料,如何做出確認對話盒,讓使用者確認是否要執行動作,並且在這個範例中,我們會將使用者所設定的資料儲存起來,並且在下次需要時,重新載入資料。大部分的介面可以利用BOA Constructor來幫我們產生,產生完介面之後在加入部分的程式碼來控制程式,相信大家應該很熟悉這樣的程式設計方式。
儲存資料設定的程式碼
def OnSaveDataButton(self, event):
dlg = wx.MessageDialog(self, u'\u662f\u5426\u5132\u5b58\u8a2d\u5b9a\u6a94', u'\u8a2d\u5b9a\u6a94', wx.YES_NO | wx.ICON_INFORMATION)
ans = wx.ID_NO
try:
ans = dlg.ShowModal()
finally:
dlg.Destroy()
if ans == wx.ID_YES:
split='\n[_config_]\n'
data=''
data = data + self.textBox.GetValue() + split
data = data + str(self.boyRadio.GetValue()) + split
data = data + str(self.girlRadio.GetValue()) + split
data = data + str(self.marryRadio.GetValue()) + split
data = data + str(self.numarryRadio.GetValue()) + split
data = data + str(self.school1.GetValue()) + split
data = data + str(self.school2.GetValue()) + split
data = data + str(self.school3.GetValue()) + split
data = data + str(self.school4.GetValue())
f=codecs.open('./config.txt','w', 'utf-8')
f.write(data)
f.close()
dlg = wx.MessageDialog(self, u'\u662f\u5426\u5132\u5b58\u8a2d\u5b9a\u6a94', u'\u8a2d\u5b9a\u6a94', wx.YES_NO | wx.ICON_INFORMATION)
ans = wx.ID_NO
try:
ans = dlg.ShowModal()
finally:
dlg.Destroy()
if ans == wx.ID_YES:
split='\n[_config_]\n'
data=''
data = data + self.textBox.GetValue() + split
data = data + str(self.boyRadio.GetValue()) + split
data = data + str(self.girlRadio.GetValue()) + split
data = data + str(self.marryRadio.GetValue()) + split
data = data + str(self.numarryRadio.GetValue()) + split
data = data + str(self.school1.GetValue()) + split
data = data + str(self.school2.GetValue()) + split
data = data + str(self.school3.GetValue()) + split
data = data + str(self.school4.GetValue())
f=codecs.open('./config.txt','w', 'utf-8')
f.write(data)
f.close()
這段程式碼主要是把資料儲存到config.txt這個檔案中,我們將資料以'\n[_config_]\n'來作區隔,以便將來將資料讀出來時可以知道所存放的資料到底是屬於哪一各部分的資料。
執行說明
安裝軟體Python直譯器
解開壓縮檔後執行MainApp.py
全站熱搜
留言列表