wxPython Cookbook (Chatper1)part 2

引用控件

在应用中的所有窗口控件都被各种方式所连接。为了对控件能执行操作或从控件获得一些数据,取得对控件的引用是非常普遍的。这一部分将向你展示一些可用于寻找和引用控件的方法。
How to do it?
这里我们将扩展前面的MyFrame类,新增一个用于处理按钮点击事件的回调程序。在其中我们能看到一些在运行时环境下去获得我们不同界面控件的方法。
import wx


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)
                     button = wx.Button(self.panel,
                                        label = "Get Children",
                                        pos = (50, 50))
                     self.btnId = button.GetId()
                     self.Bind(wx.EVT_BUTTON, self.OnButton, button)
    
    def OnButton(self, event):
        print "\nFrame GetChildren: "
        for child in self.GetChildren():
            print "%s" % repr(child)
        
        print "\nPanel FindWindowById: "
        button = self.panel.FindWindowById(self.btnId)
        print "%s" % repr(button)
        button.SetLabel("Changed Label")
        
        print "\nButton GetParent: "
        panel = button.GetParent()
        print "%s" % repr(panel)
        
        print "\nGet the Application Object: "
        app = wx.GetApp()
        print "%s" % repr(app)
        
        print "\nGet the Frame from the App: "
        frame = app.GetTopWindow()
        print "%s" % repr(frame)

if __name__ == "__main__":
    app = wx.PySimpleApp(False)
    MyFrame(None).Show()
    app.MainLoop()
How it works?
每一个在框架中的窗口都保持着一个对它父辈和子辈的引用。运行上述程序将展现所有窗口都共有的用于寻找和获得它们子辈及其它相关控件引用的公共方法。
  • GetChildren:返回指定控件所有子辈的列表
  • FindWindowById:通过ID寻找特定子窗口
  • GetParent:返回窗口的对应父窗口
  • wx.GetApp:用于获取唯一的Application实体的全局函数
  • App.GetTopWindow:用于获得应用的顶层窗口
点击按钮会引起OnButtion回调函数的执行。在OnButtion中,展示了如何应用上述方法的例子。它们都将返回一个GUI对象的关联。在我们的例子中,调用Panel的GetChildren方法反悔了其子控件的列表。通过迭代列表,我们打印出每一个子控件,在这里就只是按钮控件一个。为了展现我们找到了按钮控件,我们通过SetLabel方法来改变它的标签(label)。接着我们调用按钮控件的Getparent方法类来按钮的父辈,显然它就是Panel。最后通过调用全局犯法GetApp,我们获得了对于Application 对象的引用。这个对象的GetTopWindow方法将返回一个我们框架(Frame)的引用。
There's more
这里还有一些有用的方法来获得控件的关联
  • wx.FindWindowByLabel(label):通过标签(label)来寻找子窗口
  • wx.FindWindowByName(name):通过名称来寻找子窗口
  • wx.GetTopLevelParent():获得顶级窗口
应用Bitmap
在有些场合,你可能希望来在你的应用中显示一张图片。Bitmap便是在应用中显示图像的基本数据类型。在这个部分,我们将展示如何通过Bitmap来加载图片并在框架中展示。
 How to do it?
为了展示如何应用Bitmap,我们创建一个从硬盘上读取图片并在框架中展示的小应用
import os
import wx


class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, title = "Bitmaps")
        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)
                     
                     img_path = os.path.abspath("./face-grin.png")
                     bitmap = wx.Bitmap(img_path, type = wx.BITMAP_TYPE_PNG)
                     self.bitmap = wx.StaticBitmap(self.panel, bitmap = bitmap)

if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()

How it works?
在应用中展示一个Bitmap最简单的控件就是StaticBitmap。在我们实例的程序中,有一张叫做face-grin.png的图片是跟代码放在一个文件夹里的。为了显示图片,我们首先用Bitmap的构造函数来把图片加载到内存中,然后再传递给StaticBitmap控件来显示它。构造函数需要文件的路径和图片类型的参数。

There's more
下面列举了支持的图片文件类型
wx.BITMAP_TYPE_ANY
wx.BITMAP_TYPE_BMP
wx.BITMAP_TYPE_ICO
wx.BITMAP_TYPE_CUR
wx.BITMAP_TYPE_XBM
wx.BITMAP_TYPE_XPM
wx.BITMAP_TYPE_TIF
wx.BITMAP_TYPE_GIF
wx.BITMAP_TYPE_PNG
wx.BITMAP_TYPE_JPEG
wx.BITMAP_TYPE_PNM
wx.BITMAP_TYPE_PCX
wx.BITMAP_TYPE_PICT
wx.BITMAP_TYPE_ICON
wx.BITMAP_TYPE_ANI
wx.BITMAP_TYPE_IFF

向窗口(Window)添加图标(Icon)
为你的应用的标题栏添加图标是帮你区分不同的应用和彰显特色的好方法。这个部分我们将展示是如何轻易就可以为窗口增加图标。
How to do it?
这里我们创建一个框架(Frame)的子类用于加载图标并显示在标题栏中
import os
import wx


class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, title = "Bitmaps")
        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)
                     
                     path = os.path.abspath("./face-monkey.png")
                     icon = wx.Icon(path, wx.BITMAP_TYPE_PNG)
                     self.SetIcon(icon)

if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()
How it works?
我们在上面的程序中将一个猴子小图标(16X16)显示在框架的标题栏中。为了简化,图片与程序脚本放在同一个子目录里,我们在加载的时候用了相对路径。框架需要一个图标(Icon)而非Bitmap,所以我们用Icon来加载图片到内存中。在加载完毕后,剩下的事情便是调用框架(Frame)的SetIcon方法来设置图标。


  • 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、付费专栏及课程。

余额充值