python使用wxpython開(kāi)發(fā)簡(jiǎn)單記事本的方法
本文實(shí)例講述了python使用wxpython開(kāi)發(fā)簡(jiǎn)單記事本的方法。分享給大家供大家參考。具體分析如下:
wxPython是Python編程語(yǔ)言的一個(gè)GUI工具箱。他使得Python程序員能夠輕松的創(chuàng)建具有健壯、功能強(qiáng)大的圖形用戶界面的程序。它是Python語(yǔ)言對(duì)流行的wxWidgets跨平臺(tái)GUI工具庫(kù)的綁定。而wxWidgets是用C++語(yǔ)言寫(xiě)成的。
和Python語(yǔ)言與wxWidgetsGUI工具庫(kù)一樣,wxPython是開(kāi)源軟件。這意味著任何人都可以免費(fèi)地使用它并且可以查看和修改它的源代碼,或者貢獻(xiàn)補(bǔ)丁,增加功能。
wxPython是跨平臺(tái)的。這意味著同一個(gè)程序可以不經(jīng)修改地在多種平臺(tái)上運(yùn)行。現(xiàn)今支持的平臺(tái)有:32位微軟Windows操作系統(tǒng)、大多數(shù)Unix或類Unix系統(tǒng)、蘋(píng)果MacOS X。
下面使用wxpython編寫(xiě)一個(gè)簡(jiǎn)單的記事本程序,可以打開(kāi)本地文件,編輯,保存。
#!/usr/bin/python
import wx
def OnOpen(event):
"""
Load a file into the textField.
"""
dialog = wx.FileDialog(None,'Notepad',style = wx.OPEN)
if dialog.ShowModal() == wx.ID_OK:
filename.SetValue(dialog.GetPath())
file = open(dialog.GetPath())
contents.SetValue(file.read())
file.close()
dialog.Destroy()
def OnSave(event):
"""
Save text into the orignal file.
"""
if filename.GetValue() == '':
dialog = wx.FileDialog(None,'Notepad',style = wx.SAVE)
if dialog.ShowModal() == wx.ID_OK:
filename.SetValue(dialog.GetPath())
file = open(dialog.GetPath(), 'w')
file.write(contents.GetValue())
file.close()
dialog.Destory()
else:
file = open(filename.GetValue(), 'w')
file.write(contents.GetValue())
file.close()
app = wx.App()
win = wx.Frame(None, title="Simple Editor", size=(600,400))
bkg = wx.Panel(win)
# Define a 'load' button and its label,
# bind to an button event with a function 'load'
loadButton = wx.Button(bkg, label='Open')
loadButton.Bind(wx.EVT_BUTTON, OnOpen)
# Define a 'save' button and its label,
# bind to an button event with a function 'save'
saveButton = wx.Button(bkg, label='Save')
saveButton.Bind(wx.EVT_BUTTON, OnSave)
# Define a textBox for filename.
filename = wx.TextCtrl(bkg)
# Define a textBox for file contents.
contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE | wx.HSCROLL)
# Use sizer to set relative position of the components.
# Horizontal layout
hbox = wx.BoxSizer()
hbox.Add(filename, proportion=1, flag=wx.EXPAND)
hbox.Add(loadButton, proportion=0, flag=wx.LEFT, border=5)
hbox.Add(saveButton, proportion=0, flag=wx.LEFT, border=5)
# Vertical layout
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(contents, proportion=1,
flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)
bkg.SetSizer(vbox)
win.Show()
app.MainLoop()
運(yùn)行效果如下圖所示:

這個(gè)例子是《Python基礎(chǔ)教程》中的一個(gè)例子,并做了一些修改。雖然完成了基本的記事本功能,但是界面略顯簡(jiǎn)單,而且代碼也沒(méi)有很好地遵循面向?qū)ο缶幊淘瓌t。
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Caffe均值文件mean.binaryproto轉(zhuǎn)mean.npy的方法
今天小編就為大家分享一篇Caffe均值文件mean.binaryproto轉(zhuǎn)mean.npy的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python進(jìn)度條實(shí)時(shí)顯示處理進(jìn)度的示例代碼
本篇文章主要介紹了Python進(jìn)度條實(shí)時(shí)顯示處理進(jìn)度的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
在終端啟動(dòng)Python時(shí)報(bào)錯(cuò)的解決方案
這篇文章主要介紹了在終端啟動(dòng)Python時(shí)報(bào)錯(cuò)的解決方案,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11
PyCharm中Matplotlib繪圖不能顯示UI效果的問(wèn)題解決
這篇文章主要介紹了PyCharm中Matplotlib繪圖不能顯示UI效果的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
詳解如何在VS Code中安裝Spire.PDF for Python
這篇文章主要為大家詳細(xì)介紹了如何在VS Code中安裝Spire.PDF for Python,文中的示例代碼簡(jiǎn)潔易懂,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10
查看keras的默認(rèn)backend實(shí)現(xiàn)方式
這篇文章主要介紹了查看keras的默認(rèn)backend實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06

