gtk.MessageDialog + time.sleep()

Programmierung für GNOME und GTK+, GUI-Erstellung mit Glade.
Antworten
Zumpfi
User
Beiträge: 2
Registriert: Mittwoch 27. August 2008, 19:21

Hallo,
weiter unten seht ihr mein erstes Programm.

Mein Problem ist folgendes:
In einer Endlosschleife rechne ich über eine gewisse Zeit(10min) meine CPU Load aus und möchte sie mir dann ab einem gewissen Wert (z.B 50%) per Message Dialog ausgeben lassen(so eine Art Alarm).

Das ganze funktionert auch alles ohne Probleme bis auf den MessageDialog der sich erst komplett schließt wenn der Timer(time.sleep()) abgelaufen ist.

Nun meine Frage:
1. Wie kann ich es lösen, dass der Dialog zu geht und dann erst die Zeit läuft

Anbei mein Code der nicht optimal ist woran aber noch gearbeitet wird.

Code: Alles auswählen

#!/usr/bin/python
import gtk
import os
import time
import string

strCommand    = "/bin/grep \"^cpu\ \" /proc/stat | cut -b 6-"
intTime       = 10
fltCPUIdleAlt = 0
fltCPUSumAlt  = 0
messageLoad =""

while True:
    strCPUCommand = os.popen(strCommand).readline()
    fltCPUValue   = [float(s) for s in strCPUCommand.split()] 
    print fltCPUValue
    fltCPUSum     = fltCPUValue[0] + fltCPUValue[1] + fltCPUValue[2] + fltCPUValue[3]
    fltCPUIdle    = fltCPUValue[3]
    print fltCPUIdle
    print fltCPUSum

    if fltCPUIdleAlt >= 0:
        fltCPULoad = 100 -(100* (fltCPUIdle - fltCPUIdleAlt) / (fltCPUSum - fltCPUSumAlt))
        print fltCPULoad

        fltCPUIdleAlt = fltCPUIdle
        fltCPUSumAlt  = fltCPUSum

    
    messageLoad   = "CPU Last: " + str(int(fltCPULoad))
    dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION, 
                                gtk.BUTTONS_YES_NO, messageLoad)
    dialog.set_title("last")
    respons = dialog.run()

    if respons == gtk.RESPONSE_NO: 
        break 
    if respons == gtk.RESPONSE_YES: 
        dialog.destroy() 
        # hier schließt er nicht erst die MessageBox und macht dann weiter
        # sondern erst nach dem sleep fertig ist
    time.sleep(intTime)
PS: das ganze soll nachher ein System Monitor werden
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

``time.sleep`` durch ``gobject.timeout_add`` ersetzen, das alles in eine Funktion packen und generell in Zukunft OOP verwenden.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
Zumpfi
User
Beiträge: 2
Registriert: Mittwoch 27. August 2008, 19:21

Danke für deine schnelle Antwort.
Objektorientiert wird es noch das hatte ich auch vor nur war es gestern auf die schnelle so einfacher.

Nun aber zurück zum eigentlichen Problem:

Code: Alles auswählen

#!/usr/bin/python
import gtk
import os
import time
import string

strCommand    = "/bin/grep \"^cpu\ \" /proc/stat | cut -b 6-"
intTime       = 10
fltCPUIdleAlt = 0
fltCPUSumAlt  = 0
messageLoad =""

def timeout ():
    gobject.timeout_add(intTime, monitoring)
    
def monitoring():
    while True:
        strCPUCommand = os.popen(strCommand).readline()
        fltCPUValue   = [float(s) for s in strCPUCommand.split()]
        print fltCPUValue
        fltCPUSum     = fltCPUValue[0] + fltCPUValue[1] + fltCPUValue[2] + fltCPUValue[3]
        fltCPUIdle    = fltCPUValue[3]
        print fltCPUIdle
        print fltCPUSum

        if fltCPUIdleAlt >= 0:
            fltCPULoad = 100 -(100* (fltCPUIdle - fltCPUIdleAlt) / (fltCPUSum - fltCPUSumAlt))
            print fltCPULoad

            fltCPUIdleAlt = fltCPUIdle
            fltCPUSumAlt  = fltCPUSum
       
        messageLoad   = "CPU Last: " + str(int(fltCPULoad))
        dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION,
                                   gtk.BUTTONS_YES_NO, messageLoad)
        dialog.set_title("last")
        respons = dialog.run()

        if respons == gtk.RESPONSE_NO:
            break
        if respons == gtk.RESPONSE_YES:
            dialog.destroy()
            timeout()
monitoring()
Nun schließt er zwar sofort das Fenster wie er es soll, beendet aber auch sofort das Programm.
Irgendwas mache ich falsch, nur was habe ich noch nicht rausgefunden.

Danke für die Hilfe
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

Zumpfi hat geschrieben:Irgendwas mache ich falsch, nur was habe ich noch nicht rausgefunden.
Du hast keine Main-Loop, daher beendet sich die GUI.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
Antworten