免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 19853 | 回复: 17
打印 上一主题 下一主题

一個 wxPython 小例子 [完成品 at 4 樓] [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-06 12:35 |只看该作者 |倒序浏览
先弄個 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 编辑 ]

论坛徽章:
0
2 [报告]
发表于 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.        

复制代码


论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2007-04-07 10:28 |只看该作者
不错!支持!建议版主加精华。

论坛徽章:
0
4 [报告]
发表于 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 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2007-04-12 18:23 |只看该作者
app.MainLoop()            # 一定要有這個, 但不知道怎樣 explain..  xP

呵呵, 以前学过一阵, 这个是启动消息循环, 不然程序就直接结束了

论坛徽章:
0
6 [报告]
发表于 2007-04-12 21:09 |只看该作者
大家可以群里讨论,
qq群"动态语言研究社"群号:38663927,欢迎加入

论坛徽章:
0
7 [报告]
发表于 2007-04-12 21:45 |只看该作者
支持一把

论坛徽章:
0
8 [报告]
发表于 2007-05-03 22:26 |只看该作者
这个好像wxpython的sample里面有

论坛徽章:
0
9 [报告]
发表于 2007-05-09 11:24 |只看该作者
建议楼主再多加点功能,如果能实现实现SPE, BOA中的功能,那就更加cool啦

论坛徽章:
0
10 [报告]
发表于 2008-05-05 15:55 |只看该作者
不错。。。。。。。。。。。。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP