先弄個 Hello World 好了...
import wx
app = wx.PySimpleApp() # create 一個 wxPysimpleApp 的實例
frame = wx.Frame(None, -1, "Hello World") # create 一個 wxFrame 的實例, parent = None , id = -1, title = 'Hello World'
frame.Show() # 顯示這個框框
app.MainLoop() # 一定要有這個, 但不知道怎樣 explain.. xP
-----------------------------------------------------------------------------------------------
再來一個最基本 text editor 的例子
# The signature of the wx.Frame constructor is:
# wx.Frame__init__(self, parent, id = -1, title =' ', pos = wx.DeFaultPosition,
size = wx.DeFaultSize, style = wx.DEFAULT_FRAME_STYLE,
name = 'frame')
# id = -1 ( program 會自動給一個 id )
# -1 is same as wx.ID_ANY
# id = wx.ID_ANY ( recommended, because easy for people to read )
import wx
class MainWindow(wx.Frame): # 自己定義一個 class, 繼承了 wx.Frame
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size = (300,200))
self.control = wx.TextCtrl(self, 1, style = wx.TE_MULTILINE) # create 一個 text area
if __name__=='__main__':
app = wx.PySimpleApp()
frame = MainWindow( None, -1, 'Simple Editor')
frame.Show()
app.MainLoop()
-----------------------------------------------------------------------------------------------
中文解釋好難ah......我的中文不太好...sorry.. >_< !!
Hope you guys like it..
[ 本帖最后由 eookoo 于 2007-4-9 08:33 编辑 ]
eookoo 回复于:2007-04-07 08:01:54
這次展示如何加 menu bar, status bar, and menu
import wx
class SimpleEditor(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
# ---------------------create a text area ----------------------
self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
# ---------------------create a status bar ---------------------
self.CreateStatusBar()
# ---------------------create a menu bar -----------------------
menuBar = wx.MenuBar()
# ---------------------create a menu ---------------------------
# making two new menu
menuFile = wx.Menu()
menuHelp = wx.Menu()
# lets do something with menuFile
# adding 'Open' to menu list
# when mouse move over it, 'open a file' will show up on the status bar
menuFile.Append(wx.ID_ANY, '&Open', 'open a file')
# adding 'Save' to menu list
menuFile.Append(wx.ID_ANY, '&Save', 'save the file')
# adding a separator to separate the menu list
menuFile.AppendSeparator()
# adding 'Exit' to menu list
menuFile.Append(wx.ID_ANY, 'E&xit', 'terminate the program')
# now is menuHelp turn
menuHelp.Append(wx.ID_ANY, 'A&bout', 'info about this program')
# add the menu list we made to menu bar
# This will show 'File' on the menu bar
menuBar.Append(menuFile, '&File')
# This will show 'Help' on the menu bar
menuBar.Append(menuHelp, '&Help')
# Last step, set the menu bar
self.SetMenuBar(menuBar)
if __name__=='__main__':
app = wx.PySimpleApp()
frame = SimpleEditor(None, wx.ID_ANY, 'Simple Editor')
frame.Show()
app.MainLoop()

flw 回复于:2007-04-07 10:28:42
不错!支持!建议版主加精华。
eookoo 回复于:2007-04-09 07:35:57
完成品是由 2 樓的 code 改成過來...
如果見到 : # --- new: --- or # --- Changed: ---
就是新加入的東西 or 改變過的東西
import wx
# -------- new: we need os module to handle path, dir stuffs-----------
import os
# ------- new: make our own id numbers --------------
ID_OPEN = 100
ID_SAVE = 101
ID_EXIT = 102
ID_ABOUT = 200
class SimpleEditor(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size = (700, 600))
# ---------------------create a text area ----------------------
self.text = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
# ---------------------create a status bar ---------------------
self.CreateStatusBar()
# ---------------------create a menu bar -----------------------
menuBar = wx.MenuBar()
# ---------------------create a menu ---------------------------
# making two new menus
menuFile = wx.Menu()
menuHelp = wx.Menu()
# ------------- Changed: use our own id instead wx.ID_ANY ------------------
#menuFile.Append(wx.ID_ANY, '&Open', 'open a file')
#menuFile.Append(wx.ID_ANY, '&Save', 'save the file')
menuFile.Append(ID_OPEN, '&Open', 'open a file')
menuFile.Append(ID_SAVE, '&Save', 'save the file')
# ----------------------------------------------------------------------
# adding a separator to separate the menu list
menuFile.AppendSeparator()
# ------------- Changed: use our own id instead wx.ID_ANY ------------------
#menuFile.Append(wx.ID_ANY, 'E&xit', 'terminate the program')
#menuHelp.Append(wx.ID_ANY, 'A&bout', 'info about this program')
menuFile.Append(ID_EXIT, 'E&xit', 'terminate the program')
menuHelp.Append(ID_ABOUT, 'A&bout', 'info about this program')
# -------------------------------------------------------------------------
# add the menu list we made to menu bar
# This will show 'File' on the menu bar
menuBar.Append(menuFile, '&File')
# This will show 'Help' on the menu bar
menuBar.Append(menuHelp, '&Help')
# Last step, set the menu bar
self.SetMenuBar(menuBar)
# ------------------------- new: 4 events handler -------------------------------
wx.EVT_MENU(self, ID_OPEN, self.OnOpen)
wx.EVT_MENU(self, ID_SAVE, self.OnSave)
wx.EVT_MENU(self, ID_EXIT, self.OnExit)
wx.EVT_MENU(self, ID_ABOUT, self.OnAbout)
# ---------------------- new: Declare 4 functions for the events--------------------------
# open file when you click File -> open
def OnOpen(self, event):
# open a file using wx.FileDialog
# wx.FileDialog(self, parent, message = FileSelectorPromptStr, defaultDir = EmptyString, defaultFile = EmptyString,
# wildcard = FileSelectorDefaultWildcardStr, style = FD_DEFAULT_STYLE, pos = DefaultPosition)
dlg = wx.FileDialog(self, message = 'Choose a file', defaultDir = '',
defaultFile = '', wildcard = '*.*', style = wx.OPEN)
# if we click 'OK' button it do something
if dlg.ShowModal() == wx.ID_OK:
# get the file name and directory
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
# add the directory path and file name
f = open(os.path.join(self.dirname, self.filename),'r')
# read the file , show on the text area
self.text.SetValue(f.read())
f.close()
dlg.Destroy()
# save file when you click File -> save
def OnSave(self, event):
# get the current value from text area
itcontains = self.text.GetValue()
# overwrite the same file with current value
f = open(os.path.join(self.dirname, self.filename), 'w')
f.write(itcontains)
f.close()
# exit program when you click File -> exit
def OnExit(self, event):
# close the frame
self.Close(True)
# about this program Help -> About
def OnAbout(self, event):
# make a message dialog
# wx.MessageDialog(self, parent, message, caption=MessageBoxCaptionStr,
# style=wxOK|wxCANCEL|wxCENTRE, pos=DefaultPosition)
dlg = wx.MessageDialog(self, message = 'A simple editor created by wxPython!\n'
'author : eookoo \n date : Apr 4, 2007', caption = 'About this program', style = wx.OK)
# show the message dialog
dlg.ShowModal()
# we destroy it when finished
dlg.Destroy()
# ----------------------------------------------------------------------------------------------
if __name__=='__main__':
app = wx.PySimpleApp()
frame = SimpleEditor(None, wx.ID_ANY, 'Simple Editor')
frame.Show()
app.MainLoop()
File -> Open
Help -> About

[ 本帖最后由 eookoo 于 2007-4-9 07:43 编辑 ]
I/0 回复于:2007-04-12 18:23:14
app.MainLoop() # 一定要有這個, 但不知道怎樣 explain.. xP
呵呵, 以前学过一阵, 这个是启动消息循环, 不然程序就直接结束了
wibrst 回复于:2007-04-12 21:09:06
大家可以群里讨论,
qq群"动态语言研究社"群号:38663927,欢迎加入;-)
yishanju 回复于:2007-04-12 21:45:45
支持一把
tigerjgh 回复于:2007-05-03 22:26:39
这个好像wxpython的sample里面有
apacher911 回复于:2007-05-09 11:24:32
建议楼主再多加点功能,如果能实现实现SPE, BOA中的功能,那就更加cool啦
|