在Ubuntu系統(tǒng)下安裝使用Python的GUI工具wxPython
(一)wxpython的安裝
Ubuntu下的安裝,還是比較簡單的。
#使用:apt-cache search wxpython 測(cè)試一下,可以看到相關(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 ...
測(cè)試是否安裝成功。進(jìn)入Python,import wx 不報(bào)錯(cuò),即可
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 >>>
(二)顯示出一個(gè)窗口
#!/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() #這便是一個(gè)最簡單的可視化窗口的實(shí)現(xiàn)
(三)添加可視化組建及簡單布局
#coding:utf-8 import wx def main(): app = wx.App() win = wx.Frame(None,title='NotePad',size=(440,320)) #很明顯,title就是標(biāo)題,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就是按鈕顯示的標(biāo)簽,pos是控件左上角的相對(duì)位置,size就是控件的絕對(duì)大小 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使其具有水平滾動(dòng)條 win.Show() app.MainLoop() if __name__ == '__main__': main() #做過桌面軟件開發(fā)的,對(duì)這個(gè)肯定很熟悉。 #由于之前學(xué)過一點(diǎn)VB,VC,Delphi等,學(xué)起來感覺很簡單。 #將wx提供的控件添加到某個(gè)Frame上,并進(jìn)行各自的屬性設(shè)置即可完成 #由于文本控件的size屬性,設(shè)置的為絕對(duì)值。這樣就會(huì)有一些問題......
(四)界面布局管理
由于之前的控件直接綁定在Frame上,這樣會(huì)有一些問題。下面將使用Panel面板進(jìn)行管理。
## 當(dāng)然,之前說將各種控件的位置都寫成絕對(duì)位置和大小,會(huì)有一些問題。這是不對(duì)的
## 有時(shí)需要?jiǎng)討B(tài)布局,而有時(shí)則需要靜態(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()
#這個(gè)是動(dòng)態(tài)布局。當(dāng)然這只是一個(gè)視圖而已。
#這只是個(gè)表面而已,靈魂不在此!
(五)添加控件的事件處理
直接上代碼。
#!/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
#######################################################
# 打開,保存功能基本實(shí)現(xiàn),但還存在很多bug。 #
# 怎么也算自己的第二個(gè)Python小程序吧?。? #
###########################################################################
(六)ListCtrl列表控件的使用示例
ListCtrl這個(gè)控件比較強(qiáng)大,是我比較喜歡使用的控件之一。
下面是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()
如何獲取選中的項(xiàng)目?
最常用的方法就是獲取選中的第一項(xiàng):GetFirstSelected(),這個(gè)函數(shù)返回一個(gè)int,即ListCtrl中的項(xiàng)(Item)的ID。
還有一個(gè)方法是:GetNextSelected(itemid),獲取指定的itemid之后的第一個(gè)被選中的項(xiàng),同樣也是返回itemid。
通過這兩個(gè)方法,我們就可以遍歷所有選中的項(xiàng)了。
相關(guān)文章
Python使用post及get方式提交數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇關(guān)于Python使用post及get方式提交數(shù)據(jù)的實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01
Python socket.error: [Errno 98] Address already in use的原因和解決
這篇文章主要介紹了Python socket.error: [Errno 98] Address already in use的原因和解決方法,在Python的socket編程中可能會(huì)經(jīng)常遇到這個(gè)問題,需要的朋友可以參考下2014-08-08
Python?numpy生成矩陣基礎(chǔ)用法實(shí)例代碼
矩陣是matrix類型的對(duì)象,該類繼承自numpy.ndarray,任何針對(duì)ndarray的操作,對(duì)矩陣對(duì)象同樣有效,下面這篇文章主要給大家介紹了關(guān)于Python?numpy生成矩陣基礎(chǔ)的相關(guān)資料,需要的朋友可以參考下2022-08-08
Django如何繼承AbstractUser擴(kuò)展字段
這篇文章主要介紹了Django如何繼承AbstractUser擴(kuò)展字段,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
python實(shí)現(xiàn)定時(shí)自動(dòng)備份文件到其他主機(jī)的實(shí)例代碼
這篇文章主要介紹了python實(shí)現(xiàn)定時(shí)自動(dòng)備份文件到其他主機(jī)的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-02-02

