python gtk window.show()在_init之外__

python gtk window.show()在_init之外__,python,gtk,Python,Gtk,我正试图编写一个Gtk脚本来通知root中的用户,因为我在root中遇到了python notify的问题。 因此,我编写了以下代码: #!/usr/bin/env python # -*- coding: utf-8 -*- import pygtk pygtk.require('2.0') import gtk import gtk.gdk import time class Time: def auto(self, Time, donnees=None):

我正试图编写一个Gtk脚本来通知root中的用户,因为我在root中遇到了python notify的问题。 因此,我编写了以下代码:

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

import pygtk
pygtk.require('2.0')
import gtk
import gtk.gdk
import time

class Time:

    def auto(self, Time, donnees=None):

        print "Show the window"
        self.window.show()
        time.sleep(10)
        print "Hide the window"
        self.window.hide()

    def __init__(self):

        color = "#000"
        positionX = 1560
        positionY = 35

        # Création fenetre principale
        self.window = gtk.Window(gtk.WINDOW_POPUP)

        # Position de la fenetre principale
        self.window.move(positionX+100, positionY)
        self.window.set_default_size(250, 80)
        self.window.set_position(gtk.WIN_POS_NONE)
        self.window.set_position(gtk.WIN_POS_CENTER_ON_PARENT)

        # Couleur de la fenetre
        map = self.window.get_colormap()
        colour = map.alloc_color(color) 
        style = self.window.get_style().copy()
        style.bg[gtk.STATE_NORMAL] = colour
        self.window.set_style(style)   

        #self.window.show()

        self.auto(self, Time)

def main():
        gtk.main()
        return 0

if __name__ == "__main__":
        Time()
        main()
问题是我无法在需要时显示或隐藏窗口。当我调用self.autoself时,init窗口中的时间不会出现。 我必须使用不同的线程吗

谢谢

您需要将您的时间替换为:


让主循环在睡眠时运行。

另一个选项是使用gobject.timeout\u add10000,lambda:self.window.hide,这将允许主循环在没有0.1s打嗝的情况下运行。嵌套的主循环往往会吸引bug。请不要使用sleep和gtk.main_迭代。这只会导致应用程序交互性差。只需使用gobject.timeout\u add10000、self.window.hide就可以了,不需要lambda。
then = time.time()
while then + 10 > time.time():
    while gtk.events_pending():
        gtk.main_iteration()
    time.sleep(0.1)