How do I get the wxpython combo box selection and change value?

247
July 08, 2017, at 4:56 PM
# -*- coding: utf-8 -*-
import wx

class Main(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, size=(430,550))
        self.mainPanel = wx.Panel(self, size=(0,500))
        self.data1 = [1,2,3]
        self.data2 = ['google','amazon']
        self.listCtrl = wx.ListCtrl(self.mainPanel, size=(0,0), style=wx.LC_REPORT|wx.BORDER_SUNKEN)
        self.listCtrl.InsertColumn(0, 'ONE', format=wx.LIST_FORMAT_CENTRE, width=wx.LIST_AUTOSIZE_USEHEADER)
        self.listCtrl.InsertColumn(1, 'TWO', format=wx.LIST_FORMAT_CENTRE, width=wx.LIST_AUTOSIZE)
        self.listCtrl.InsertColumn(2, 'THREE', format=wx.LIST_FORMAT_CENTRE, width=wx.LIST_AUTOSIZE)
        self.ComboBoxs = wx.ComboBox(self.mainPanel, choices=self.data2, style=wx.CB_READONLY)
        self.ComboBoxs.Bind(wx.EVT_COMBOBOX, self.ComboSelect, self.ComboBoxs)
        self.textLabel = wx.StaticText(self.mainPanel)
        self.autoRefreshCount = 0
        self.BoxSizer = wx.BoxSizer(wx.VERTICAL)
        self.BoxSizer.Add(self.ComboBoxs, 0, wx.ALL, 5)
        self.BoxSizer.Add(self.listCtrl, 1, wx.EXPAND | wx.ALL, 5)
        self.BoxSizer.Add(self.textLabel, 0, wx.EXPAND | wx.ALL, 5)
        self.mainPanel.SetSizer(self.BoxSizer)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.autoRefresh, self.timer)
        self.timer.Start(5000)
        self.ComboSelect(self)
    def ComboSelect(self, event):
        self.listCtrl.Append(self.data1)
    def autoRefresh(self, evnet):
        if self.ComboBoxs.GetStringSelection() in self.data2:
            self.ComboSelect(self)
            self.textLabel.SetLabel('count : ' + str(self.autoRefreshCount))
            self.autoRefreshCount += 1
        else:
            self.textLabel.SetLabel('count : ' + str(0))
            self.autoRefreshCount = 0
if __name__ == '__main__':
    app = wx.App()
    frame = Main()
    frame.Show(True)
    app.MainLoop()

I have created an automatic import after the combo box selection value.

If the problem changes the combo box selection, the changed value self.textLabel.SetLabel ('count:' + str (self.autoRefreshCount)) must be initialized.

I've tried a lot but I do not know how to do it.

if self.ComboBoxs.GetStringSelection () in self.data2: There seems to be a problem in the conditional expression.

Answer 1

It is not clear what you are trying to achieve in this code.
Your test if self.ComboBoxs.GetStringSelection() in self.data2: is always going to be True because self.ComboBoxs is read only and therefore cannot change, so whatever the selection is, it will always be in self.data2.
Try the following replacement and see if it gets you closer to what you want.

    def ComboSelect(self, event):
#        self.listCtrl.Append(self.data1)
        self.autoRefreshCount = 0
    def autoRefresh(self, evnet):
#        if self.ComboBoxs.GetStringSelection() in self.data2:
#            self.ComboSelect(self)
        self.listCtrl.Append(self.data1)
        self.textLabel.SetLabel('count : ' + str(self.autoRefreshCount))
        self.autoRefreshCount += 1
#        else:
#            self.textLabel.SetLabel('count : ' + str(0))
#            self.autoRefreshCount = 0
READ ALSO
Numpy addition to an element of an array

Numpy addition to an element of an array

I am coding a neural network in python, and need to adjust my weightsIn order to do so, I need to add my change variable to an element of my weights array

391
Euclidean distance between points in two different Numpy arrays, not within

Euclidean distance between points in two different Numpy arrays, not within

I have two arrays of x-y coordinates, and I would like to find the minimum Euclidean distance between each point in one array with all the points in the other arrayThe arrays are not necessarily the same size

1194
Pandas: How to check if a list-type column is in dataframe

Pandas: How to check if a list-type column is in dataframe

How can I create a new list column from a list column

550
Cosine similarity vs Cosine distance

Cosine similarity vs Cosine distance

I have a set of documents as given in the example below

365