wxPython Cookbook (Chatper1)part 1

在学习用wxPython开发GUI应用时,发现了一本英文的好书《wxPython 2.8 Application Development Cookbook 》。对比与另外一本已经翻译成中文的《wxPython in Action》,感觉Cookbook能更好的建立一个GUI程序的开发体系,这比单单介绍各种控件的应用要更能使开发者避免只见树木不见森林的问题。行文的结构上基本采用三个步骤:How to do it? How it works? There's more,循序渐进,倍感清爽。由于发现网络上还没有起相关的译文,就斗胆以学习笔记的形式来简单翻译下此书,主要目的还是提高运用wxPython的能力。也就逐步翻译,逐步更新了,受限于能力,计划年底前能完工吧。

Chatper1 Getting Started with wxPython 

 现如今有许多不同的跨平台Python框架来开发桌面应用。wxPython是是一系列用C++开发的跨平台框架的wxWigets库。与其它跨平台框架不同的地方在于,wxPython利用平台原生的UI工具包来生成与呈现UI组件。源于利用相同的控件和主题,一个wxPython应用与平台上其它应用相比,拥有同样的外观和体验。利用wxPython开发应用为您提供了巨大的灵活度,它可以在Windows, Macitosh OS X, Linux 和其它Unix类似的环境下运行。应用可迅速开发,并甚少或无须改动的基础上迁移到其它平台。


 Application 对象
    App 对象是库文件的引导,并初始化基础的工具包。一切wxPython的应用都必须创建一个App对象。它必须在开始创建任何其他GUI对象之前完成,以确保相互依赖的库能被正确的初始化。App对象需要维持MainLoop,它是用于驱动一个wxPython应用
How to do it?
    这里我们将创建一个"Hello World“应用来展示一个wxPython应用的基本框架
import wx

class MyApp(wx.App):
    def OnInit(self):
        wx.MessageBox("Hello wxPython", "wxApp")
        return True

if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()</span>
   运行上面的脚本会在屏幕上产生一个弹出的对话框。点击OK可以关闭并退出这个应用
How it works?
     当完成了创建后,应用对象调用了它的OnInit方法。这个方法被重写,并用来作为初始化应用的入口。通过返回True,此方法通知框架(framework)万事具备了。OnInit是绝大多数应用用来初始化和创建它们主窗口的地方。在这个例子中,我们在创建App对象时传入了False作为第一个参数。这个参数是用来告诉wxPython是否需要重定向输出。当正在开发一个应用,我们建议设置这个参数为False,这样你就能在命令行察觉到那些只是双击脚本而错过的错误提示信息。
    在完成创建和初始化后,你应该做的最后一件事情就是去调用App对象的MainLoop方法,这样便可以进入事件循环(event loop)。直到顶层窗口(top-level window)销毁或App对象告诉它去退出,MainLoop方法否则不会自行返回。wxPython是一个事件驱动系统(event-driven system),MainLoop便是整个系统的核心。在循环的迭代过程中,事件将被分发来实现GUI中的各种任务,例如处理鼠标的点击,移动窗口和重绘屏幕等。
There's more
wx.App类的构造函数包括了四个可选的关键字参数:
wx.App(redirect=Truefilename=None,useBestVisual=False,clearSigInt=True)
  • redirect: 重定向去stdout
  • filename:可以重定向去制定的文件
  • useBestVisual: 指明应用是否要采用最佳视觉体验,在大多数系统上这个设置没有效果
  • clearSigInt: Should SIGINT be cleared? Setting this to True will allow the application to be terminated by pressing Ctrl C, like most other applications. (没发现什么区别)
主界面(Main Frame)
对于大多数应用而言,你希望呈现一个窗口并通过其和用户进行交互。在wxPython中,最为典型的窗口(window)就是 Frame。下面这段代码就是向你展示如何继承Frame基类,并在一个应用中来展示它。
How to do it?
下面的例子将扩展上一个例子,增加一个最简单的空白应用窗口
import wx

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, title = "The Main Frame")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

class MyFrame(wx.Frame):
    def __init__(self, parent, id = wx.ID_ANY, title ="",
                 pos = wx.DefaultPosition, size = wx.DefaultSize,
                 style = wx.DEFAULT_FRAME_STYLE,
                 name = "MyFrame"):
                     super(MyFrame, self).__init__(parent, id, title, pos, size, style, name)
                     self.panel = wx.Panel(self)
                     
if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()
How it works?
   Frame 是主要的顶级窗口(top-level window)和大多数应用的载体(container)。我们首先来看看MyFrame这个类,这个类中需要重点关注的是我们生成了一个Panel对象作为Frame的子窗口。你可以把Panel认为是一个装载其它控件的容器。同样,为了使Frame在所有的平台上都能正确显示,把Panel作为它的主子界面是非常重要的。
首先,在App对象的OnInit方法中,我们创建了一个MyFrame的实体,并传递None作为其第一个参数。这个参数用来区分Frame的父窗口。因为这是我们的主窗口,所以我们传入None来指明其没有父窗口。其次,我们在App中调用SetTopWindow方法来设置我们刚生成的MyFrame实体作为应用的主窗口。最后,我们调用Show方法来显示我们的Frame。这个方法就是为了显示Frame对象,尽管需要直到MainLoop循环开始Frame对象才真正可视。
There's more
   Frame类的构造函数有一系列的格式标示(style flag)来设置窗口的样式和行为。这些格式标示(style flag)是可以作为比特掩码的形式来组合设置的。下面列出了主要一些设置。更多详细的内容,可以参见http://wxpython.org/onlinedocs.php
  • wx.MINIMIZE_BOX :显示一个最小化按钮
  • wx.MAXIMIZE_BOX:显示一个最大化按钮
  • wx.CLOSE_BOX:显示一个关闭按钮
  • wx.RESIZE_BORDER:允许Frame可以通过拖拽改变大小
  • wx.CAPTION:显示标题
  • wx.SYSTEM_MENU:当点击frame的图标是显示系统标题
  • wx.CLIP_CHILDREN:Eliminates flicker caused by the background being repainted (Windows only)
  • wx.DEFAULT_FRAME_STYLE:上面7个标示的综合

理解窗口(Window)的层级
   所有不同的窗口和控件在wxPython中都有一个容纳的等级。一些控件可以作为其他控件容器(container),但一些就不行。这里我们将加深你关于这个等级的理解
How to do it?
下面是我们对已有的Frame 类进行修改的新代码
import wx

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, title = "The Main Frame")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

class MyFrame(wx.Frame):
    def __init__(self, parent, id = wx.ID_ANY, title ="",
                 pos = wx.DefaultPosition, size = wx.DefaultSize,
                 style = wx.DEFAULT_FRAME_STYLE,
                 name = "MyFrame"):
                     super(MyFrame, self).__init__(parent, id, title, pos, size, style, name)
                     
                     self.panel = wx.Panel(self)
                     self.panel.SetBackgroundColour(wx.BLACK)
                     self.button = wx.Button(self.panel,
                                             label = "Push Me",
                                             pos = (50, 50))
                     
if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()
How it works?
基本上,有三类主要的窗口实体来进行层叠,下面是容纳的层级:
  • 顶级窗口(Top-Level Windows):Frames, Dialogs
  • 通用容器(General Containers):Panels, Notebooks
  • 控件(Controls):Buttons, CheckBoxes, ComboBoxes
顶级窗口位于层级顶端,它可以容纳除了其他顶级窗口外一切窗口。通用容器则次之,可以包含其它通用容器和控件。最后,在层级的末端是各种控件。在上面的例子中, Frame是作为顶级窗口, Panel 作为通用容器,而Button为控件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2016出版新书,2016.2.27重新上传,此pdf比我之前(2016.01.05)上传的那个版本更好。 Paperback: 304 pages Publisher: Packt Publishing - ebooks Account (January 6, 2016) Language: English ISBN-10: 1785287737 ISBN-13: 978-1785287732 Key Features This book empowers you to create rich cross-platform graphical user interfaces using Python It helps you develop applications that can be deployed on Windows, OSX, and Linux The recipes in the book involve real-world applications, giving you a first-hand experience of the practical scenarios Book Description wxPython is a GUI toolkit for the Python programming language built on top of the cross-platform wxWidgets GUI libraries. wxPython provides a powerful set of tools that allow you to quickly and efficiently building applications that can run on a variety of different platforms. Since wxWidgets provides a wrapper around each platform's native GUI toolkit, the applications built with wxPython will have a native look and feel wherever they are deployed. This book will provide you with the skills to build highly functional and native looking user interfaces for Python applications on multiple operating system environments. By working through the recipes, you will gain insights into and exposure to creating applications using wxPython. With a wide range of topics covered in the book, there are recipes to get the most basic of beginners started in GUI programming as well as tips to help experienced users get more out of their applications. The recipes will take you from the most basic application constructs all the way through to the deployment of complete applications. What you will learn Create full featured user interfaces Design and develop custom controls Deploy and distribute wxPython applications to Windows, Macintosh OS X, Linux, and other UNIX-like environments Handle and respond to application events Manage and display data using grids Interact with web services from your GUI Use Paint events to draw custom displays Support the display of user interfaces in multiple languages
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值