在wxPython中操作ComboBox

时间:2017-06-28 03:05:55

标签: python user-interface combobox wxpython

我使用Python和wxPython创建一个UI,让用户在第一个组合框中选择一个XML文件,XML中的所有组件(即按钮)都显示为下面另一个组合框的选项。它清楚地正确阅读,因为它在我浏览所有XML时在控制台中打印出正确的内容,但我似乎无法将其链接回我正在寻找的组合框。

以下是代码:

import wx
import os
import xml.dom.minidom
from xml.dom.minidom import parse


# get all xmls
path = "C:\Users\William\Desktop\RES\Param"
files = os.listdir(path)

class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.xmlList = files
        self.xmlPickerTitle = wx.StaticText(self, label="XML Picker", pos=(20, 30))
        self.xmlPicker = wx.ComboBox(self, pos=(100, 30), size=(500, -1), choices=self.xmlList, style=wx.CB_DROPDOWN)

        self.elementsTitle = wx.StaticText(self, label="Elements Found", pos=(20, 100))
        # labels
        self.buttonsPickerTitle = wx.StaticText(self, pos=(20,120), label="Buttons")

        self.buttonList = []

        self.buttonsPicker = wx.ComboBox(self, pos=(100, 120), size=(250, -1), choices=buttonList, style=wx.CB_DROPDOWN)

        self.Bind(wx.EVT_COMBOBOX, self.XMLSelect,)


    def XMLSelect(self, event):
        xmlPicked = self.xmlList[event.GetSelection()]
        DOMTree = xml.dom.minidom.parse(xmlPicked)
        collection = DOMTree.documentElement

        buttons = DOMTree.getElementsByTagName("Button")

        for button in buttons:
            if button.hasAttribute("name"):
                buttonList.append(button.getAttribute("name"))
                print button.getAttribute("name")
app = wx.App(False)
frame = wx.Frame(None, title = "Auto", size = (800, 600))
panel = Panel(frame)
frame.Show()
app.MainLoop()

有什么想法吗?

提前致谢!

// 06/30 - 添加示例XML

以下是我尝试处理的XML文件的一个示例:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Window size="615,600" caption="0,0,0,40">
  <Include source="MainFrame_NewStyle.xml" />
  <HorizontalLayout>    
    <VerticalLayout width="1" bkimage="ProcessMgr_boder.png"/>
    <VerticalLayout bkimage="Control_bkg1.png">
        <VerticalLayout height="1" bkimage="ProcessMgr_boder.png"/>
        <HorizontalLayout height="60">
            <VerticalLayout>
                <Label name="lbl_tip" text="Attribute Edit" height="60" font="4" textpadding="0,0,0,0" align="center" textcolor="#001e1e1f" disabledtextcolor="#FFA7A6AA" />
            </VerticalLayout>

            <VerticalLayout width="46">                
                <Button name="btn_close" float="true" pos="0,10,0,0" width="36" height="36" textcolor="#004C5D6E" disabledtextcolor="#FFA0A0A0" 
                    tooltip="Close Notice Window"
                    normalimage="file=&apos;Control_FrameCloseBtn.png&apos; source=&apos;0,0,36,36&apos;" 
                    hotimage="file=&apos;Control_FrameCloseBtn.png&apos; source=&apos;0,36,36,72&apos;" 
                    pushedimage="file=&apos;Control_FrameCloseBtn.png&apos; source=&apos;0,72,36,108&apos;" />
            </VerticalLayout>
        </HorizontalLayout>
        <VerticalLayout height="1" bkimage="ProcessMgr_boder.png"/>
        <HorizontalLayout height="20" bkimage="Control_bkg2.png"/>   

        <HorizontalLayout height="450" bkimage="Control_bkg2.png">              
            <HorizontalLayout width="20" />  

            <HorizontalLayout width="1"  bkimage="Control_boder.png" />                                 
            <VerticalLayout width="200"  bkimage="Control_bkg2.png">
                <VerticalLayout height="1" width="200" bkimage="Control_boder.png" />
                    <VerticalLayout>
            <!--List name="pa_cmb_PropertySet" menu="true" header="hidden" style="Common_List_Style"-->
            <List name="pa_cmb_PropertySet" menu="true" style="Common_List_Style">  
              <ListHeader height="30" bkimage="Control_WorklistHeader_Nomal.png" menu="true">
                <ListHeaderItem name="title_Collimation Width" text="Collimation Width" minwidth="30" width="200" textcolor="#001e1e1f" sepwidth="1" align="center" hotimage="file=&apos;Control_WorklistHeader_HighLight.png&apos;" pushedimage="file=&apos;Control_WorklistHeader_Pushed.png&apos;" sepimage="file=&apos;Control_boder.png&apos;" />
              </ListHeader>
            </List>  
                    </VerticalLayout>                               
                <VerticalLayout height="1" width="200" bkimage="Control_boder.png" />
            </VerticalLayout>
            <HorizontalLayout width="1"  bkimage="Control_boder.png" />

            <HorizontalLayout width="20" /> 

            <HorizontalLayout >
                <HorizontalLayout width="1"  bkimage="Control_boder.png" />     
                <VerticalLayout width="350"  bkimage="Control_bkg2.png">
                    <VerticalLayout height="1"  bkimage="Control_boder.png" />
                    <VerticalLayout >
                        <List name="list_CollimationWidthSet" menu="true" style="Common_List_Style">
                            <ListHeader height="30" bkimage="Control_WorklistHeader_Nomal.png" menu="true">
                <ListHeaderItem name="title_status" text="Status" minwidth="30" width="60" textcolor="#001e1e1f" sepwidth="1" align="center" hotimage="file=&apos;Control_WorklistHeader_HighLight.png&apos;" pushedimage="file=&apos;Control_WorklistHeader_Pushed.png&apos;" sepimage="file=&apos;Control_boder.png&apos;" />
                    <ListHeaderItem name="title_name" text="Attribute Name" width="250" textcolor="#001e1e1f" sepwidth="1" align="center" hotimage="file=&apos;Control_WorklistHeader_HighLight.png&apos;" pushedimage="file=&apos;Control_WorklistHeader_Pushed.png&apos;" sepimage="file=&apos;Control_boder.png&apos;" />
                            </ListHeader>
                </List>
                    </VerticalLayout>                               
                    <VerticalLayout height="1"  bkimage="Control_boder.png" />
                </VerticalLayout>
                <HorizontalLayout width="1"  bkimage="Control_boder.png" />     
            </HorizontalLayout>
        </HorizontalLayout>

        <VerticalLayout bkimage="Control_bkg2.png">

            <Button name="btn_ok" text="OK" float="true" pos="480,20,0,0" width="94" height="35" style="Common_Button_222x63_Style" />
        </VerticalLayout>
    <VerticalLayout height="1" bkimage="ProcessMgr_boder.png"/>
    </VerticalLayout>
    <VerticalLayout width="1" bkimage="ProcessMgr_boder.png"/>
    </HorizontalLayout>
</Window>

它告诉用户界面在布局结构中使用不同的组件,如标签,按钮,列表等。

1 个答案:

答案 0 :(得分:0)

我遇到了文件名不包含路径的问题所以我不得不加入它们以传递到xmlPicked,但这可能是linux和Windows之间的区别。
关键点是Clear()Append()ComboBox 此外,Bind与特定ComboBox,因为您有2.
最后,设置ComboBox的选择,以便显示您有可用的数据。

import wx
import os
import xml.dom.minidom
from xml.dom.minidom import parse


# get all xmls
path = "/home/whatever"
files = os.listdir(path)

class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.xmlList = files
        self.xmlPickerTitle = wx.StaticText(self, label="XML Picker", pos=(20, 30))
        self.xmlPicker = wx.ComboBox(self, pos=(100, 30), size=(500, -1), choices=self.xmlList, style=wx.CB_DROPDOWN)

        self.elementsTitle = wx.StaticText(self, label="Elements Found", pos=(20, 100))
        # labels
        self.buttonsPickerTitle = wx.StaticText(self, pos=(20,120), label="Buttons")

        self.buttonList = []

        self.buttonsPicker = wx.ComboBox(self, pos=(100, 120), size=(250, -1), choices=self.buttonList, style=wx.CB_DROPDOWN)

        self.xmlPicker.Bind(wx.EVT_COMBOBOX, self.XMLSelect,)



    def XMLSelect(self, event):
        self.buttonsPicker.Clear()
        xmlPicked = self.xmlList[event.GetSelection()]
        xmlPicked = os.path.join(path,xmlPicked)
        DOMTree = xml.dom.minidom.parse(xmlPicked)
        collection = DOMTree.documentElement
        buttons = DOMTree.getElementsByTagName("Button")
        for button in buttons:
            if button.hasAttribute("name"):
                button_name = str(button.getAttribute("name"))
                self.buttonsPicker.Append(button_name)
                print button_name
        self.buttonsPicker.SetSelection(0)

app = wx.App(False)
frame = wx.Frame(None, title = "Auto", size = (800, 600))
panel = Panel(frame)
frame.Show()
app.MainLoop()