[英]How do I get the wxpython combo box selection and change value?
# -*- 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()
在组合框选择值之后,我创建了一个自动导入。
如果问题更改了组合框的选择,则必须初始化更改后的值self.textLabel.SetLabel ('count:' + str (self.autoRefreshCount))
。
我已经尝试了很多,但是我不知道该怎么做。
if self.ComboBoxs.GetStringSelection () in self.data2:
条件表达式中似乎存在问题。
目前尚不清楚您要在此代码中实现什么。
您的测试if self.ComboBoxs.GetStringSelection() in self.data2:
始终为True
因为self.ComboBoxs
是只读的,因此无法更改,因此无论选择什么,它始终位于self.data2
。
尝试以下替换,看看它是否使您更接近所需的内容。
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
编辑:
根据您的评论,我怀疑您想要的是EVT_TEXT
当组合框中的文本更改时,将触发此事件。
像这样绑定它,看看这是否是您想要的。
self.ComboBoxs.Bind(wx.EVT_TEXT, self.ComboChange, self.ComboBoxs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.