我一直在試圖弄清楚如何將數據與選中的複選框事件相關聯。問題是我的數據被困在一個5鍵深的字典裏(dict {dict {dict {dict {}}}}})。我已經嘗試爬過父母,並獲得他們的標籤,因爲他們是從鑰匙創建,但沒有多少運氣,因爲我如何嵌套筆記本電腦。該代碼看起來如下:wx.Checkbox - 使用複選框關聯數據
#!/usr/bin/env python
import os
import sys
import datetime
import numpy as np
from readmonifile import MonitorFile
from sortmonifile import sort
import wx
class NestedPanelOne(wx.Panel):
#----------------------------------------------------------------------
# First notebook that creates the tab to select the component number
#----------------------------------------------------------------------
def __init__(self, parent, label, data):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
#Loop creating the tabs according to the component name
nestedNotebook = wx.Notebook(self, wx.ID_ANY)
for slabel in sorted(data[label].keys()):
tab = NestedPanelTwo(nestedNotebook, label, slabel, data)
nestedNotebook.AddPage(tab,slabel)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(nestedNotebook, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
class NestedPanelTwo(wx.Panel):
#------------------------------------------------------------------------------
# Second notebook that creates the tab to select the main monitoring variables
#------------------------------------------------------------------------------
def __init__(self, parent, label, slabel, data):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
nestedNotebook = wx.Notebook(self, wx.ID_ANY)
for sslabel in sorted(data[label][slabel].keys()):
tab = NestedPanelThree(nestedNotebook, label, slabel, sslabel, data)
nestedNotebook.AddPage(tab, sslabel)
sizer.Add(nestedNotebook, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(sizer)
def GetPage(self):
return nestedNotebook.GetPage()
class NestedPanelThree(wx.Panel):
#-------------------------------------------------------------------------------
# Third notebook that creates checkboxes to select the monitoring sub-variables
#-------------------------------------------------------------------------------
def __init__(self, parent, label, slabel, sslabel, data):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.labels=[]
self.chbox=[]
if label not in chboxdict.keys():
chboxdict[label]={}
chboxvaldict[label]={}
if slabel not in chboxdict[label].keys():
chboxdict[label][slabel]={}
chboxvaldict[label][slabel]={}
if sslabel not in chboxdict[label][slabel].keys():
chboxdict[label][slabel][sslabel]={}
chboxvaldict[label][slabel][sslabel]={}
for ssslabel in sorted(data[label][slabel][sslabel].keys()):
self.cb=wx.CheckBox(self, -1, ssslabel)
chboxdict[label][slabel][sslabel][ssslabel]=self.cb
chboxvaldict[label][slabel][sslabel][ssslabel]=self.cb.GetValue()
wx.EVT_CHECKBOX(self, self.cb.GetId(), self.OnTimer)
self.chbox.append(self.cb)
self.labels.append(ssslabel)
gridSizer = wx.GridSizer(np.shape(list(set(self.labels)))[0],3, 5, 5)
gridSizer.AddMany(self.chbox)
self.SetSizer(gridSizer)
def OnTimer(self, event):
#print "OnTimer Update..."
# before we get data, do we have to bother?
isVisible = self.IsShown()
child = self
parent = self.GetParent()
while(parent!=None and not parent.IsTopLevel()):
if(parent.IsShown() and isinstance(parent, wx.BookCtrlBase)):
if(parent.IsShown()):
selectionPage = parent.GetPage(parent.GetSelection())
pageselected = parent.GetParent().GetPage()
print selectionPage, pageselected, 'test2'
if(selectionPage!=child):
isVisible=False
break
else:
if(not parent.IsShown()):
isVisible=False
break
child = parent
parent = parent.GetParent()
def ShowParents(self, event):
if self.cb.GetValue():
print label, slabel, sslabel, self.cb.GetLabel()
else:
print 'Please select'
########################################################################
class NestedNotebook(wx.Notebook):
#---------------------------------------------------------------------------------
# Main notebook creating tabs for the monitored components
#---------------------------------------------------------------------------------
def __init__(self, parent, data):
wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=
wx.BK_DEFAULT
)
for label in sorted(data.keys()):
tab = NestedPanelOne(self,label, data)
self.AddPage(tab, label)
########################################################################
class mainFrame(wx.Frame):
#----------------------------------------------------------------------
# Putting it all together
#----------------------------------------------------------------------
def __init__(self,data):
wx.Frame.__init__(self, None, wx.ID_ANY,
"pDAQ monitoring plotting tool",
size=(800,400)
)
panel = wx.Panel(self)
notebook = NestedNotebook(panel, data)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
panel.SetSizer(sizer)
self.Layout()
#Menu Bar to be added later
'''
menubar = wx.MenuBar()
file = wx.Menu()
file.Append(1, '&Quit', 'Exit Tool')
menubar.Append(file, '&File')
self.SetMenuBar(menubar)
self.Bind(wx.EVT_MENU, self.OnClose, id=1)
'''
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
if len(sys.argv) == 1:
raise SystemExit("Please specify a file to process")
for f in sys.argv[1:]:
data=sort.sorting(f)
print 'test'
chboxdict={}
chboxvaldict={}
app = wx.PySimpleApp()
frame = mainFrame(data)
app.MainLoop()
print 'testend'
的NestedPanelThree
類是我的數據複選框關聯,並試圖通過面板爬上。
有沒有辦法將複選框與數據相關聯,例如,如果複選框被選中,那麼用於創建它的鍵被檢索?或者有沒有辦法更新chboxvaldict[label][slabel][sslabel][ssslabel]
的值,而不必遍歷所有的密鑰?
多謝提前
解決方案:
class NestedPanelThree(wx.Panel):
###
# Third notebook that creates checkboxes to select the monitoring sub-variables
###
def __init__(self, parent, label, slabel, sslabel, data):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.chbox=[]
for ssslabel in sorted(data[label][slabel][sslabel].keys()):
self.cb=wx.CheckBox(self, -1, ssslabel)
chboxdict[str(self.cb.GetId())]=[label,slabel,sslabel,ssslabel]
wx.EVT_CHECKBOX(self, self.cb.GetId(), self.OnClick)
self.chbox.append(self.cb)
gs = wx.GridSizer(0,3)
gs.AddMany(self.chbox)
self.SetSizer(gs)
def OnClick(self, event):
if chboxdict[str(event.GetEventObject().GetId())] not in clicked:
clicked.append(chboxdict[str(event.GetEventObject().GetId())])
else:
clicked.remove(chboxdict[str(event.GetEventObject().GetId())])
不看你的代碼:你不能派生一個MyCheckBox類並添加一些關鍵屬性?您仍然需要一些數據結構來檢索關聯的CheckBox。 – any1
我很想盡量避免,但我想這似乎是唯一的選擇,任何人都可以拿出atm – madtowneast