Chinaunix

标题: 一個 wxPython 小例子 [完成品 at 4 樓] [打印本页]

作者: eookoo    时间: 2007-04-06 12:35
标题: 一個 wxPython 小例子 [完成品 at 4 樓]
先弄個 Hello World 好了...


  1. import wx                 

  2. app = wx.PySimpleApp()               # create 一個 wxPysimpleApp 的實例
  3. frame = wx.Frame(None, -1, "Hello World")         # create 一個 wxFrame 的實例, parent = None , id = -1, title = 'Hello World'
  4. frame.Show()                # 顯示這個框框      
  5. 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 )


  1. import wx

  2. class MainWindow(wx.Frame):                # 自己定義一個 class, 繼承了 wx.Frame
  3.         def __init__(self, parent, id, title):
  4.                 wx.Frame.__init__(self, parent, id, title, size = (300,200))
  5.                 self.control = wx.TextCtrl(self, 1, style = wx.TE_MULTILINE)    # create 一個 text area

  6. if __name__=='__main__':
  7.         app = wx.PySimpleApp()
  8.         frame = MainWindow( None, -1, 'Simple Editor')                    
  9.         frame.Show()
  10.         app.MainLoop()
复制代码




-----------------------------------------------------------------------------------------------

中文解釋好難ah......我的中文不太好...sorry.. >_< !!
Hope you guys like it..

[ 本帖最后由 eookoo 于 2007-4-9 08:33 编辑 ]
作者: eookoo    时间: 2007-04-07 08:01
這次展示如何加 menu bar, status bar, and menu


  1. import wx

  2. class SimpleEditor(wx.Frame):
  3.         def __init__(self, parent, id, title):
  4.                 wx.Frame.__init__(self, parent, id, title)
  5.                
  6.                 # ---------------------create a text area ----------------------
  7.                 self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
  8.                
  9.                 # ---------------------create a status bar ---------------------
  10.                 self.CreateStatusBar()
  11.                
  12.                 # ---------------------create a menu bar -----------------------
  13.                 menuBar = wx.MenuBar()
  14.        
  15.                 # ---------------------create a menu ---------------------------       
  16.                 # making two new menu               
  17.                 menuFile = wx.Menu()
  18.                 menuHelp = wx.Menu()               

  19.                 # lets do something with menuFile
  20.                 # adding 'Open' to menu list
  21.                 # when mouse move over it, 'open a file' will show up on the status bar
  22.                 menuFile.Append(wx.ID_ANY, '&Open', 'open a file')
  23.                 # adding 'Save' to menu list
  24.                 menuFile.Append(wx.ID_ANY, '&Save', 'save the file')
  25.                 # adding a separator to separate the menu list
  26.                 menuFile.AppendSeparator()
  27.                 # adding 'Exit' to menu list
  28.                 menuFile.Append(wx.ID_ANY, 'E&xit', 'terminate the program')
  29.                
  30.                 # now is menuHelp turn
  31.                 menuHelp.Append(wx.ID_ANY, 'A&bout', 'info about this program')

  32.                 # add the menu list we made to menu bar
  33.                 # This will show 'File' on the menu bar
  34.                 menuBar.Append(menuFile, '&File')
  35.                 # This will show 'Help' on the menu bar
  36.                 menuBar.Append(menuHelp, '&Help')

  37.                 # Last step, set the menu bar
  38.                 self.SetMenuBar(menuBar)

  39. if __name__=='__main__':
  40.         app = wx.PySimpleApp()
  41.         frame = SimpleEditor(None, wx.ID_ANY, 'Simple Editor')
  42.         frame.Show()
  43.         app.MainLoop()
  44.        

复制代码



作者: flw    时间: 2007-04-07 10:28
不错!支持!建议版主加精华。
作者: eookoo    时间: 2007-04-09 07:35
完成品是由 2 樓的 code 改成過來...
如果見到 :  # --- new: ---  or # --- Changed: ---
就是新加入的東西 or 改變過的東西


  1. import wx
  2. # -------- new: we need os module to handle path, dir stuffs-----------
  3. import os      

  4. # ------- new: make our own id numbers --------------
  5. ID_OPEN = 100      
  6. ID_SAVE = 101          
  7. ID_EXIT = 102
  8. ID_ABOUT = 200

  9. class SimpleEditor(wx.Frame):
  10.         def __init__(self, parent, id, title):
  11.                 wx.Frame.__init__(self, parent, id, title, size = (700, 600))
  12.                
  13.                 # ---------------------create a text area ----------------------
  14.                 self.text = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
  15.                
  16.                 # ---------------------create a status bar ---------------------
  17.                 self.CreateStatusBar()
  18.                
  19.                 # ---------------------create a menu bar -----------------------
  20.                 menuBar = wx.MenuBar()
  21.        
  22.                 # ---------------------create a menu ---------------------------       
  23.                 # making two new menus               
  24.                 menuFile = wx.Menu()
  25.                 menuHelp = wx.Menu()               
  26.                
  27.                 # ------------- Changed: use our own id instead wx.ID_ANY ------------------
  28.                
  29.                 #menuFile.Append(wx.ID_ANY, '&Open', 'open a file')
  30.                 #menuFile.Append(wx.ID_ANY, '&Save', 'save the file')

  31.                 menuFile.Append(ID_OPEN, '&Open', 'open a file')
  32.                 menuFile.Append(ID_SAVE, '&Save', 'save the file')
  33.                 # ----------------------------------------------------------------------

  34.                 # adding a separator to separate the menu list
  35.                 menuFile.AppendSeparator()
  36.        
  37.                 # ------------- Changed: use our own id instead wx.ID_ANY ------------------
  38.                
  39.                 #menuFile.Append(wx.ID_ANY, 'E&xit', 'terminate the program')               
  40.                 #menuHelp.Append(wx.ID_ANY, 'A&bout', 'info about this program')

  41.                 menuFile.Append(ID_EXIT, 'E&xit', 'terminate the program')               
  42.                 menuHelp.Append(ID_ABOUT, 'A&bout', 'info about this program')               
  43.                 # -------------------------------------------------------------------------

  44.                 # add the menu list we made to menu bar
  45.                 # This will show 'File' on the menu bar
  46.                 menuBar.Append(menuFile, '&File')
  47.                 # This will show 'Help' on the menu bar
  48.                 menuBar.Append(menuHelp, '&Help')
  49.                
  50.                 # Last step, set the menu bar
  51.                 self.SetMenuBar(menuBar)
  52.                        
  53.                 # ------------------------- new: 4 events handler -------------------------------
  54.                
  55.                 wx.EVT_MENU(self, ID_OPEN, self.OnOpen)
  56.                 wx.EVT_MENU(self, ID_SAVE, self.OnSave)
  57.                 wx.EVT_MENU(self, ID_EXIT, self.OnExit)
  58.                 wx.EVT_MENU(self, ID_ABOUT, self.OnAbout)

  59.         # ---------------------- new: Declare 4 functions for the events--------------------------
  60.                
  61.         # open file when you click File -> open
  62.         def OnOpen(self, event):
  63.                 # open a file using wx.FileDialog
  64.                 # wx.FileDialog(self, parent, message = FileSelectorPromptStr, defaultDir = EmptyString, defaultFile = EmptyString,                
  65.                 #                           wildcard = FileSelectorDefaultWildcardStr, style = FD_DEFAULT_STYLE, pos = DefaultPosition)  
  66.                 dlg = wx.FileDialog(self, message = 'Choose a file', defaultDir = '',
  67.                                                     defaultFile = '', wildcard = '*.*', style = wx.OPEN)
  68.                 # if we click 'OK' button it do something               
  69.                 if dlg.ShowModal() == wx.ID_OK:
  70.                         # get the file name and directory
  71.                         self.filename = dlg.GetFilename()
  72.                         self.dirname = dlg.GetDirectory()
  73.                         # add the directory path and file name
  74.                         f = open(os.path.join(self.dirname, self.filename),'r')
  75.                         # read the file , show on the text area
  76.                         self.text.SetValue(f.read())
  77.                         f.close()
  78.                 dlg.Destroy()

  79.         # save file when you click File -> save
  80.         def OnSave(self, event):
  81.                 # get the current value from text area
  82.                 itcontains = self.text.GetValue()
  83.                 # overwrite the same file with current value
  84.                 f = open(os.path.join(self.dirname, self.filename), 'w')
  85.                 f.write(itcontains)

  86.                 f.close()

  87.         # exit program when you click File -> exit
  88.         def OnExit(self, event):
  89.                 # close the frame
  90.                 self.Close(True)
  91.                
  92.         # about this program Help -> About
  93.         def OnAbout(self, event):
  94.                 # make a message dialog
  95.                 # wx.MessageDialog(self, parent, message, caption=MessageBoxCaptionStr,
  96.                 #                                 style=wxOK|wxCANCEL|wxCENTRE, pos=DefaultPosition)  
  97.                 dlg = wx.MessageDialog(self, message = 'A simple editor created by wxPython!\n'
  98.                                                           'author : eookoo \n date : Apr 4, 2007',  caption = 'About this program', style = wx.OK)
  99.                 # show the message dialog
  100.                 dlg.ShowModal()
  101.                 # we destroy it when finished
  102.                 dlg.Destroy()                       
  103.         # ----------------------------------------------------------------------------------------------
  104.        
  105. if __name__=='__main__':
  106.         app = wx.PySimpleApp()
  107.         frame = SimpleEditor(None, wx.ID_ANY, 'Simple Editor')
  108.         frame.Show()
  109.         app.MainLoop()
  110.        

复制代码


File  -> Open





Help -> About



[ 本帖最后由 eookoo 于 2007-4-9 07:43 编辑 ]
作者: I/0    时间: 2007-04-12 18:23
app.MainLoop()            # 一定要有這個, 但不知道怎樣 explain..  xP

呵呵, 以前学过一阵, 这个是启动消息循环, 不然程序就直接结束了
作者: wibrst    时间: 2007-04-12 21:09
大家可以群里讨论,
qq群"动态语言研究社"群号:38663927,欢迎加入
作者: yishanju    时间: 2007-04-12 21:45
支持一把
作者: tigerjgh    时间: 2007-05-03 22:26
这个好像wxpython的sample里面有
作者: apacher911    时间: 2007-05-09 11:24
建议楼主再多加点功能,如果能实现实现SPE, BOA中的功能,那就更加cool啦
作者: nicsky    时间: 2008-05-05 15:55
不错。。。。。。。。。。。。。。
作者: MePython    时间: 2008-06-26 13:47
标题: 回复 #1 eookoo 的帖子
再看
作者: gawk    时间: 2009-04-13 13:19
不错,最近正在看wxPython
作者: jutang    时间: 2009-08-10 20:33
非常感谢LZ的精神,谢了!
作者: nietsche    时间: 2009-08-10 20:42
学习wxpython,最好看它的wxDemo,非常简单明了!
-----如果mfc有如此例子就好了啊!
作者: kv2002    时间: 2009-09-04 11:07
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()               

不太理解为何  menuBar  不是 self.menuBar 和self.control 一样,区别在哪里
作者: oufuji    时间: 2012-08-13 21:49
回复 2# eookoo


这个代码有bug阿,运行后就有了一个text窗口可以输入文本,然后点击“SAVE”,没有保存路径,直接报错的。跟平常的编辑器有很大区别,可以加入自动识别工作目录,有个默认保存路径就不会出现了。


   
作者: wulien88    时间: 2012-11-07 08:23
这些不是都是最基本的功能吗?随便找个教程都多的是,demo里面详细的很
作者: pizgchen    时间: 2013-01-15 12:00
Wiki有一個類似的 http://wiki.wxpython.org/WxHowtoSmallEditor




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2