GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Sep 19, 2014 9:49 am

All times are UTC




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Show variable real time in window
PostPosted: Mon Jul 14, 2014 8:02 pm 
Offline
Familiar Face

Joined: Sun Jul 13, 2014 10:17 pm
Posts: 9
Hi all,

I am a beginner in python and also in gtk. I am trying to do a little app that should display a value, real time in a simple window (label).
What i have done so far is :

Code:
import u3
from time import sleep
from datetime import datetime
import struct
import traceback
from gi.repository import Gtk

d = u3.U3()
d.getCalibrationData()
d.configIO(FIOAnalog = 255)


cmds = [u3.AIN(0, 30, 0, 0), u3.AIN(1, 30, 0, 0), u3.AIN(2, 30, 0, 0), u3.AIN(3, 30, 0, 0)]
ainBinary = d.getFeedback(cmds)

ainVoltage = [0]*4
for i in range(4):

    ainVoltage[i] = d.binaryToCalibratedAnalogVoltage(ainBinary[i], isLowVoltage = False, isSpecialSetting=True, channelNumber = i)

    #print "AIN%d voltage = %f V" % (i, ainVoltage[i])

    variabila0 = ainVoltage[0]
    variabila1 = ainVoltage[1]
    variabila2 = ainVoltage[2]
    variabila3 = ainVoltage[3]

class LabelWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Label Example")

        hbox = Gtk.Box(spacing=10)
        hbox.set_homogeneous(False)
        vbox_left = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
        vbox_left.set_homogeneous(False)
        vbox_right = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
        vbox_right.set_homogeneous(False)

        hbox.pack_start(vbox_left, True, True, 0)
        hbox.pack_start(vbox_right, True, True, 0)

        label = Gtk.Label(variabila0)
        vbox_left.pack_start(label, True, True, 0)
        label = Gtk.Label(variabila1)
        vbox_left.pack_start(label, True, True, 0)
        label = Gtk.Label(variabila2)
        vbox_left.pack_start(label, True, True, 0)
        label = Gtk.Label(variabila3)
        vbox_left.pack_start(label, True, True, 0)

        self.add(hbox)

window = LabelWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()


If i run this i see in window the 4 values but i see values from the moment i run script...i don t know how include that in a while routine to have real time values displayed on the window

Any help will be high appreciated

Thank you in advance


Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Tue Jul 15, 2014 3:54 pm 
Offline
Familiar Face

Joined: Mon Mar 31, 2014 9:59 pm
Posts: 16
Did you try a while True loop like here: http://www.lebsanft.org/?p=48

_________________
I am learning to develop GTK+ (GTK 3.10) programs written in Python 3.4.0, designed with Glade 3.16.1, with plots using Matplotlib 1.4. I program and test using Ubuntu 14.04 (64bit) and Gedit. (http://gtk3-matplotlib-cookbook.readthedocs.org)


Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Sat Jul 19, 2014 10:50 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
Hello,

How often do you need to update the display to the user?

The GLib library has a time-out function [url]http://lazka.github.io/pgi-docs/#GLib-2.0/functions.html#GLib.timeout_add[/url] where you can have a call-back set up to repeatedly call a function.

In this function you can then collect your data from your IO devices and then up date your labels.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Wed Aug 06, 2014 3:07 pm 
Offline
Familiar Face

Joined: Sun Jul 13, 2014 10:17 pm
Posts: 9
Maybe sounds stupid or i am stupid enough but i don t get it how should i use this while....

the frequency should be "real time" if this matters

thanks again


Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Sat Aug 09, 2014 6:15 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
Can you define what you mean by real-time? for you application. Real time is a very loose term.

I have an audio application which can play multiple audio channels and display information on the playing positions.

For the audio I might have a sample rate of 41100 samples per second. Each sample must be processed within approximately 24uS and sent to the sound card. If I was not able to keep up with this rate then I would have stutters in the audio as if the audio was repeatedly paused. I might also have the opposite where I could over flow the buffers and skip data going to the sound card and could have jumps in the audio. This is real-time.

When I am displaying the data to the user about playing position I am not too concerned about having the data shown as soon as it has changed. Most computer monitors only refresh the display at a rate of 60Hz or approximately every 16.7mS. In my application I have data the shows the time to the nearest 0.1 second. In this case I only update the time labels every 0.1 second and I am not to worried about any small delays in updating the display as the human brain is very flexible in interpreting this information. In this section of my application I did not need real-time, just fairly up to date information.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Sat Aug 09, 2014 9:18 am 
Offline
Familiar Face

Joined: Sun Jul 13, 2014 10:17 pm
Posts: 9
Hi errol,

Thanks for your reply. What you said is very correct. By real time i want say as quick as possible....your suggestion with 10 times per second should be perfect.

So i have to put in a loop what happen in the if statement and i will have variabila0,1,2,3 continuously updated. Should i declare those variables global somehow? I mean, beside the fact i need to show them in gtk window i have to do some decisions with them ad also do some calculations ...

So if are so kind to give me a little example of how to do that, i will really appreciate.

Thank you very much again


Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Sat Aug 09, 2014 10:28 pm 
Offline
Familiar Face

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 34
It is in C but you can give this a try.

Code:
//gcc -Wall `pkg-config --cflags gtk+-3.0` time_label.c -o time_label `pkg-config --libs gtk+-3.0`

#include<time.h>
#include<gtk/gtk.h>

static guint timer_id;

static gboolean get_time(gpointer data)
{
   time_t time1;
   struct tm* tm_info;
   char string[30];
   time(&time1);
   tm_info=localtime(&time1);
   strftime(string, 30, "%m-%d-%Y %I:%M:%S\n", tm_info);
   gtk_label_set_text(GTK_LABEL(data), string);
   return TRUE;
}
static void close_program()
{
   //remove timer.
   g_source_remove(timer_id);
   gtk_main_quit();
}
int main(int argc, char **argv)
{
   gtk_init(&argc, &argv);
   GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title(GTK_WINDOW(window), "Time Label");
   gtk_window_set_default_size(GTK_WINDOW(window), 200, 100);
   g_signal_connect_swapped(window, "destroy", G_CALLBACK(close_program), NULL);
   GtkWidget *label1=gtk_label_new("");
   gtk_widget_set_hexpand(label1, TRUE);
   gtk_container_add(GTK_CONTAINER(window), label1);
   gtk_widget_show_all(window);
   timer_id=g_timeout_add(1000/10, get_time, label1);
   gtk_main();
   return 0;
  }


Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Sun Aug 10, 2014 6:35 pm 
Offline
Familiar Face

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 34
Or in Python your can give this a try.

Code:
#!/user/bin/python

from gi.repository import Gtk
from gi.repository import GObject
import time

class TimeLabel(Gtk.Label):
    def __init__(self):
        Gtk.Label.__init__(self)
        GObject.timeout_add(1000/10, self.get_time)
   
    def get_time(self):
        localtime = time.asctime(time.localtime(time.time()))
        self.set_text(localtime)
        return True

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Time Label")
        self.set_default_size(200,100)
        label1 = TimeLabel()
        self.add(label1)

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()


Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Sun Aug 10, 2014 8:26 pm 
Offline
Familiar Face

Joined: Sun Jul 13, 2014 10:17 pm
Posts: 9
Thank you very much

The code worked but i added a second variable and a second label and i still see only first one. If I comment
Code:
label1 = ReadAIN0()
self.add(label1)


then i see the second one. But i cannot see both of them on the screen

My code is this

Code:

class ReadAIN0(Gtk.Label):
    def __init__(self):
        Gtk.Label.__init__(self)
        GObject.timeout_add(1000/10, self.get_ain0)

    def get_ain0(self):
        ainVoltage[0] = d.getAIN(0, negChannel = 32)
        ain0 = str(ainVoltage[0])
        self.set_text(ain0)
        return True

class ReadAIN1(Gtk.Label):
    def __init__(self):
        Gtk.Label.__init__(self)
        GObject.timeout_add(1000/10, self.get_ain1)

    def get_ain1(self):
        ainVoltage[1] = d.getAIN(1, negChannel = 32)
        ain1 = str(ainVoltage[1])
        self.set_text(ain1)
        return True

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="USV - BETA")
        self.set_default_size(300,200)
        label1 = ReadAIN0()
        self.add(label1)
        label2 = ReadAIN1()
        self.add(label2)

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()



Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Sun Aug 10, 2014 9:29 pm 
Offline
Familiar Face

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 34
Try putting in a GTK Grid into your main window and then add your labels to your grid. The main window only gets one container widget.


Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Sun Aug 10, 2014 9:58 pm 
Offline
Familiar Face

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 34
Something like this

...
Code:
Gtk.Window.__init__(self, title="Time Label")
        self.set_default_size(200,100)
        label1 = TimeLabel()
        label2 = TimeLabel()
        grid = Gtk.Grid()
        grid.attach(label1, 0, 0, 1, 1)
        grid.attach(label2, 0, 1, 1, 1)
        self.add(grid)

...

Then you can put all sorts of widgets into your application.


Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Tue Aug 12, 2014 1:26 pm 
Offline
Familiar Face

Joined: Sun Jul 13, 2014 10:17 pm
Posts: 9
Maybe i miss something

Code:
#!/user/bin/python

from gi.repository import Gtk
from gi.repository import GObject
import time
import u3
import sys
import struct
import traceback

d = u3.U3()
d.configU3()
d.getCalibrationData()
d.configIO(FIOAnalog = 255)
d.configIO(EIOAnalog=12)
d.configIO(NumberOfTimersEnabled = 2,TimerCounterPinOffset = 8)

t0Config = u3.TimerConfig(0, TimerMode = 2)
t1Config = u3.TimerConfig(1, TimerMode = 2)

d.getFeedback( t0Config, t1Config )

d.configTimerClock(TimerClockBase = 2, TimerClockDivisor = 48)

timer0 = u3.Timer(0)
timer1 = u3.Timer(1)

ainVoltage = [0]*8

class ReadAIN0(Gtk.Label):
    def __init__(self):
        Gtk.Label.__init__(self)
        GObject.timeout_add(1000/10, self.get_ain0)

    def get_ain0(self):
        ainVoltage[0] = d.getAIN(0, negChannel = 32)
        ain0 = str(ainVoltage[0])
        self.set_text(ain0)
        return True

class ReadAIN1(Gtk.Label):
    def __init__(self):
        Gtk.Label.__init__(self)
        GObject.timeout_add(1000/10, self.get_ain1)

    def get_ain1(self):
        ainVoltage[1] = d.getAIN(1, negChannel = 32)
        ain1 = str(ainVoltage[1])
        self.set_text(ain1)
        return True

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="USV - BETA")
        self.set_default_size(300,200)
        label1 = ReadAIN0()
        label2 = ReadAIN1()
        self.add(label2)
        grid = Gtk.Grid()
        grid.attach(label1, 0, 0, 1, 1)
        grid.attach(label2, 0, 1, 1, 1)
        self.add(grid)

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()


and i see only one value on window which surprising is label2


Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Tue Aug 12, 2014 3:41 pm 
Offline
Familiar Face

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 34
Comment out the self.add(label2) and see what happens.

The main window can only have one widget added to it. Because of this you want to add all your widgets to a container widget like a grid and then add the grid to the main window. It works pretty good but it may take a while to figure out how things are arranged in the grid container.


Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Thu Aug 28, 2014 10:37 am 
Offline
Familiar Face

Joined: Sun Jul 13, 2014 10:17 pm
Posts: 9
Hi again

I did a lot of progress here but again i have a problem

i have something like this

Code:
t0Config = u3.TimerConfig(0, TimerMode = 2)
t1Config = u3.TimerConfig(1, TimerMode = 2)

d.getFeedback( t0Config, t1Config )

d.configTimerClock(TimerClockBase = 2, TimerClockDivisor = 48)

timer0 = u3.Timer(0)
timer1 = u3.Timer(1)
timer = d.getFeedback(timer0, timer1)
ainVoltage = [0]*8

#results = d.getFeedback(timer0, timer1)

class Timer0(Gtk.Label):
    def __init__(self):
        Gtk.Label.__init__(self)
        GObject.timeout_add(1000/10, self.get_timer0)
    def get_timer0(self):
        timer[0] = d.getFeedback(timer0, timer1)
        #ainVoltage[0] = d.getAIN(0, negChannel = 32)
        global timer0
        timer0 = timer[0]
        timer0 = str(timer0)
        #ain0 = str(ain0)
        self.set_text(timer0)
        return True

class Timer1(Gtk.Label):
    def __init__(self):
        Gtk.Label.__init__(self)
        GObject.timeout_add(1000/10, self.get_timer1)
    def get_timer1(self):
        timer[1] = d.getFeedback(timer0, timer1)
        #ainVoltage[0] = d.getAIN(0, negChannel = 32)
        global timer1
        timer1 = timer[1]
        timer1 = str(timer1)
        #ain0 = str(ain0)
        self.set_text(timer1)
        return True


If i show in window only timer0 it show it but in [] - like [630222] - this is correct value

but if i show in another label timer1 it show only [] for both timer0 and timer1...

any idea what i am doing wrong?


Top
 Profile  
 
 Post subject: Re: Show variable real time in window
PostPosted: Thu Aug 28, 2014 4:13 pm 
Offline
Familiar Face

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 34
Not sure where your error is but you might want to try to create a class that can be reused. One of those object oriented things that works really well in Python. Then you can instantiate several objects from the same class that can all show slightly different output in different labels.

Code:
#!/user/bin/python

from gi.repository import Gtk
from gi.repository import GObject
import time

class TimeLabel(Gtk.Label):
    def __init__(self, milliseconds=1000):
        Gtk.Label.__init__(self)
        GObject.timeout_add(milliseconds, self.get_time)
   
    def get_time(self):
        localtime = time.asctime(time.localtime(time.time()))
        self.set_text(localtime)
        return True

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Time Label")
        self.set_default_size(200,100)
        label1 = TimeLabel(100)
        label2 = TimeLabel()
        label3 = TimeLabel(3000)
        grid = Gtk.Grid()
        grid.attach(label1, 0, 0, 1, 1)
        grid.attach(label2, 0, 1, 1, 1)
        grid.attach(label3, 0, 2, 1, 1)
        self.add(grid)

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group