我正在构建一个工具来从目录中生成产品代码以获得乐趣。
我遇到了它想要不断调用错误代码的事件处理程序的问题,我不知道为什么。
import wx
class MyToolMainScreen(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Contrinex Helper")
panel = wx.Panel(self, wx.ID_ANY)
##initialize the variables we need here.
self.sensorTypeSelection = ""
self.connectionSelection = ""
self.seriesSelection = ""
self.outputSelection = ""
self.housingSizeSelection = ""
self.housingTypeSelection = ""
self.outputTypeSelection = ""
global stldict
global cldict
global sldict
global oldict
global hsldict
global otldict
###Lists to hold our combobox list options
sensorTypeList = ["Conventional","High-temperature"]
connectionList = ["Cable","Connector","Cable w/ moulded connector"]
seriesList = ["500/520 (Extra-Distance)","600/620 (Classics)","700 (Full-inox)","Embeddable / Quasi-embeddable","Non-embeddable","Increased Operating Distance, (Quasi-)embeddable","Increased Operating Distance, Non-embeddable"]
outputList = ["NPN N.O.","NPN N.C.","PNP N.O.","PNP N.C."]
housingSizeList = ["Threaded M4","Threaded M5","Threaded M8","Threaded M12","Threaded M18","Threaded M30","Threaded M50","Smooth 0 3mm","Smooth 0 4mm","Smooth 0 6.5mm","Smooth 0 8mm","Smooth 5x5mm","Smooth 8x8mm","Smooth 40x40mm","Smooth 40x120mm","Smooth 60x80mm","Smooth 80x100mm"]
housingTypeList = ["Threaded cylindrical housing","Rectangular Housing","Smooth cylindrical housing"]
outputTypeList = ["2-wire DC - N.O./Namur","2-wire DC - N.C.","2-wire AC/DC - N.O.","2-wire N.C."]
###Dictionaries to hold the key:value pairs I will use to concatenate the string before it is placed in the spreadsheet
stldict = {"Conventional":"A","High-temperature":"H"}
cldict = {"Cable":"D","Connector":"S","Cable w/ moulded connector":"V"}
sldict = {"500/520 (Extra-Distance)":"5","600/620 (Classics)":"6","700 (Full-inox)":"7","Embeddable / Quasi-embeddable":"0","Non-embeddable":"1","Increased Operating Distance (Quasi-)embeddable":"2","Increased Operating Distance, Non-embeddable":"3"}
oldict = {"NPN N.O.":"1","NPN N.C.":"2","PNP N.O.":"3","PNP N.C.":"4"}
hsldict = {"Threaded M4":"4","Threaded M5":"5","Threaded M8":"8","Threaded M12":"12","Threaded M18":"18","Threaded M30":"30","Threaded M50":"50","Smooth 0 3mm":"3","Smooth 0 4mm":"4","Smooth 0 6.5mm":"65","Smooth 0 8mm":"80","Smooth 5x5mm":"5","Smooth 8x8mm":"8","Smooth 40x40mm":"44","Smooth 40x120mm":"40","Smooth 60x80mm":"60","Smooth 80x100mm":"80"}
htldict = {"Threaded cylindrical housing":"M","Rectangular Housing":"C","Smooth cylindrical housing":"O"}
otldict = {"2-wire DC - N.O./Namur":"5","2-wire DC - N.C.":"6","2-wire AC/DC - N.O.":"7","2-wire N.C.":"8"}
self.combo = wx.ComboBox(panel, choices=sensorTypeList)
self.combo2 = wx.ComboBox(panel, choices=connectionList)
self.combo3 = wx.ComboBox(panel, choices=seriesList)
self.combo4 = wx.ComboBox(panel, choices=outputList)
self.combo5 = wx.ComboBox(panel, choices=housingSizeList)
self.combo6 = wx.ComboBox(panel, choices=housingTypeList)
self.combo7 = wx.ComboBox(panel, choices=outputTypeList)
self.combo.Bind(wx.EVT_COMBOBOX, self.onCombo)
self.combo2.Bind(wx.EVT_COMBOBOX, self.onCombo2)
self.combo3.Bind(wx.EVT_COMBOBOX, self.onCombo3)
self.combo4.Bind(wx.EVT_COMBOBOX, self.onCombo4)
self.combo5.Bind(wx.EVT_COMBOBOX, self.onCombo5)
self.combo6.Bind(wx.EVT_COMBOBOX, self.onCombo6)
self.combo7.Bind(wx.EVT_COMBOBOX, self.onCombo7)
##Layout goes here... v0.1 v2 will have 2 columns and custom graphics all around
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.combo)
sizer.Add(self.combo2)
sizer.Add(self.combo3)
sizer.Add(self.combo4)
sizer.Add(self.combo5)
sizer.Add(self.combo6)
sizer.Add(self.combo7)
panel.SetSizer(sizer)
#----------------------------------------------------------------------
#Event handlers go here, what happens after selection
def onCombo(self, event):
"""
"""
self.censorTypeSelection = self.combo.GetValue()
value = stldict[self.censorTypeSelection]
### next step is to add this to a spreadsheet that will be at the bottom of the sizer. for now I'll just print and set the variable to concatenate at the end
print value
def onCombo2(self, event):
"""
"""
self.connectionSelection = self.combo.GetValue()
value2 = cldict[self.connectionSelection]
print value2
def onCombo3(self, event):
"""
"""
self.seriesSelection = self.combo.GetValue()
value3 = sldict[self.seriesSelection]
print value3
def onCombo4(self, event):
"""
"""
self.outputSelection = self.combo.GetValue()
value4 = oldict[self.outputSelection]
print value4
def onCombo5(self, event):
"""
"""
self.housingSizeSelection = self.combo.GetValue()
value5 = hsldict[self.housingSizeSelection]
print value5
def onCombo6(self, event):
"""
"""
self.housingTypeSelection = self.combo.GetValue()
value6 = htldict[self.housingTypeSelection]
print value6
def onCombo7(self, event):
"""
"""
self.outputTypeSelection = self.combo.GetValue()
value7 = otldict[self.outputTypeSelection]
print value7
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyToolMainScreen().Show()
app.MainLoop()
当我运行程序并选择我的第一个下拉框时,它打印出预期的输出,但是当我选择下面的下拉框时,它似乎反复尝试第一个事件处理程序,即使我对它们进行了不同的定义。有没有人遇到过这个问题?你是怎么解决的?
答案 0 :(得分:1)
def onCombo6(self, event):
"""
"""
self.housingTypeSelection = self.combo.GetValue() #<- this line is wrong
value6 = htldict[self.housingTypeSelection]
print value6
您总是获得self.combo
(您创建的第一个组合框)的值...您需要获得与处理程序匹配的self.comboN
的值...
这可以通过在一些不同的处理程序中添加简单的打印(或者在调试模式下运行并逐步执行)来轻松诊断出来