wxPython构建一个简单的文本编辑器

来源:互联网 发布:甬温动车事故 知乎 编辑:程序博客网 时间:2024/06/09 16:27

Windows or Frames?

When people talk about GUIs, they usually speak of windows, menus and icons. Naturally then, you would expect thatwx.Window should represent a window on the screen. Unfortunately, this is not the case. Awx.Window is the base class from which all visual elements are derived (buttons, menus, etc) and what we normally think of as a program window is awx.Frame. This is an unfortunate inconsistency that has led to much confusion for new users.

 

窗口和框架?

当人们谈论起GUIs,他们通常说的是窗口,菜单和图标。自然地,你也期待wx.Window应该是代表屏幕上的一个窗口。

不幸的是,情况不是这样的。一个wx.Window是一个基类,它从视觉派生而来(buttons,menus,等),并且我们通常认为作为一个程序的窗口就是wx.Frame。

这是一个不幸的前后矛盾,这已经导致了许多新手感到困惑。

 

构建一个简单的文本编辑器

在本教程中,我们将构建一个简单的文本编辑器。在这个过程中,我们将研究几个小部件,并了解特性,比如事件和回调。

 

第一步是要做一个简单的框架内与一个可编辑的文本框。一个文本框是TextCtrl小部件。默认情况下,一个文本框是一个单行的领域,但是wx.TE_MULTILINE参数允许您输入多个文本的行。

[python] view plaincopy
  1. import wx  
  2.   
  3. class MyFrame(wx.Frame):  
  4.     def __init__(self,parent,title):  
  5.         wx.Frame.__init__(self,parent,title=title,size=(400,300))  
  6.         self.control = wx.TextCtrl(self,style=wx.TE_MULTILINE)  
  7.         self.Show(True)  
  8.   
  9. app = wx.App(False)  
  10. frame = MyFrame(None,"Small Editor")  
  11. app.MainLoop()  


这个例子里,我们从wx.Frame派生并重写它的__init__方法。声明一个可以编辑TextCtrl控件。

请注意,因为MyFrame的内部运行__init__方法self.Show),我们不再需要显式地调用frame.Show()。

原创粉丝点击