wxpython将多个wx.NumCtrl转换为数据结构,以便在matplotlib中进行绘图

时间:2013-07-26 10:43:14

标签: python matplotlib wxpython

我正在wxpython中编写我的第一个gui,我非常关注如何正确地将输入的数据从NumCtrl导入到数据结构中,matplotlib可以很容易地操作它来提供我想要的绘图。我的GUI模板如下所示

GUI template

基本上对于每个角度,我想输入(C)和没有(U)准直器的计数数量。当我按下绘图按钮时,我会从每个wx.NumCtrl中输入数字并绘制图形。目前,我们使用ggplot2中的R将计数输入电子表格和图表。但是,我想用一个更友好的GUI来替换它,该GUI使用在同一脚本中完成的绘图编写。下面给出了所需最终图的一个例子

enter image description here

以下是相关代码的片段:

#Set input boxes for results
numctrl_ccm90 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ucm90 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ccp90 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ucp90 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ccm75 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ucm75 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ccp75 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ucp75 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ccm60 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ucm60 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ccp60 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ucp60 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ccm45 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ucm45 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ccp45 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ucp45 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ccm30 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ucm30 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ccp30 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ucp30 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ccm15 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ucm15 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ccp15 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_ucp15 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_cc0 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_uc0 = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)
numctrl_fwhm = NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB)

fgs.AddMany([(angle),(count_c), (count_u),(angle2),(count_c2), (count_u2),
             (wx.StaticText(panel, -1, '-90')),
             (numctrl_ccm90,0,wx.EXPAND,5),
             (numctrl_ucm90,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, '90')),
             (numctrl_ccp90,0,wx.EXPAND,5),
             (numctrl_ucp90,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, '-75')),
             (numctrl_ccm75,0,wx.EXPAND,5),
             (numctrl_ucm75,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, '75')),
             (numctrl_ccp75,0,wx.EXPAND,5),
             (numctrl_ucp75,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, '-60')),
             (numctrl_ccm60,0,wx.EXPAND,5),
             (numctrl_ucm60,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, '60')),
             (numctrl_ccp60,0,wx.EXPAND,5),
             (numctrl_ucp60,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, '-45')),
             (numctrl_ccm45,0,wx.EXPAND,5),
             (numctrl_ucm45,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, '45')),
             (numctrl_ccp45,0,wx.EXPAND,5),
             (numctrl_ucp45,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, '-30')),
             (numctrl_ccm30,0,wx.EXPAND,5),
             (numctrl_ucm30,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, '30')),
             (numctrl_ccp30,0,wx.EXPAND,5),
             (numctrl_ucp30,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, '-15')),
             (numctrl_ccm15,0,wx.EXPAND,5),
             (numctrl_ucm15,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, '15')),
             (numctrl_ccp15,0,wx.EXPAND,5),
             (numctrl_ucp15,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, '0')),
             (numctrl_cc0,0,wx.EXPAND,5),
             (numctrl_uc0,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, '')),
             (wx.StaticText(panel, -1, '')),
             (wx.StaticText(panel, -1, '')),
             (wx.StaticText(panel, -1, '')),
             (wx.StaticText(panel, -1, '')),
             (wx.StaticText(panel, -1, '')),
             (wx.StaticText(panel, -1, 'FWHM')),
             (numctrl_fwhm,0,wx.EXPAND,5),
             (wx.StaticText(panel, -1, 'deg'))])

其中numctrl_ccm90numctrl_ucp90是对应于-90deg的准直计数和未校正计数+ 90deg等的小部件。

我假设我应该创建某种绑定,用每个NumCtrl小部件的计数更新变量/列表/字典。我不确定我应该怎么做,以何种格式最适合后续绘图。例如,当我在numctrl_ccm90中输入1234个计数时,我想将此信息存储为counts: 1234 collimator:Y Angle: -90.

当我输入数据时,我会按下绘图按钮,它会在每个numCtrl框中拾取计数值(除了fwhm numctrl)并将所有这些信息放在允许​​绘图的结构中

对于绘图 - 我绘制了准直和非准直探测器与角度的归一化计数。处理此任务的最佳方法是什么。我现在很迷茫


我现在在列表列表中有我的计数

self.angle_list = []
self.angles_list.append([-90,'U',self.numctrl_ucm90.GetValue()])
self.angles_list.append([-75,'U',self.numctrl_ucm75.GetValue()])

其中angle_list的每个元素都是格式为[angle,collimator,counts]

的列表

不确定这对我有什么帮助。我需要的是两个组合'U'与'C'的角​​度与计数的散点图。

1 个答案:

答案 0 :(得分:1)

我建议有3个控件列表,自动生成,然后你可以按顺序运行列表,一旦填充网格大小调整器并在每个图上单击运行每个再次拾取值放入两个列表中用于绘图的tupples或数组。 e.g。

self.Angles = range(-90, 100, 15)
self.Texts = [wx.StaticText(panel, -1, '%d' % a) for a in Angles]
self.CCount = [NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB) for a in Angles]
self.UCount = [NumCtrl(panel,-1,style=wx.TE_PROCESS_ENTER|wx.TE_PROCESS_TAB) for a in Angles]

添加到sizer并使用OnPlot方法:

   Cs = [ctrl.GetValue() for ctrl in self.CCount]
   Us = [ctrl.GetValue() for ctrl in self.UCount]
   plot(zip(Cs, Angles)) # or something similar

等...