如何指定我的事件使用另一个处理程序wxPython

2024-04-29 20:34:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在建立一个工具,从一个有趣的目录生成产品代码

我有一个问题的事件处理程序,它想不断调用错误的代码,我不知道为什么

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()

当我运行程序并选择第一个下拉框时,它会打印预期的输出,但当我选择下面的下拉框时,它似乎会反复尝试第一个事件处理程序,尽管我对它们的定义不同。以前有人遇到过这个问题吗?你怎么修好的


Tags: theselfbinddefthreadedchoiceswxprint
1条回答
网友
1楼 · 发布于 2024-04-29 20:34:20
def onCombo6(self, event):
        """
        """
        self.housingTypeSelection = self.combo.GetValue() #<- this line is wrong
        value6 = htldict[self.housingTypeSelection]

        print value6

您总是得到self.combo(您创建的第一个组合框)的值。。。您需要获得与处理程序匹配的self.comboN

通过在几个不同的处理程序中添加一个简单的打印(或者在调试模式下运行并逐步执行),可以很容易地诊断出这一问题

相关问题 更多 >