wxPython:如何从 wx.StaticText 获取 sizer? [英] wxPython: How to get sizer from wx.StaticText?

查看:42
本文介绍了wxPython:如何从 wx.StaticText 获取 sizer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该小部件位于我制作的众多 sizer 之一中,但如何获取这些小部件之一的 sizer,例如在 wx.StaticText 中.首先,我虽然wx.StaticText有一个方法GetSizer(),因为它派生自wx.Window,但它总是返回None,有没有方法?

The widget is in one of many sizers I make, but how to get sizer of one of these widgets, for example in wx.StaticText. First, I though wx.StaticText have a method GetSizer() because it derived from wx.Window, but it always return None, is there a way?

抱歉我的语言不好.

编辑 (08/23/2012) 来自 Mike Driscoll 的解决方案:

EDIT (08/23/2012) Solution from Mike Driscoll:

使用 self.sizer.GetChildren() 从某个 sizer 获取 SizerItemList,然后使用 GetWindow() 从列表

Using self.sizer.GetChildren() to get SizerItemList from some sizer, then using GetWindow() to get actual widget from the list

推荐答案

如果 sizer 有子项,则 GetChildren 确实返回小部件列表.我已经用 wxPython 2.8 做过很多次了.我不记得有人提到它在 2.9 或 Phoenix 中有所不同,所以我猜不是.您能告诉我们您使用的是哪个操作系统和 wxPython 版本吗?

If the sizer has children, then GetChildren does return a list of widgets. I've done it many times with wxPython 2.8. I don't remember anyone mentioning it was different in 2.9 or Phoenix, so I'm guessing it's not. Can you tell us which OS and wxPython version you're using?

如果您想知道如何获得任意的 sizer,您可以尝试 GetContainingSizer 或使用 Widget检测工具

If you want to know how to get an arbitrary sizer, you might try GetContainingSizer or use the Widget Inspection Tool

编辑 (08/22/2012):这是一个工作示例:

EDIT (08/22/2012): Here's a working example:

import wx

########################################################################
class MyApp(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Example")
        panel = wx.Panel(self)

        lbl = wx.StaticText(panel, label="I'm a label!")
        txt = wx.TextCtrl(panel, value="blah blah")
        btn = wx.Button(panel, label="Clear")
        btn.Bind(wx.EVT_BUTTON, self.onClear)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(lbl, 0, wx.ALL, 5)
        self.sizer.Add(txt, 0, wx.ALL, 5)
        self.sizer.Add(btn, 0, wx.ALL, 5)

        panel.SetSizer(self.sizer)

    #----------------------------------------------------------------------
    def onClear(self, event):
        """"""
        children = self.sizer.GetChildren()

        for child in children:
            widget = child.GetWindow()
            print widget
            if isinstance(widget, wx.TextCtrl):
                widget.Clear()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyApp()
    frame.Show()
    app.MainLoop()

这篇关于wxPython:如何从 wx.StaticText 获取 sizer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆