在选择框中选择项目时更新静态文本或textctrl *只读*

时间:2013-05-21 21:52:04

标签: python wxpython

基本上,我尝试做的是获取静态标签或只读textctrl以相应地改变它在选择小部件中选择的项目的价格。给出了同时创建的2个不同的列表,Item_ID和各个价格的列表共享相同的索引...无论我选择哪个项目,价格保持不变 Code above有静态文本方法...只读textctrl结果是相同的任何关于可以做什么的想法?

有问题的对话框的代码

class AddItemOrder ( wx.Dialog ):

    def __init__( self, parent,entryit ):
            wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"Add Item", pos = wx.DefaultPosition, size = wx.Size( 285,173 ), style = wx.DEFAULT_DIALOG_STYLE )
            self.entryit = entryit
            self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
            self.ite = i.Item()
            bSizer25 = wx.BoxSizer( wx.VERTICAL )

            gSizer9 = wx.GridSizer( 2, 2, 0, 0 )

            self.m_staticText34 = wx.StaticText( self, wx.ID_ANY, u"Item ID", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_staticText34.Wrap( -1 )
            gSizer9.Add( self.m_staticText34, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )

            self.m_choice2Choices = []
            self.create_choices(self.m_choice2Choices)
            self.Item_ID = wx.Choice( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, self.m_choice2Choices, 0 )
            self.Item_ID.SetSelection( 0 )
            self.Item_ID.Bind(EVT_CHOICE,self.get_index)
            gSizer9.Add( self.Item_ID, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER_HORIZONTAL, 5 )

            self.m_staticText17 = wx.StaticText( self, wx.ID_ANY, u"Item Price", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_staticText17.Wrap( -1 )
            gSizer9.Add( self.m_staticText17, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )

            self.Order_Price = wx.StaticText( self, wx.ID_ANY, str(self.prices[self.Item_ID.GetSelection() ]), wx.DefaultPosition, wx.DefaultSize, 0 )
            self.Order_Price.Wrap( -1 )
            self.Order_Price.SetLabel(str(self.prices[self.Item_ID.GetSelection() ]))
            gSizer9.Add( self.Order_Price, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )



            self.m_staticText35 = wx.StaticText( self, wx.ID_ANY, u"Item Qty", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_staticText35.Wrap( -1 )
            gSizer9.Add( self.m_staticText35, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )

            self.Item_Qty = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 120,-1 ), 0 )
            gSizer9.Add( self.Item_Qty, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )

            bSizer25.Add( gSizer9, 1, wx.EXPAND, 5 )

            self.m_button19 = wx.Button( self, wx.ID_ANY, u"Ok", wx.DefaultPosition, wx.DefaultSize, 0 )
            self.m_button19.Bind(wx.EVT_BUTTON, self.ok)

            bSizer25.Add( self.m_button19, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.EXPAND, 5 )

            self.SetSizer( bSizer25 )
            self.Layout()

            self.Centre( wx.BOTH )

创建选择和获取索引函数

    def create_choices(self,event):
        self.prices = []
        for entry in self.ite.entries:
            self.custom = entry.Item_ID
            self.customPr = entry.Item_Price
            self.prices.append(self.customPr)
            self.m_choice2Choices.append(self.custom)
        return self.m_choice2Choices

    def get_index(self,event):
        self.currIndex = self.Item_ID.GetSelection()
        return self.currIndex   

1 个答案:

答案 0 :(得分:0)

如果要更改statictext显示的文本(可能是在这种情况下是EVT_CHOICE处理程序),那么您只需要使用新文本调用statictext的SetLabel方法。所以,像这样:

newText = str(self.prices[self.currIndex])
self.m_staticText17.SetLabel(newText)