Clock in Python does not update

3

I'm trying to create a clock in PyGTK. But it looks like it gave some semantic error here:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import pygtk
pygtk.require('2.0')
import gtk
from datetime import *

def relogio():
    janela = gtk.Window()
    janela.set_title("Relógio digital")

    label = gtk.Label()
    janela.add(label)

    janela.connect("destroy", gtk.main_quit)
    janela.show_all()

    while 1:
        label.set_markup("<big>" + str(datetime.now().hour) + ":" + str(datetime.now().minute) + ":" + str(datetime.now().second) + "</big>")
if __name__ == '__main__':
    relogio()
    gtk.mainloop()

Could you help me fix it? Note that by running the code with the interpreter, the window simply does not appear!

    
asked by anonymous 23.10.2015 / 00:58

2 answers

2

I imagine that this code is blocking everything in an endless loop and no loop for nothing else to execute:

while 1:
    label.set_markup("<big>" + str(datetime.now().hour) + ":" + str(datetime.now().minute) + ":" + str(datetime.now().second) + "</big>")

I would move this second line to another method and would use it would create a timer to call it repeatedly. I also converted to a class to get more encapsulated:

import pygtk
pygtk.require('2.0')
import gtk
from datetime import *

class Relogio():
    def __init__(self):
        janela = gtk.Window()
        janela.set_title("Relógio digital")

        self.label = gtk.Label()
        janela.add(self.label)

        janela.connect("destroy", gtk.main_quit)
        janela.show_all()
        gtk.timeout_add(1000, self.atualiza)

    def atualiza(self):
        self.label.set_markup(datetime.now().strftime('<big>%H:%M:%S</big>'))
        return True

if __name__ == '__main__':
    relogio = Relogio()
    gtk.mainloop()
    
23.10.2015 / 05:42
1

You have to give GTK a chance to redraw the updates in label and also create a thread to get the current time.

Here's how your code looks:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import threading
import logging
import pygtk
pygtk.require('2.0')
import gtk
from datetime import *

class Relogio():
    def __init__(self):
        janela = gtk.Window()
        janela.set_title("Relógio digital")

        self.label = gtk.Label()
        janela.add(self.label)

        janela.connect("destroy", gtk.main_quit)
        janela.show_all()
        gtk.timeout_add(1, self.atualiza) #Mude o timeout para 1

    def atualiza(self):            
        while gtk.events_pending(): #Verifica os eventos pedentes.
            gtk.main_iteration() #Aqui esta fazendo as interações.
            self.label.set_text(datetime.now().strftime('%H:%M:%S')) #Defini para a metodo set_text.        

if __name__ == '__main__':                
    logging.basicConfig(level=logging.DEBUG, format='[%(levelname)s] (%(threadName)-10s) %(message)s',)
    relogio = Relogio()
    t1 = threading.Thread(name='Hora', target=relogio.atualiza())
    t1.start()
    gtk.main()

Sources: Working with thread . Getting interactions to update the label .

    
24.10.2015 / 15:52