(一)wxpython的安裝
Ubuntu下的安裝,還是比較簡單的。
#使用:apt-cache search wxpython 測試一下,可以看到相關信息dizzy@dizzy-pc:~/Python$ apt-cache search wxpythoncain - simulations of chemical reactionscain-examples - simulations of chemical reactionscain-solvers - simulations of chemical reactionsgnumed-client - medical practice management - Client... #這樣的話,直接使用: sudo apt-get install python-wxtools 即可安裝dizzy@dizzy-pc:~/Python$ sudo apt-get install python-wxtools[sudo] password for dizzy: Reading package lists... DoneBuilding dependency tree ...
測試是否安裝成功。進入Python,import wx 不報錯,即可
dizzy@dizzy-pc:~/Python$ pythonPython 2.7.3 (default, Apr 20 2012, 22:44:07) [GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import wx>>>
(二)顯示出一個窗口
#!/usr/bin/python#coding:utf-8 import wx def main(): app = wx.App() win = wx.Frame(None) win.Show() app.MainLoop() if __name__ == '__main__': main()#這便是一個最簡單的可視化窗口的實現
(三)添加可視化組建及簡單布局
#coding:utf-8 import wx def main(): app = wx.App() win = wx.Frame(None,title='NotePad',size=(440,320)) #很明顯,title就是標題,size就是大小 bt_open = wx.Button(win,label='open',pos=(275,2),size=(80,30)) bt_save = wx.Button(win,label='save',pos=(355,2),size=(80,30)) #label就是按鈕顯示的標簽,pos是控件左上角的相對位置,size就是控件的絕對大小 text_title = wx.TextCtrl(win,pos=(5,2),size=(265,30)) text_content = wx.TextCtrl(win,pos=(5,34),size=(430,276),style=wx.TE_MULTILINE|wx.HSCROLL) #style樣式,wx.TE_MULTILINE使其能夠多行輸入,wx.HSCROOL使其具有水平滾動條 win.Show() app.MainLoop() if __name__ == '__main__': main() #做過桌面軟件開發的,對這個肯定很熟悉。#由于之前學過一點VB,VC,Delphi等,學起來感覺很簡單。#將wx提供的控件添加到某個Frame上,并進行各自的屬性設置即可完成#由于文本控件的size屬性,設置的為絕對值。這樣就會有一些問題......
(四)界面布局管理
由于之前的控件直接綁定在Frame上,這樣會有一些問題。下面將使用Panel面板進行管理。
## 當然,之前說將各種控件的位置都寫成絕對位置和大小,會有一些問題。這是不對的## 有時需要動態布局,而有時則需要靜態布局 import wx def main(): #創建App app = wx.App() #創建Frame win = wx.Frame(None,title='NotePad',size=(440,320)) win.Show() #創建Panel panel = wx.Panel(win) #創建open,save按鈕 bt_open = wx.Button(panel,label='open') bt_save = wx.Button(panel,label='save') #創建文本框,文本域 text_filename = wx.TextCtrl(panel) text_contents = wx.TextCtrl(panel,style=wx.TE_MULTILINE|wx.HSCROLL) #添加布局管理器 bsizer_top = wx.BoxSizer() bsizer_top.Add(text_filename,proportion=1,flag=wx.EXPAND) bsizer_top.Add(bt_open,proportion=0,flag=wx.LEFT,border=5) bsizer_top.Add(bt_save,proportion=0,flag=wx.LEFT,border=5) bsizer_all = wx.BoxSizer(wx.VERTICAL) #wx.VERTICAL 橫向分割 bsizer_all.Add(bsizer_top,proportion=0,flag=wx.EXPAND|wx.LEFT,border=5) bsizer_all.Add(text_contents,proportion=1,flag=wx.EXPAND|wx.ALL,border=5) panel.SetSizer(bsizer_all) app.MainLoop() if __name__ == '__main__': main() #這個是動態布局。當然這只是一個視圖而已。#這只是個表面而已,靈魂不在此!
(五)添加控件的事件處理
直接上代碼。
#!/usr/bin/python#coding:utf-8 import wx def openfile(evt): filepath = text_filename.GetValue() fopen = file(filepath) fcontent = fopen.read() text_contents.SetValue(fcontent) fopen.close() def savefile(evt): filepath = text_filename.GetValue() filecontents = text_contents.GetValue() fopen = file(filepath,'w') fopen.write(filecontents) fopen.close() app = wx.App()#創建Framewin = wx.Frame(None,title='NotePad',size=(440,320))#創建Panelpanel = wx.Panel(win)#創建open,save按鈕bt_open = wx.Button(panel,label='open')bt_open.Bind(wx.EVT_BUTTON,openfile) #添加open按鈕事件綁定,openfile()函數處理bt_save = wx.Button(panel,label='save')bt_save.Bind(wx.EVT_BUTTON,savefile) #添加save按鈕事件綁定,savefile()函數處理#創建文本框,文本域text_filename = wx.TextCtrl(panel)text_contents = wx.TextCtrl(panel,style=wx.TE_MULTILINE|wx.HSCROLL)#添加布局管理器bsizer_top = wx.BoxSizer()bsizer_top.Add(text_filename,proportion=1,flag=wx.EXPAND,border=5)bsizer_top.Add(bt_open,proportion=0,flag=wx.LEFT,border=5)bsizer_top.Add(bt_save,proportion=0,flag=wx.LEFT,border=5) bsizer_all = wx.BoxSizer(wx.VERTICAL)bsizer_all.Add(bsizer_top,proportion=0,flag=wx.EXPAND|wx.LEFT,border=5)bsizer_all.Add(text_contents,proportion=1,flag=wx.EXPAND|wx.ALL,border=5) panel.SetSizer(bsizer_all) win.Show()app.MainLoop() 47,0-1 Bot ######################################################## 打開,保存功能基本實現,但還存在很多bug。 ## 怎么也算自己的第二個Python小程序吧?。? # ###########################################################################
(六)ListCtrl列表控件的使用示例
ListCtrl這個控件比較強大,是我比較喜歡使用的控件之一。
下面是list_report.py中提供的簡單用法:
import wximport sys, glob, randomimport dataclass DemoFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, "wx.ListCtrl in wx.LC_REPORT mode", size=(600,400)) il = wx.ImageList(16,16, True) for name in glob.glob("smicon??.png"): bmp = wx.Bitmap(name, wx.BITMAP_TYPE_PNG) il_max = il.Add(bmp) self.list = wx.ListCtrl(self, -1, style=wx.LC_REPORT) self.list.AssignImageList(il, wx.IMAGE_LIST_SMALL) # Add some columns for col, text in enumerate(data.columns): self.list.InsertColumn(col, text) # add the rows for item in data.rows: index = self.list.InsertStringItem(sys.maxint, item[0]) for col, text in enumerate(item[1:]): self.list.SetStringItem(index, col+1, text) # give each item a random image img = random.randint(0, il_max) self.list.SetItemImage(index, img, img) # set the width of the columns in various ways self.list.SetColumnWidth(0, 120) self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE) self.list.SetColumnWidth(2, wx.LIST_AUTOSIZE) self.list.SetColumnWidth(3, wx.LIST_AUTOSIZE_USEHEADER)app = wx.PySimpleApp()frame = DemoFrame()frame.Show()app.MainLoop()
如何獲取選中的項目?
最常用的方法就是獲取選中的第一項:GetFirstSelected(),這個函數返回一個int,即ListCtrl中的項(Item)的ID。
還有一個方法是:GetNextSelected(itemid),獲取指定的itemid之后的第一個被選中的項,同樣也是返回itemid。
通過這兩個方法,我們就可以遍歷所有選中的項了。