473,513 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Listening to changes in a C++ variable from python

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?

Jul 19 '05 #1
3 1823
On Fri, 06 May 2005 19:56:34 -0700, lamthierry wrote:
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++.

Jul 19 '05 #2
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?
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++.


Jul 19 '05 #3
On Sat, 07 May 2005 07:16:58 -0700, lamthierry wrote:
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.
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2717
by: Christian von Essen | last post by:
Hi, As I don't know if my problem is python, platform or non-specific, I try to post my question here, as you may have made similar experiences. I try to write a simple chatserver, using the...
1
2409
by: Gerry Sutton | last post by:
Hi All! I have noticed a strange behavior when using a constant identifier to initialize an instance list variable in a base class and then trying to modifying the list in subclasses by using...
6
4275
by: John J. Hughes II | last post by:
My code starts a TCP/IP socket listener and waits for incoming connections. This works fine on my system and my test system but I have a customer who say it does not work on their system. As far...
16
1766
by: Richard | last post by:
Hi, I am passing a structure to a subroutine where the passed parameter has been declared as ByVal. However, changes made to the passed variable inside the subroutine flow through to the...
6
3094
by: kai | last post by:
Hi, I was tring to run an example (HelloWorld.aspx) from MSPrss book, I get this message: "ASP.NET Development Server faild to start listening port 1034. Error message: An attempt was made...
0
837
by: Neal Norwitz | last post by:
If you don't write or otherwise maintain Python Extension Modules written in C (or C++) or embed Python in your application, you can stop reading. Python 2.5 alpha 1 was released April 5, 2006. ...
0
1289
by: Daniel Walton | last post by:
I have been working on a problem for a full week now. If someone could please help me it would be great. Even a clue would be great at this point. What I am seeing is quite a mystery. I have...
10
5144
by: ThunderMusic | last post by:
Hi, I'm currently working with sockets. I accept connections using m_mySocket.Listen(BackLogCount); But when I want to stop listening, I shutdown all my clients and call m_mySocket.Close(), but it...
1
1996
by: petr.poupa | last post by:
Hello, is there anybody who has experience with listening on TCP port with python? I am looking for anything codes, tutorials,... . Thanks for any information
0
7264
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7534
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
459
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.