Reputation: 3
I am building a tool to generate product codes from a catalog for fun.
I am having a problem with the event handler it wants to call the wrong code constantly and I have no idea why.
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()
When I run the program and select my first dropdown box it prints the expected output, but when I select the following dropdown boxes it seems like it tries the first event handler over and over even though I have them defined differently. Has anyone ran into this problem before? How did you fix it?
Upvotes: 0
Views: 46
Reputation: 113978
def onCombo6(self, event):
"""
"""
self.housingTypeSelection = self.combo.GetValue() #<- this line is wrong
value6 = htldict[self.housingTypeSelection]
print value6
you are always getting the value of self.combo
(the first combo box you created) ... you need to get the value of self.comboN
that matches the handler ...
this could have been easily diagnosed by adding a simple print in a few of the handlers that was different (or running in debug mode and stepping through)
Upvotes: 1