在Ubuntu系統(tǒng)下安裝使用Python的GUI工具wxPython
(一)wxpython的安裝
Ubuntu下的安裝,還是比較簡單的。
#使用:apt-cache search wxpython 測試一下,可以看到相關(guān)信息 dizzy@dizzy-pc:~/Python$ apt-cache search wxpython cain - simulations of chemical reactions cain-examples - simulations of chemical reactions cain-solvers - simulations of chemical reactions gnumed-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... Done Building dependency tree ...
測試是否安裝成功。進入Python,import wx 不報錯,即可
dizzy@dizzy-pc:~/Python$ python Python 2.7.3 (default, Apr 20 2012, 22:44:07) [GCC 4.6.3] on linux2 Type "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() #這便是一個最簡單的可視化窗口的實現(xiàn)
(三)添加可視化組建及簡單布局
#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() #做過桌面軟件開發(fā)的,對這個肯定很熟悉。 #由于之前學(xué)過一點VB,VC,Delphi等,學(xué)起來感覺很簡單。 #將wx提供的控件添加到某個Frame上,并進行各自的屬性設(shè)置即可完成 #由于文本控件的size屬性,設(shè)置的為絕對值。這樣就會有一些問題......
(四)界面布局管理
由于之前的控件直接綁定在Frame上,這樣會有一些問題。下面將使用Panel面板進行管理。
## 當(dāng)然,之前說將各種控件的位置都寫成絕對位置和大小,會有一些問題。這是不對的
## 有時需要動態(tài)布局,而有時則需要靜態(tài)布局
import wx
def main():
#創(chuàng)建App
app = wx.App()
#創(chuàng)建Frame
win = wx.Frame(None,title='NotePad',size=(440,320))
win.Show()
#創(chuàng)建Panel
panel = wx.Panel(win)
#創(chuàng)建open,save按鈕
bt_open = wx.Button(panel,label='open')
bt_save = wx.Button(panel,label='save')
#創(chuàng)建文本框,文本域
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()
#這個是動態(tài)布局。當(dāng)然這只是一個視圖而已。
#這只是個表面而已,靈魂不在此!
(五)添加控件的事件處理
直接上代碼。
#!/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()
#創(chuàng)建Frame
win = wx.Frame(None,title='NotePad',size=(440,320))
#創(chuàng)建Panel
panel = wx.Panel(win)
#創(chuàng)建open,save按鈕
bt_open = wx.Button(panel,label='open')
bt_open.Bind(wx.EVT_BUTTON,openfile) #添加open按鈕事件綁定,openfile()函數(shù)處理
bt_save = wx.Button(panel,label='save')
bt_save.Bind(wx.EVT_BUTTON,savefile) #添加save按鈕事件綁定,savefile()函數(shù)處理
#創(chuàng)建文本框,文本域
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
#######################################################
# 打開,保存功能基本實現(xiàn),但還存在很多bug。 #
# 怎么也算自己的第二個Python小程序吧??! #
###########################################################################
(六)ListCtrl列表控件的使用示例
ListCtrl這個控件比較強大,是我比較喜歡使用的控件之一。
下面是list_report.py中提供的簡單用法:
import wx
import sys, glob, random
import data
class 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(),這個函數(shù)返回一個int,即ListCtrl中的項(Item)的ID。
還有一個方法是:GetNextSelected(itemid),獲取指定的itemid之后的第一個被選中的項,同樣也是返回itemid。
通過這兩個方法,我們就可以遍歷所有選中的項了。
相關(guān)文章
Python使用post及get方式提交數(shù)據(jù)的實例
今天小編就為大家分享一篇關(guān)于Python使用post及get方式提交數(shù)據(jù)的實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
Python socket.error: [Errno 98] Address already in use的原因和解決
這篇文章主要介紹了Python socket.error: [Errno 98] Address already in use的原因和解決方法,在Python的socket編程中可能會經(jīng)常遇到這個問題,需要的朋友可以參考下2014-08-08
python實現(xiàn)定時自動備份文件到其他主機的實例代碼
這篇文章主要介紹了python實現(xiàn)定時自動備份文件到其他主機的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-02-02

