简体   繁体   English

在 wxpython 中,是否可以填充由 xrc 文件定义的 gui 元素内部的内容

[英]In wxpython, is it possible to populate something inside of a gui element that is defined by an xrc file

I have a wxpython gui, which I defined in an xrc file, the root item (idk if that is the right term) of this gui is a wxFrame, which I load into a wxDialog window. I want to then populate stuff, inside of this gui, from python, but I can't seem to be able to do this.我有一个 wxpython gui,它是我在 xrc 文件中定义的,这个 gui 的根项(idk,如果这是正确的术语)是一个 wxFrame,我将其加载到 wxDialog window 中。然后我想填充内容,在这个 gui,来自 python,但我似乎无法做到这一点。

As an example, lets say I have the following 2 files:例如,假设我有以下 2 个文件:

test.py:测试.py:

import wx
import wx.xrc

class GUI(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size(400,800), style = wx.TAB_TRAVERSAL)

        # Create an instance of the XmlResource class and load the resource file
        xml_resource = wx.xrc.XmlResource()
        xml_resource.Load('test.xrc')

        # Create a box sizer for the dialog
        outer_sizer = wx.BoxSizer(wx.VERTICAL)
        
        # Load the panel object from the resource file and add it to the outer sizer
        panel = xml_resource.LoadObject(self, 'panel', 'wxPanel')
        outer_sizer.Add(panel, 1, wx.EXPAND)

        panel2 = xml_resource.LoadObject(self, 'panel2', 'wxPanel')

        # Set the dialog's sizer
        self.SetSizer(outer_sizer)

# Create a wx.App instance and run the main dialog
app = wx.App()
dialog = GUI(None)
dialog.Show()
app.MainLoop()

test.xrc:测试.xrc:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<resource xmlns="http://www.wxwidgets.org/wxxrc" version="2.5.3.0">
    <object class="wxPanel" name="panel">
        <style>wxTAB_TRAVERSAL</style>
        <object class="wxBoxSizer">
            <orient>wxVERTICAL</orient>
            <object class="sizeritem">
                <option>1</option>
                <flag>wxEXPAND | wxALL</flag>
                <border>5</border>
                <object class="wxPanel" name="panel2">
                    <style>wxTAB_TRAVERSAL</style>
                    <bg>#4b6983</bg>
                </object>
            </object>
        </object>
    </object>
</resource>

Then panel will load correctly, but it will throw an error: (XRC error: XRC resource "panel2" (class "wxPanel") not found . So my question would be: how would one properly load a panel (or a scrollbox, or a sizer, etc) that has been defined in an xrc file into python, so that it can be populated? And is that even possible?然后面板将正确加载,但会抛出错误: (XRC error: XRC resource "panel2" (class "wxPanel") not found 。所以我的问题是:如何正确加载面板(或滚动框,或一个 sizer 等)已经在 xrc 文件中定义为 python,以便它可以被填充?这有可能吗?

Let's run with a second attempt, now you have defined a little more, of what you're trying to achieve.让我们进行第二次尝试,现在您已经对要实现的目标有了更多的定义。

the wx.xrc module allows you to request the name and/or id of an object within the xrc resource. wx.xrc模块允许您在 xrc 资源中请求 object 的名称和/或 ID。 So in your case, this could result in:所以在你的情况下,这可能会导致:

import wx
import wx.xrc

class GUI(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size(400,800), style = wx.TAB_TRAVERSAL)

        # Create an instance of the XmlResource class and load the resource file
        xml_resource = wx.xrc.XmlResource()
        xml_resource.Load('test.xrc')

        # Create a box sizer for the dialog
        outer_sizer = wx.BoxSizer(wx.VERTICAL)

        # Load the panel object from the resource file and add it to the outer sizer
        panel = xml_resource.LoadObject(self, 'panel', 'wxPanel')
        outer_sizer.Add(panel, 1, wx.EXPAND)

        self.panel2 = wx.xrc.XRCCTRL(self, "panel2")

        self.cb = wx.ComboBox(self.panel2, wx.ID_ANY, choices=[("Choice 1"), ("Choice 2")], style=wx.CB_READONLY)

        # Set the dialog's sizer
        self.SetSizer(outer_sizer)

# Create a wx.App instance and run the main dialog
app = wx.App()
dialog = GUI(None)
dialog.Show()
app.MainLoop()

Here the resource object defined in the xrc file as panel2 is defined within the code and then is available to be accessed as such.在这里,在 xrc 文件中定义为panel2的资源 object 在代码中定义,然后可以这样访问。

For the record, for the id use wx.xrc.XRCID("panel2")作为记录,对于id使用wx.xrc.XRCID("panel2")

In your example, this results as follows, if I add in a wx.ComboBox在您的示例中,如果我添加wx.ComboBox ,结果如下

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM