0

I would like to separate my model (property) from the view (gtk widget).

For example: my model has a "level" property. I would like to bind the "level" to GtkScrollbar and to a GtkLabel so that a change to the variable e.g. self.level=5 will be reflected in the widgets and a change in the widgets will set the property.

This is common practice in many frameworks (WPF).

How to do this with python+gtk ? or is there a library for doing this binding?

1
  • Please provide some minimal mock code to illustrate what you want. Commented Oct 31, 2018 at 19:22

1 Answer 1

1

Official documentation provides exhaustive answer on bindings.

What you need exactly is bind_property_full method with custom function for transformation from GtkScrollbar's value (which is a number) to GtkLabel's string.

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

3 Comments

@user1656671 Not really. There is another approach, which involves more code. As a GtkRange's subclass GtkScrollbar emits "value-changed" signal when user moves the slider. You can connect to this signal and update your label value. Moreover, you can use the power of property to update both scrollbar and label when somebody updates self.level. However, you must understand, that GTK functions should only be called from main loop.
Damn, looks like I should've read the question more careful. Well, if your model knows about Label and Scrollbar, just update your setter and connect to said signal.
The idea is that my model would not know abut the controls, or at least would not have to know.. The best would be to define a model in terms of data types and bind as simply as possible. For example binding by looking up keys in glade files - as can be done with events.

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.