1

How can I databind the value self.text in MyModel to the controls in the wx.Frame?

When the text control changes the text I would like the label to change aswell as the contents of the other text control automatically. (ideally without having to write any code if that's possible)

Note: this is a dummy example below to illustrate the general problem which may get more complicated as more fields are added to both the view and the model

import wx

class MyModel(object):
    def __init__(self):
        self.text = "hello"

class MyRegion(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title="My Region")
        self.model = MyModel()

        self.label = wx.StaticText(self, label=self.model.text)
        self.textbox = wx.TextCtrl(self, value=self.model.text)
        self.textbox2 = wx.TextCtrl(self, value=self.model.text)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.label, 0, wx.ALL, 5)
        sizer.Add(self.textbox, 0, wx.ALL, 5)
        sizer.Add(self.textbox2, 0, wx.ALL, 5)
        self.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App()
    frame = MyRegion(None)
    frame.Show()
    app.MainLoop()
  • Is there a data/model-centric approach to this as opposed to having to bind the views events to code so that that code update the model which then triggers some more code which updates the rest?
  • If there isn't then what mechanisms/recommended approaches are there which would be used to trigger updates to the view once the model has been updated?

1 Answer 1

2

Define a handler for wxEVT_UPDATE_UI event for the control whose text you'd like to change and call event.SetText() from it. This allows you to declaratively specify the text that the control should have, without bothering with updating it whenever something else changes -- instead it will be always up to date.

Sign up to request clarification or add additional context in comments.

2 Comments

What about the scenario where the data is updated in a background process by a timer? Is there a way to trigger an update for that?
Call wxWakeUpIdle() after an update to trigger the UI refresh (done from idle time processing).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.