2014-09-21 179 views
0

以外我想写一个Gtk脚本来通知用户(以root身份),因为我在root用户中遇到了python-notify问题。 所以,我写了这个代码:python gtk window.show()__init__

#!/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() 

问题是我不能显示或隐藏窗口时,我想。当我在init中调用self.auto(self,Time)时,窗口不会出现。 我必须使用不同的线程吗?

感谢

回答

0

你需要你的time.sleep(10)来取代:

then = time.time() 
while then + 10 > time.time(): 
    while gtk.events_pending(): 
     gtk.main_iteration() 
    time.sleep(0.1) 

让主循环运行,而你正在睡觉。

+1

另一种选择是使用'gobject.timeout_add(10000,lambda:self.window.hide())',这将允许主循环在没有0.1s打嗝的情况下运行。嵌套主循环倾向于吸引错误。 – user4815162342 2014-09-21 18:14:27

+0

请不要使用sleep()和gtk.main_iteration()。它只会导致糟糕的应用程序交互性。只需使用'gobject.timeout_add(10000,self.window.hide)'应该可以工作(不需要lambda)。 – 2014-09-23 00:25:20