0

Here is the code I am working on:

import wx
import wx.aui
import wx.lib.scrolledpanel
from parse import *
import settings    

parser=parse()

ID_OPEN = 101
ID_SAVE = 102
ID_QUIT = 103

factors = ['Power','Fuel','Pot Water','Bottled Water','Storage Area','Personnel','Grey Water','Black Water','Solid Waste','Food','Material','Cost']

class TabPanel100(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
        sizer = wx.BoxSizer(wx.VERTICAL)

        chpanel1=wx.Panel(self,-1)    

        static1 = wx.StaticText(chpanel1,wx.NewId(), "Power:",pos=(10,10))
        self.text1 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[0]),size=(80,-1),pos=(140,10),style = wx.TE_PROCESS_ENTER,name = factors[0])
        static2 = wx.StaticText(chpanel1,wx.NewId(),"Fuel:",pos=(10,35))
        self.text2 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[1]),size=(80,-1),pos=(140,35),style = wx.TE_PROCESS_ENTER,name = factors[1])
        static3 = wx.StaticText(chpanel1,wx.NewId(),"Pot Water:",pos=(10,60))
        self.text3 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[2]),size=(80,-1),pos=(140,60),style = wx.TE_PROCESS_ENTER,name = factors[2])
        static4 = wx.StaticText(chpanel1,wx.NewId(),"Bottled Water:",pos=(10,85))
        self.text4 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[3]),size=(80,-1),pos=(140,85),style = wx.TE_PROCESS_ENTER,name = factors[3])
        static5 = wx.StaticText(chpanel1,wx.NewId(),"Storage Area:",pos=(10,110))
        self.text5 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[4]),size=(80,-1),pos=(140,110),style = wx.TE_PROCESS_ENTER,name = factors[4])
        static6 = wx.StaticText(chpanel1,wx.NewId(),"Personnel:",pos=(10,135))
        self.text6 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[5]),size=(80,-1),pos=(140,135),style = wx.TE_PROCESS_ENTER,name = factors[5])
        static7 = wx.StaticText(chpanel1,wx.NewId(),"Grey Water:",pos=(10,160))
        self.text7 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[6]),size=(80,-1),pos=(140,160),style = wx.TE_PROCESS_ENTER,name = factors[6])
        static8 = wx.StaticText(chpanel1,wx.NewId(),"Black Water:",pos=(10,185))
        self.text8 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[7]),size=(80,-1),pos=(140,185),style = wx.TE_PROCESS_ENTER,name = factors[7])
        static9 = wx.StaticText(chpanel1,wx.NewId(),"Solid Waste:",pos=(10,210))
        self.text9 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[8]),size=(80,-1),pos=(140,210),style = wx.TE_PROCESS_ENTER,name = factors[8])
        static10 = wx.StaticText(chpanel1,wx.NewId(),"Food:",pos=(10,235))
        self.text10 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.values[9]),size=(80,-1),pos=(140,235),style = wx.TE_PROCESS_ENTER,name = factors[9])
        static11 = wx.StaticText(chpanel1,wx.NewId(),"Materials:",pos=(10,260))
        self.text11 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.values[10]),size=(80,-1),pos=(140,260),style = wx.TE_PROCESS_ENTER,name = factors[10])
        static12 = wx.StaticText(chpanel1,wx.NewId(),"Cost($k):",pos=(10,285))
        self.text12 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.values[11]),size=(80,-1),pos=(140,285),style = wx.TE_PROCESS_ENTER,name = factors[11])
        inputs = [self.text1,self.text2,self.text3,self.text4,self.text5,self.text5,self.text6,self.text7,self.text8,self.text9,self.text10,self.text11,self.text12]

        for item in inputs:
            item.Bind(wx.EVT_TEXT_ENTER, self.UpdateEstValues)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(chpanel1)

        self.SetSizer(sizer)

    def UpdateEstValues(self,event):
        item = event.GetEventObject()
        if float(item.GetValue()) < 0:
            dlg = wx.MessageDialog(self, "Value cannot be negative.",'Error', wx.OK | wx.ICON_ERROR)
            dlg.ShowModal()
            dlg.Destroy()
            pass
        else:
            varName = item.GetName()

            if varName == factors[0]:
                parser.values[0] = float(item.GetValue())    

class TabPanel200(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
        sizer = wx.BoxSizer(wx.VERTICAL)

        chpanel1=wx.Panel(self,-1)    

        static1 = wx.StaticText(chpanel1,wx.NewId(),"Power:",pos=(10,10))
        text1 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[0]),size=(80,-1),pos=(140,10))
        static2 = wx.StaticText(chpanel1,wx.NewId(),"Fuel:",pos=(10,35))
        text2 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[1]),size=(80,-1),pos=(140,35))
        static3 = wx.StaticText(chpanel1,wx.NewId(),"Pot Water:",pos=(10,60))
        text3 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[2]),size=(80,-1),pos=(140,60))
        static4 = wx.StaticText(chpanel1,wx.NewId(),"Bottled Water:",pos=(10,85))
        text4 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[3]),size=(80,-1),pos=(140,85))
        static5 = wx.StaticText(chpanel1,wx.NewId(),"Storage Area:",pos=(10,110))
        text5 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[4]),size=(80,-1),pos=(140,110))
        static6 = wx.StaticText(chpanel1,wx.NewId(),"Personnel:",pos=(10,135))
        text6 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[5]),size=(80,-1),pos=(140,135))
        static7 = wx.StaticText(chpanel1,wx.NewId(),"Grey Water:",pos=(10,160))
        text7 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[6]),size=(80,-1),pos=(140,160))
        static8 = wx.StaticText(chpanel1,wx.NewId(),"Black Water:",pos=(10,185))
        text8 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[7]),size=(80,-1),pos=(140,185))
        static9 = wx.StaticText(chpanel1,wx.NewId(),"Solid Waste:",pos=(10,210))
        text9 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[8]),size=(80,-1),pos=(140,210))
        static10 = wx.StaticText(chpanel1,wx.NewId(),"Food:",pos=(10,235))
        text10 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[9]),size=(80,-1),pos=(140,235))
        static11 = wx.StaticText(chpanel1,wx.NewId(),"Materials:",pos=(10,260))
        text11 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[10]),size=(80,-1),pos=(140,260))
        static12 = wx.StaticText(chpanel1,wx.NewId(),"Cost($k):",pos=(10,285))
        text12 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[11]),size=(80,-1),pos=(140,285))    

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(chpanel1)

        self.SetSizer(sizer)    

class TabPanel300(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
        sizer = wx.BoxSizer(wx.VERTICAL)

        chpanel1=wx.Panel(self,-1)    

        static1 = wx.StaticText(chpanel1,wx.NewId(),"Power:",pos=(10,10))
        self.text1 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[0]),size=(80,-1),pos=(140,10))
        static2 = wx.StaticText(chpanel1,wx.NewId(),"Fuel:",pos=(10,35))
        text2 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[1]),size=(80,-1),pos=(140,35))
        static3 = wx.StaticText(chpanel1,wx.NewId(),"Pot Water:",pos=(10,60))
        text3 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[2]),size=(80,-1),pos=(140,60))
        static4 = wx.StaticText(chpanel1,wx.NewId(),"Bottled Water:",pos=(10,85))
        text4 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[3]),size=(80,-1),pos=(140,85))
        static5 = wx.StaticText(chpanel1,wx.NewId(),"Storage Area:",pos=(10,110))
        text5 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[4]),size=(80,-1),pos=(140,110))
        static6 = wx.StaticText(chpanel1,wx.NewId(),"Personnel:",pos=(10,135))
        text6 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[5]),size=(80,-1),pos=(140,135))
        static7 = wx.StaticText(chpanel1,wx.NewId(),"Grey Water:",pos=(10,160))
        text7 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[6]),size=(80,-1),pos=(140,160))
        static8 = wx.StaticText(chpanel1,wx.NewId(),"Black Water:",pos=(10,185))
        text8 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[7]),size=(80,-1),pos=(140,185))
        static9 = wx.StaticText(chpanel1,wx.NewId(),"Solid Waste:",pos=(10,210))
        text9 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[8]),size=(80,-1),pos=(140,210))
        static10 = wx.StaticText(chpanel1,wx.NewId(),"Food:",pos=(10,235))
        text10 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[9]),size=(80,-1),pos=(140,235))
        static11 = wx.StaticText(chpanel1,wx.NewId(),"Materials:",pos=(10,260))
        text11 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[10]),size=(80,-1),pos=(140,260))
        static12 = wx.StaticText(chpanel1,wx.NewId(),"Cost($k):",pos=(10,285))
        text12 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[11]),size=(80,-1),pos=(140,285))

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(chpanel1)

        self.SetSizer(sizer)

class TabPanel1(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

        sizer = wx.BoxSizer(wx.VERTICAL)    
        self.SetSizer(sizer)

class TabPanel2(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
        sizer = wx.BoxSizer(wx.VERTICAL)

        chpanel2=wx.Panel(self,-1)    

        static11 = wx.StaticText(chpanel2,wx.NewId(),"Power:",pos=(10,10))
        text11 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresources1),size=(80,-1),pos=(140,10))
        static21 = wx.StaticText(chpanel2,wx.NewId(),"Fuel:",pos=(10,35))
        text21 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces2),size=(80,-1),pos=(140,35))
        static31 = wx.StaticText(chpanel2,wx.NewId(),"Pot Water:",pos=(10,60))
        text31 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces3),size=(80,-1),pos=(140,60))
        static41 = wx.StaticText(chpanel2,wx.NewId(),"Bottled Water:",pos=(10,85))
        text41 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces4),size=(80,-1),pos=(140,85))
        static51 = wx.StaticText(chpanel2,wx.NewId(),"Storage Area:",pos=(10,110))
        text51 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces5),size=(80,-1),pos=(140,110))
        static61 = wx.StaticText(chpanel2,wx.NewId(),"Personnel:",pos=(10,135))
        text61 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces6),size=(80,-1),pos=(140,135))
        static71 = wx.StaticText(chpanel2,wx.NewId(),"Grey Water:",pos=(10,160))
        text71 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces7),size=(80,-1),pos=(140,160))
        static81 = wx.StaticText(chpanel2,wx.NewId(),"Black Water:",pos=(10,185))
        text81 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces8),size=(80,-1),pos=(140,185))
        static91 = wx.StaticText(chpanel2,wx.NewId(),"Solid Waste:",pos=(10,210))
        text91 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces9),size=(80,-1),pos=(140,210))
        static101 = wx.StaticText(chpanel2,wx.NewId(),"Food:",pos=(10,235))
        text101 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces10),size=(80,-1),pos=(140,235))
        static111 = wx.StaticText(chpanel2,wx.NewId(),"Materials:",pos=(10,260))
        text111 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces11),size=(80,-1),pos=(140,260))
        static121 = wx.StaticText(chpanel2,wx.NewId(),"Cost($k):",pos=(10,285))
        text121 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces12),size=(80,-1),pos=(140,285))

        sizer = wx.BoxSizer(wx.VERTICAL)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(chpanel2)

        self.SetSizer(sizer)    

class ChoicebookDemo1(wx.Choicebook):

    def __init__(self, parent):
        wx.Choicebook.__init__(self, parent, wx.ID_ANY)

           # Create the first tab and add it to the notebook
        tabOne = TabPanel1(self)
        tabOne.SetBackgroundColour("Gray")
        self.AddPage(tabOne, "Select facility")

           # Create and add the second tab
        tabTwo = TabPanel100(self)
        self.AddPage(tabTwo, "Electrical Distribution")

           # Create and add the third tab
        self.AddPage(TabPanel100(self), "Electrical Generation")
        self.AddPage(TabPanel100(self), "Natural gas Distribution")
        self.AddPage(TabPanel100(self), "Water distribution")


        self.Bind(wx.EVT_CHOICEBOOK_PAGE_CHANGED, self.OnPageChanged)

    def OnPageChanged(self, event):
         old = event.GetOldSelection()
         new = event.GetSelection()
         page=self.GetPageText(event.GetSelection())
        # sel = self.GetSelection()
         print(page)
         print 'OnPageChanged,  old:%d, new:%d\n' % (old, new)
         event.Skip()

class ChoicebookDemo2(wx.Choicebook):

    def __init__(self, parent):
        wx.Choicebook.__init__(self, parent, wx.ID_ANY)

           # Create the first tab and add it to the notebook
        tabOne = TabPanel1(self)
        tabOne.SetBackgroundColour("Gray")
        self.AddPage(tabOne, "Select facility")

           # Create and add the second tab
        tabTwo = TabPanel200(self)
        self.AddPage(tabTwo, "Values")

           # Create and add the third tab

        self.Bind(wx.EVT_CHOICEBOOK_PAGE_CHANGED, self.OnPageChanged)

    def OnPageChanged(self, event):
         old = event.GetOldSelection()
         new = event.GetSelection()
        # sel = self.GetSelection()
         print 'OnPageChanged,  old:%d, new:%d\n' % (old, new)
         event.Skip()    

class ChoicebookDemo3(wx.Choicebook):

    def __init__(self, parent):
        wx.Choicebook.__init__(self, parent, wx.ID_ANY)

           # Create the first tab and add it to the notebook
        tabOne = TabPanel1(self)
        tabOne.SetBackgroundColour("Gray")
        self.AddPage(tabOne, "Select facility")

           # Create and add the second tab
        tabTwo = TabPanel300(self)
        self.AddPage(tabTwo, "Electrical Distribution")

           # Create and add the third tab
        self.AddPage(TabPanel300(self), "Electrical Generation")
        self.AddPage(TabPanel300(self), "Natural gas Distribution")
        self.AddPage(TabPanel300(self), "Water distribution")

        self.Bind(wx.EVT_CHOICEBOOK_PAGE_CHANGED, self.OnPageChanged)

    def OnPageChanged(self, event):
         old = event.GetOldSelection()
         new = event.GetSelection()
        # sel = self.GetSelection()
         print 'OnPageChanged,  old:%d, new:%d\n' % (old, new)
         event.Skip()

class Panel(wx.Panel):
    def __init__(self,parent):

        wx.Panel.__init__(self,parent=parent)

        nb1=wx.aui.AuiNotebook(self)

        page1=[(TabPanel2(nb1),"Overall Resources"),(ChoicebookDemo1(nb1),"factors"),(ChoicebookDemo2(nb1),"values"),(ChoicebookDemo3(nb1),"totals")]

        for page1, label1 in page1:
            nb1.AddPage(page1,label1)

        sizer=wx.BoxSizer(wx.VERTICAL)
        sizer.Add(nb1,1,wx.EXPAND)
        self.SetSizer(sizer)

class MyFrame(wx.Frame):

    def __init__(self, parent, id=-1, title='wx.aui Test',
                 pos=wx.DefaultPosition, size=(1000, 500),
                 style=wx.DEFAULT_FRAME_STYLE):
        wx.Frame.__init__(self, parent, id, title, pos, size, style)

        menubar =wx.MenuBar()
        file = wx.Menu()
        edit = wx.Menu()
        help = wx.Menu()
        tools = wx.Menu()
        pr = wx.Menu()
        file.Append(ID_OPEN, 'Open...', 'Load a specific dataset')
        file.Append(ID_SAVE, 'Save As...', 'Save current dataset')
        file.Append(ID_QUIT, 'Quit', 'Quit application')    

        menubar.Append(file,'&File')
        menubar.Append(edit,'&Edit')
        menubar.Append(help,'&Help')
        menubar.Append(tools,'&Tools')
        menubar.Append(pr,'&PR')

        self.SetMenuBar(menubar)

        self._mgr = wx.aui.AuiManager(self)
        self.Bind(wx.EVT_CLOSE, self.OnClose)

        toppanel=wx.Panel(self)    
        text3 = Panel(self)

        sizer=wx.BoxSizer(wx.VERTICAL)
        sizer.Add(text3,0,wx.ALL,border=10)

        toppanel.SetSizer(sizer)    

        # add the panes to the manager
        self._mgr.AddPane(text3, wx.CENTER,'Restoration data')

        # tell the manager to 'commit' all the changes just made
        self._mgr.Update()

        self.Bind(wx.EVT_CLOSE, self.OnClose)    

    def OnQuit(self,event):
        self.Close()    

    def OnClose(self, event):
        # deinitialize the frame manager
        self._mgr.UnInit()
        # delete the frame
        self.Destroy()    

app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()

I'm a newbie started working on wxpython for a class project.

The mainframe has a auinotebook. Auinotebook is made of Choicebook panels. Each Choicebook panel consists of a panel of 12 textctrl variables.

If user changes a value in one Choicebook panel, I need to change the corresponding values in the other choicebook panels as well.

The values are mathematically related. I'm not able to update the values of other panels.

Any help regarding this would be great.

Thank you for taking your time to help me out.

ArK
  • 20,698
  • 67
  • 109
  • 136
  • Well, you must remember, that people are lazy. If you post such a big piece of code, nobody will read through it, and you are limiting your chance to get an answer. I guess if you posted just one notebook with 2 tabs, 2 text ctrls each, you would have had an answer days ago :-) – Petr Blahos Nov 02 '16 at 07:01

1 Answers1

1

You have correctly started with binding the EVT_TEXT_ENTER to UpdateEstValues. There you get the entered value, and the only thing you need is to transfer this value into the second tab, right? You also seem to be using the parser as a "model", that keeps the currently valid values right? So in UpdateEstValues you update the model (parser), which you probably can make work on your own, you only need to tell the second tab to update its values.

Structurally there are many ways how to do it, but it all goes down to some kind of firing an event that tells the second tab to update. So the most basic idea would be to have a function parser.fire and parser.add_listener. The second tab would have a function update

def update(self, parameter_name):
    self.text_edits[parameter_name].SetValue(parser.get_value("parameter_name"))

In your main frame after creating the tabs you would do:

parser.add_listener(second_tab.update)

and in your UpdateEstValues you would do:

parser.fire(parameter_name)

We are missing:

def add_listener(self, listener):
    self.listeners.append(listener)

def fire(self, parameter_name):
    for i in self.listeners:
        i(parameter_name)

which is of course a kind of a minimal demonstration of an approach. You can lookup a pubsub package, you can use wx events (user-defined) to notify the second tab on the change, but in principle they are all alike.

Petr Blahos
  • 2,253
  • 1
  • 11
  • 14