How to use combo box selection to insert calculations in same dialog box?

151 views Asked by At

I have created a dialog box in wxPython which contains a combo box selection from drop-down list. Once I select a value from combo box then (upon pressing "Apply" tab) I want to use it to fill other fields in the same dialog box. Is there any similar example or any suggestion to accomplish it will be helpful?

Following is what I have:

import wx    

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Segment Generator', size=(580, 500))
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        panel = wx.Panel(self, -1)

        text1.SetForegroundColour((255,0,0)) # set text color
        SegList = ['1','2','3']
        self.Soil = wx.ComboBox(panel, -1, "", (115, 45), wx.DefaultSize, SegList, wx.CB_DROPDOWN)

        wx.StaticBox(panel, -1, "Create/Update Segment", (15, 90), size=(250,290))


        wx.StaticBox(panel, -1, "Quick Merge", (30, 120), size=(220,120))
        wx.StaticText(panel, -1, "Single Overland", (100, 150))
        cb1 = wx.CheckBox(panel, -1, '', pos=(70,150))
        wx.StaticText(panel, -1, "Single Swale", (100, 180))
        cb2 = wx.CheckBox(panel, -1, '', pos=(70,180))
        wx.StaticText(panel, -1, "Single Channel", (100, 210))
        cb3 = wx.CheckBox(panel, -1, '', pos=(70,210))

        wx.StaticBox(panel, -1, "Merge Specific Segment", (30, 250), size=(220,110))
        wx.StaticText(panel, -1, "Upstream Pixel #", (40, 280))
        self.uspixel = wx.TextCtrl(panel, -1, value="", pos=(170, 275), size=(65,25))
        wx.StaticText(panel, -1, "Downstream Pixel #", (40, 315))
        self.dspixel = wx.TextCtrl(panel, -1, value="", pos=(170, 310), size=(65,25))

        wx.StaticBox(panel, -1, "Method Statistics", (300, 80), size=(240,300))
        wx.StaticText(panel, -1, "Seg #", (320, 105))
        wx.StaticText(panel, -1, "Overall Tc (hrs):", (320, 140))
        self.sanumber = wx.TextCtrl(panel, -1, value="", pos=(455, 135), size=(75,25), style = wx.TE_READONLY) # overall Tc hrs
        wx.StaticText(panel, -1, "Overland Tc (hrs):", (320, 175))
        self.sanumber = wx.TextCtrl(panel, -1, value="", pos=(455, 170), size=(75,25), style = wx.TE_READONLY) # overland Tc hrs
        wx.StaticText(panel, -1, "Swale Tc (hrs):", (320, 210))
        self.sanumber = wx.TextCtrl(panel, -1, value="", pos=(455, 205), size=(75,25), style = wx.TE_READONLY) # swale Tc hrs
        wx.StaticText(panel, -1, "Channel Tc (hrs):", (320, 245))
        self.sanumber = wx.TextCtrl(panel, -1, value="", pos=(455, 240), size=(75,25), style = wx.TE_READONLY) # channel Tc hrs
        wx.StaticText(panel, -1, "# Overland Segments:", (320, 280))
        self.sanumber = wx.TextCtrl(panel, -1, value="", pos=(455, 275), size=(75,25), style = wx.TE_READONLY) # overland segments
        wx.StaticText(panel, -1, "# Swale Segments:", (320, 315))
        self.sanumber = wx.TextCtrl(panel, -1, value="", pos=(455, 310), size=(75,25), style = wx.TE_READONLY) # swale segments
        wx.StaticText(panel, -1, "# Channel Segments:", (320, 350))
        self.sanumber = wx.TextCtrl(panel, -1, value="", pos=(455, 345), size=(75,25), style = wx.TE_READONLY) # channel segments

        self.btnApply = wx.Button(panel, label="Recalculate Tc", pos=(360, 35))
        self.Bind(wx.EVT_BUTTON, self.OnSet, id=self.btnApply.GetId())

        self.btnApply = wx.Button(panel, label="Close Dialog", pos=(450, 400))
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=self.btnApply.GetId())

        self.btnApply = wx.Button(panel, label="Apply", pos=(100, 400))
        self.Bind(wx.EVT_BUTTON, self.OnApply, id=self.btnApply.GetId())

        self.Show(True)
        self.Centre(True)

    def OnSet(self, event):
        self.Show(True)

    def OnApply(self, event):
        # some calculations to fill fields created during initialization
        self.Show(True)

    def OnClose(self, event):
        self.Show(False)
1

There are 1 answers

0
Werner On

In your OnApply handler you check the content of your combobox and then set the values accordingly in your other controls, something along this:

def OnApply(self, event):
    soilValue = self.Soil.GetValue()
    if soilValue == 1:
        self.uspixel.SetValue('something')
        # set values for other controls
    elif soilValue == 2:
        # set values
    elif soilValue == 3:
        # set values
    else:
        # clear all values

BTW, why are you calling self.Show(True) multiple times? Doing it in the init method is enough.