Python中使用wxPython開發(fā)的一個簡易筆記本程序?qū)嵗?/h1>
更新時間:2015年02月08日 11:49:15 投稿:junjie
這篇文章主要介紹了Python中使用wxPython開發(fā)的一個簡易筆記本程序?qū)嵗?本文實現(xiàn)打開文件、修改并保存文件功能,需要的朋友可以參考下
一、簡介
wxPython是Python語言的一套優(yōu)秀的GUI圖形庫,允許Python程序員很方便的創(chuàng)建完整的、功能鍵全的GUI用戶界面。 wxPython是作為優(yōu)秀的跨平臺GUI庫wxWidgets的Python封裝和Python模塊的方式提供給用戶的。
二、安裝
參考官方網(wǎng)站:http://www.wxpython.org/download.php
三、DEMO
本demo是一個簡單的記事本軟件,可以打開文件,修改并保存。
import wx
app = wx.App()
win = wx.Frame(
None,
title="simple editor",
size=(410, 335))
bkg = wx.Panel(win)
def openFile(evt):
dlg = wx.FileDialog(
win,
"Open",
"",
"",
"All files (*.*)|*.*",
wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
filepath = ''
if dlg.ShowModal() == wx.ID_OK:
filepath = dlg.GetPath()
else:
return
filename.SetValue(filepath)
fopen = open(filepath)
fcontent = fopen.read()
contents.SetValue(fcontent)
fopen.close()
def saveFile(evt):
fcontent = contents.GetValue()
fopen = open(filename.GetValue(), 'w')
fopen.write(fcontent)
fopen.close()
openBtn = wx.Button(bkg, label='open')
openBtn.Bind(wx.EVT_BUTTON, openFile)
saveBtn = wx.Button(bkg, label='save')
saveBtn.Bind(wx.EVT_BUTTON, saveFile)
filename = wx.TextCtrl(bkg, style=wx.TE_READONLY)
contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE)
hbox = wx.BoxSizer()
hbox.Add(openBtn, proportion=0, flag=wx.LEFT | wx.ALL, border=5)
hbox.Add(filename, proportion=1, flag=wx.EXPAND | wx.TOP | wx.BOTTOM, border=5)
hbox.Add(saveBtn, proportion=0, flag=wx.LEFT | wx.ALL, border=5)
bbox = wx.BoxSizer(wx.VERTICAL)
bbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.ALL)
bbox.Add(
contents,
proportion=1,
flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT,
border=5)
bkg.SetSizer(bbox)
win.Show()
app.MainLoop()
運行效果:

相關(guān)文章
-
scrapy結(jié)合selenium解析動態(tài)頁面的實現(xiàn)
這篇文章主要介紹了scrapy結(jié)合selenium解析動態(tài)頁面的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧 2020-09-09
-
Python 之pandas庫的安裝及庫安裝方法小結(jié)
Pandas 是一種開源的、易于使用的數(shù)據(jù)結(jié)構(gòu)和Python編程語言的數(shù)據(jù)分析工具,它與 Scikit-learn 兩個模塊幾乎提供了數(shù)據(jù)科學家所需的全部工具,今天通過本文給大家介紹Python 之pandas庫的安裝及庫安裝方法小結(jié),感興趣的朋友跟隨小編一起看看吧 2022-11-11
最新評論
一、簡介
wxPython是Python語言的一套優(yōu)秀的GUI圖形庫,允許Python程序員很方便的創(chuàng)建完整的、功能鍵全的GUI用戶界面。 wxPython是作為優(yōu)秀的跨平臺GUI庫wxWidgets的Python封裝和Python模塊的方式提供給用戶的。
二、安裝
參考官方網(wǎng)站:http://www.wxpython.org/download.php
三、DEMO
本demo是一個簡單的記事本軟件,可以打開文件,修改并保存。
import wx
app = wx.App()
win = wx.Frame(
None,
title="simple editor",
size=(410, 335))
bkg = wx.Panel(win)
def openFile(evt):
dlg = wx.FileDialog(
win,
"Open",
"",
"",
"All files (*.*)|*.*",
wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
filepath = ''
if dlg.ShowModal() == wx.ID_OK:
filepath = dlg.GetPath()
else:
return
filename.SetValue(filepath)
fopen = open(filepath)
fcontent = fopen.read()
contents.SetValue(fcontent)
fopen.close()
def saveFile(evt):
fcontent = contents.GetValue()
fopen = open(filename.GetValue(), 'w')
fopen.write(fcontent)
fopen.close()
openBtn = wx.Button(bkg, label='open')
openBtn.Bind(wx.EVT_BUTTON, openFile)
saveBtn = wx.Button(bkg, label='save')
saveBtn.Bind(wx.EVT_BUTTON, saveFile)
filename = wx.TextCtrl(bkg, style=wx.TE_READONLY)
contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE)
hbox = wx.BoxSizer()
hbox.Add(openBtn, proportion=0, flag=wx.LEFT | wx.ALL, border=5)
hbox.Add(filename, proportion=1, flag=wx.EXPAND | wx.TOP | wx.BOTTOM, border=5)
hbox.Add(saveBtn, proportion=0, flag=wx.LEFT | wx.ALL, border=5)
bbox = wx.BoxSizer(wx.VERTICAL)
bbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.ALL)
bbox.Add(
contents,
proportion=1,
flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT,
border=5)
bkg.SetSizer(bbox)
win.Show()
app.MainLoop()
運行效果:

相關(guān)文章
scrapy結(jié)合selenium解析動態(tài)頁面的實現(xiàn)
這篇文章主要介紹了scrapy結(jié)合selenium解析動態(tài)頁面的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
Python 之pandas庫的安裝及庫安裝方法小結(jié)
Pandas 是一種開源的、易于使用的數(shù)據(jù)結(jié)構(gòu)和Python編程語言的數(shù)據(jù)分析工具,它與 Scikit-learn 兩個模塊幾乎提供了數(shù)據(jù)科學家所需的全部工具,今天通過本文給大家介紹Python 之pandas庫的安裝及庫安裝方法小結(jié),感興趣的朋友跟隨小編一起看看吧2022-11-11

