Listening to changes in a C++ variable from python

L

lamthierry

Let's say I have the following source code in C++:

// The following is in a .cpp file

int val = 0;
for ( int i = 0; i < 10; i++ )
val = i;


// Now I'm in a python GUI, glade or GTK
Is it possible from the GUI side to listen to changes in the val
variable? Once I notice a change in the variable, I will update a
widget(for example a display progress bar from glade). Any advice or
solutions?
 
J

Jeremy Bowers

Let's say I have the following source code in C++:

// The following is in a .cpp file

int val = 0;
for ( int i = 0; i < 10; i++ )
val = i;


// Now I'm in a python GUI, glade or GTK
Is it possible from the GUI side to listen to changes in the val
variable? Once I notice a change in the variable, I will update a
widget(for example a display progress bar from glade). Any advice or
solutions?

It is not possible to "listen" to something that is not "emitting" change
notifications. Your variable "val" isn't "talking".

Some languages let you listen to every variable, but that is because
behind the scenes it is emitting every variable change as an event.

In C++, in keeping with its general philosophy, it is not going to be
possible to listen to an "int". You must either poll it, or wrap that int
in something that *will* emit change messages in some fashion that
satisfies you. Given the circumstance of updating a progress bar, I'd poll
it about every quarter to half second.

The correct answer changes depending on what you are doing with the info
and the level of control you exert over the source C++.
 
L

lamthierry

Is there some python method which can do the polling you are talking
about? Or can you send me a link to some existing example?
 
J

Jeremy Bowers

Is there some python method which can do the polling you are talking
about? Or can you send me a link to some existing example?

Take a moment to go back to the basics. C++ is, in general, a simple
system. The *language* is complicated at times, but it does actually
strive for no unknown magic taking place. When you say "int i = 4", there
is some literal value in memory somewhere taking the value of 4. If you
say "i = 5", that value in memory now says five.

The reason you can't "listen" to this is quite literally because you RAM
does not have that ability (for good reason); there are no circuits that
are triggered when a value is changed. (I'm talking conventional RAM here,
with no hardware mapping or anything else special.)

Polling here simply means checking periodically. You don't need any
special functions or libraries, you just need to check periodically. Your
GUI system should have a system for creating timeouts, but without telling
me what that is, I can't give the code. (*I* may not be able to even so; I
have not used them all extensively enough to know about all the
schedulers.) Your timeout function should simply check the new value and
do the appropriate thing based on the new value.

So, there isn't really a "method", it's just combining your GUI scheduler
with a simple == or > or < or whatever else is appropriate in your case.

If it takes more than about four or five lines to write the basic poller,
you're probably on the wrong track. (Correctly handling what the poller
sees may be more complicated, but the periodic checking should be simple.)

If you need more help (though I strongly suggest that you try to get the
scheduler to do something "two seconds from now" or something, and after
that it *should* be obvious what to do next), you'll need to include what
GUI toolkit you are using, and possibly the current Python code that you
are using to access the value you'd like the GUI to track. You may also
want to explain how you are connecting the apps; is the C++ part and the
Python part in one app in different threads, or are you using some other
communication form?

One warning: You might be tempted to use time.sleep(); don't do that. That
works OK in a Python program where only your code is running, but in any
GUI program the GUI itself is also running. Using time.sleep() will
completely stop *everything* during the duration of the sleep, completely
freezing the GUI as if it were locked up; that's why all GUIs have
schedulers, so they can keep running while your code waits.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top