2017-07-08 187 views
0
# -*- 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() 

我在組合框選擇值後創建了一個自動導入。如何獲取wxpython組合框選擇和更改值?

如果問題更改組合框選擇,則必須初始化更改的值self.textLabel.SetLabel ('count:' + str (self.autoRefreshCount))

我試了很多,但我不知道該怎麼做。

if self.ComboBoxs.GetStringSelection() in self.data2:在條件表達式中似乎存在問題。

回答

1

目前還不清楚你在這段代碼中試圖達到什麼目的。
您的測試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) 
+0

再次感謝您。我再次從你身上學到了很多東西。我認爲當組合框改變時有一箇中間值發生了變化,但它不是。祝你有美好的一天:) –

+0

查看我對「EVT_TEXT」的編輯 –