1 import wx
2 app = wx.App(False)
3 frame = wx.Frame(None, wx.ID_ANY, "Hollo World")
4 frame.Show(True)
5 app.MainLoop()
2是創(chuàng)造一個wx.App實例。參數(shù)是“False”的意思是不將stdout和stderr重定向到一個窗口,這個參數(shù)是“True”對這個例子沒有影響。
3創(chuàng)建一個頂級窗口,語法為x.Frame(parent,ID,標題)。這個例子中wx.ID_ANY wxWidgets為我們挑選一個id。
4顯示窗口
5主循環(huán),處理事件

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、羅莊ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學管理、有技術(shù)的羅莊網(wǎng)站制作公司
import wx
class my_frame(wx.Frame):
"""We simple derive a new class of Frame"""
def __init__(self,parent, title):
wx.Frame.__init__(self, parent, title=title,size=(300,100))
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.Show(True)
app = wx.App(False)
frame = my_frame (None,'Small edior')
app.MainLoop()
繼承來自wx.Frame的__init__方法。聲明一個wx.TextCtrl控件
(簡單的文本編輯控件)
import wx
class my_frame(wx.Frame):
"""We simple derive a new class of Frame"""
def __init__(self,parent, title):
wx.Frame.__init__(self, parent, title=title,size=(300,200))
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE,)
self.Show(True)
self.CreateStatusBar()#創(chuàng)建窗口底部的狀態(tài)欄
filemenu = wx.Menu()
filemenu.Append(wx.ID_EXIT, "Exit", "Termanate the program")
filemenu.AppendSeparator()
filemenu.Append(wx.ID_ABOUT, "About", "Information about this program")#設(shè)置菜單的內(nèi)容
menuBar = wx.MenuBar()
menuBar.Append(filemenu, u"設(shè)置")
self.SetMenuBar(menuBar)#創(chuàng)建菜單條
self.Show(True)
app = wx.App(False)
frame = my_frame(None, 'Small edior')
app.MainLoop()
wx.ID_ABOUT和wx.id_EXIT這是標準wxWidgets提供的id,這樣做的好處是可以保證兼容性,多個平臺可以運行
import wx
class my_frame(wx.Frame):
"""We simple derive a new class of Frame"""
def __init__(self,parent, title):
wx.Frame.__init__(self, parent, title=title,size=(300,200))
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE,)
self.Show(True)
self.CreateStatusBar()#創(chuàng)建窗口底部的狀態(tài)欄
filemenu = wx.Menu()
menu_exit = filemenu.Append(wx.ID_EXIT, "Exit", "Termanate the program")
filemenu.AppendSeparator()
menu_about = filemenu.Append(wx.ID_ABOUT, "About", "Information about this program")#設(shè)置菜單的內(nèi)容
menuBar = wx.MenuBar()
menuBar.Append(filemenu, u"設(shè)置")
self.SetMenuBar(menuBar)#創(chuàng)建菜單條
self.Show(True)
self.Bind(wx.EVT_MENU, self.on_about, menu_about)
self.Bind(wx.EVT_MENU, self.on_exit, menu_exit)#把出現(xiàn)的事件,同需要處理的函數(shù)連接起來
def on_about(self,e):#about按鈕的處理函數(shù)
dlg = wx.MessageDialog(self,"A samll text editor", "About sample Editor",wx.OK)#創(chuàng)建一個對話框,有一個ok的按鈕
dlg.ShowModal()#顯示對話框
dlg.Destroy()#完成后,銷毀它。
def on_exit(self,e):
self.Close(True)
app = wx.App(False)
frame = my_frame(None, 'Small edior')
app.MainLoop()
第一步是設(shè)定事件,然后設(shè)定事件出現(xiàn)后應(yīng)該執(zhí)行什么操作,最后把事件和操作連接起來。
def on_open(self,e):
"""open a file"""
self.dirname = ''
dlg = wx.FileDialog(self,"Choose a file", self.dirname, "","*.*",wx.OPEN)#調(diào)用一個函數(shù)打開對話框
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname,self.filename),"r")
dlg.Destroy()
然后把這個方法和添加進入菜單和一個按鈕事件綁定起來
完整代碼
import wx
import os
class my_frame(wx.Frame):
"""This is a simple text editor"""
def __init__(self,parent, title):
wx.Frame.__init__(self, parent, title=title,size=(300,200))
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE,)
self.Show(True)
self.CreateStatusBar()#創(chuàng)建窗口底部的狀態(tài)欄
filemenu = wx.Menu()
menu_open = filemenu.Append(wx.ID_OPEN,U"打開文件", " ")
menu_exit = filemenu.Append(wx.ID_EXIT, "Exit", "Termanate the program")
filemenu.AppendSeparator()
menu_about = filemenu.Append(wx.ID_ABOUT, "About", "Information about this program")#設(shè)置菜單的內(nèi)容
menuBar = wx.MenuBar()
menuBar.Append(filemenu, u"設(shè)置")
self.SetMenuBar(menuBar)#創(chuàng)建菜單條
self.Show(True)
self.Bind(wx.EVT_MENU,self.on_open,menu_open)
self.Bind(wx.EVT_MENU, self.on_about, menu_about)
self.Bind(wx.EVT_MENU, self.on_exit, menu_exit)#把出現(xiàn)的事件,同需要處理的函數(shù)連接起來
def on_about(self,e):#about按鈕的處理函數(shù)
dlg = wx.MessageDialog(self,"A samll text editor", "About sample Editor",wx.OK)#創(chuàng)建一個對話框,有一個ok的按鈕
dlg.ShowModal()#顯示對話框
dlg.Destroy()#完成后,銷毀它。
def on_exit(self,e):
self.Close(True) def on_open(self,e):
"""open a file"""
self.dirname = ''
dlg = wx.FileDialog(self,"Choose a file", self.dirname, "","*.*",wx.OPEN)#調(diào)用一個函數(shù)打開對話框
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname,self.filename),"r")
dlg.Destroy()
app = wx.App(False)
frame = my_frame(None, 'Small edior')
app.MainLoop()
# -*- coding: utf-8 -*-
import wximport os
class my_frame(wx.Frame):
"""This is a simple text editor"""
def __init__(self,parent, title):
wx.Frame.__init__(self, parent, title=title,size=(300,200))
self.control = wx.TextCtrl(self, -1,u"請先打開要修改的文件", style=wx.TE_MULTILINE,)
self.Show(True)
self.CreateStatusBar()#創(chuàng)建窗口底部的狀態(tài)欄
filemenu = wx.Menu()
menu_open = filemenu.Append(wx.ID_OPEN, U"打開文件", " ")
menu_save = filemenu.Append(wx.ID_SAVE, U"保存修改",)
menu_exit = filemenu.Append(wx.ID_EXIT, "Exit", "Termanate the program")
filemenu.AppendSeparator()
menu_about = filemenu.Append(wx.ID_ABOUT, "About", "Information about this program")#設(shè)置菜單的內(nèi)容
menuBar = wx.MenuBar()
menuBar.Append(filemenu, u"選項")
self.SetMenuBar(menuBar)#創(chuàng)建菜單條
self.Show(True)
self.Bind(wx.EVT_MENU, self.on_open, menu_open)
self.Bind(wx.EVT_MENU, self.on_about, menu_about)
self.Bind(wx.EVT_MENU, self.on_exit, menu_exit)#把出現(xiàn)的事件,同需要處理的函數(shù)連接起來
self.Bind(wx.EVT_MENU, self.on_save, menu_save) def on_about(self,e):#about按鈕的處理函數(shù)
dlg = wx.MessageDialog(self,"A samll text editor", "About sample Editor",wx.OK)#創(chuàng)建一個對話框,有一個ok的按鈕
dlg.ShowModal()#顯示對話框
dlg.Destroy()#完成后,銷毀它。
def on_exit(self,e):
self.Close(True) def on_open(self,e):
"""open a file"""
self.dirname = ''
dlg = wx.FileDialog(self,"Choose a file", self.dirname, "","*.*",wx.OPEN)#調(diào)用一個函數(shù)打開對話框
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
self.address = os.path.join(self.dirname,self.filename)
f = open(self.address,"r")
file = (f.read()).decode(encoding='utf-8')#解碼,使文件可以讀取中文
f.close()
self.control.Clear()
self.control.AppendText(file)#把打開的文件內(nèi)容顯示在多行文本框內(nèi)
dlg.Destroy() def on_save(self, e):
date = (self.control.GetValue()).encode(encoding="utf-8")#編碼,使中文可以正確存儲
f = open(self.address, 'w')
f.write(date)
f.close()#把文本框內(nèi)的數(shù)據(jù)寫入并關(guān)閉文件
dlg = wx.MessageDialog(self, u"文件已經(jīng)成功保存", u"消息提示", wx.OK)
dlg.ShowModal()
dlg.Destroy()
self.control.Clear()
self.control.AppendText(u'歡迎使用此軟件,作者即刻')
app = wx.App(False)
frame = my_frame(None, u'迷你文本編輯器')
app.MainLoop()
文章名稱:wxpython快速入門
分享鏈接:http://www.chinadenli.net/article4/pgceie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、用戶體驗、關(guān)鍵詞優(yōu)化、網(wǎng)站導航、手機網(wǎng)站建設(shè)、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)