wxPython Cookbook (Chatper1)part 2

来源:互联网 发布:sun.java 编辑:程序博客网 时间:2024/06/11 10:42

引用控件

在应用中的所有窗口控件都被各种方式所连接。为了对控件能执行操作或从控件获得一些数据,取得对控件的引用是非常普遍的。这一部分将向你展示一些可用于寻找和引用控件的方法。
How to do it?
这里我们将扩展前面的MyFrame类,新增一个用于处理按钮点击事件的回调程序。在其中我们能看到一些在运行时环境下去获得我们不同界面控件的方法。
import wxclass 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 osimport wxclass MyApp(wx.App):    def OnInit(self):        self.frame = MyFrame(None, title = "Bitmaps")        self.SetTopWindow(self.frame)        self.frame.Show()                return Trueclass 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 osimport wxclass MyApp(wx.App):    def OnInit(self):        self.frame = MyFrame(None, title = "Bitmaps")        self.SetTopWindow(self.frame)        self.frame.Show()                return Trueclass 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
原创粉丝点击