您的位置:首页 > 其它

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

2015-12-14 10:24 459 查看


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

import wx  

  

class MyFrame(wx.Frame):  

    def __init__(self,parent,title):  

        wx.Frame.__init__(self,parent,title=title,size=(400,300))  

        self.control = wx.TextCtrl(self,style=wx.TE_MULTILINE)  

        self.Show(True)  

  

app = wx.App(False)  

frame = MyFrame(None,"Small Editor")  

app.MainLoop()  

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

请注意,因为在MyFrame的内部运行它的__init__方法self.Show(),我们不再需要显式地调用frame.Show()。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: