2012-04-27 37 views
1

我在使用Python-gtk進行應用程序的繁重工作時遇到了一些問題。我一直在谷歌上搜索,發現這個鏈接:http://code.activestate.com/recipes/577919-splash-screen-gtk/。由於這個例子很清楚,我下載了該程序並在我的電腦上運行。但是,splashscreen並未顯示其內容。 難道是Gtk的某些配置參數設置錯了嗎?怎麼會添加代碼:初始屏幕沒有顯示它的內容

while gtk.events_pending(): 
    gtk.main_iteration() 

顯示啓動畫面後權利並不在我的情況下工作嗎?

感謝您的幫助

回答

2

我嘗試添加:在self.window.show_all()splashScreen

 while gtk.events_pending(): 
      gtk.main_iteration() 

權後,和工作。在此之前,它沒有顯示文字「這不應該花太長時間... :)」。

這是有效的,因爲它可以確保立即顯示啓動畫面,而不是讓它變成偶然的,我想必須隨機爲一些人工作(編寫此代碼的人和在此回覆的其他人),而不是爲他人(你和我)。

+0

這對我來說都不適用。事實上,在初始化splashscreen之後(在主函數中),已經有了「while gtk.events_pending ...」。我正在Gnome上工作,但在xfce桌面上試過它,它在那裏工作... – 2012-04-27 15:28:12

+0

我不是在討論主函數中的函數(因爲那個在我的計算機上似乎沒有做任何事情),我'正在討論在'splashScreen'中的'self.window.show_all()'之後添加一個。是你做的嗎?它仍然不起作用?如果是的話......很奇怪。 – dumbmatter 2012-04-27 17:11:24

+0

對不起,我一定是做錯了。現在它可以工作:-)。非常感謝 – 2012-05-02 09:25:50

0

我測試了你給出的鏈接中的代碼,它確實顯示了splashscreen的組件。但它可能似乎不顯示它們可能是因爲splashscreen窗口沒有給出大小?我說:

self.window.set_size_request(300,300)

示例代碼果然標籤確實出現。

+0

謝謝你的時間。不幸的是,這並沒有幫助。它顯示了一個沒有寫入任何內容的窗口。 – 2012-04-27 15:11:14

+0

「主窗口」是什麼,在啓動畫面之後顯示的那個?那個是否顯示其內容? – Wes 2012-04-27 16:44:29

0

我知道這是一個非常古老的問題,但我有與Gtk + 3相同的問題,並使用Gtk.events_pending()沒有效果,所以我採取了不同的路線。

基本上,我只是把一個按鈕手動清除啓動畫面,我見過很多商業應用程序做。然後我在創建主窗口後在啓動畫面上調用window.present()以保持啓動畫面在前面。這似乎只是暫停Gtk + 3需要在之前實際顯示啓動畫面內容,顯示其後面的主窗口。

class splashScreen(): 

    def __init__(self): 

     #I happen to be using Glade 
     self.gladefile = "VideoDatabase.glade" 
     self.builder = Gtk.Builder() 
     self.builder.add_from_file(self.gladefile) 
     self.builder.connect_signals(self) 
     self.window = self.builder.get_object("splashscreen") 

     #I give my splashscreen an "OK" button 
     #This is not an uncommon UI approach 
     self.button = self.builder.get_object("button") 

     #Button cannot be clicked at first 
     self.button.set_sensitive(False) 

     #Clicking the button will destroy the window 
     self.button.connect("clicked", self.clear) 
     self.window.show_all() 

     #Also tried putting this here to no avail 
     while Gtk.events_pending(): 
      Gtk.main_iteration() 

    def clear(self, widget): 
     self.window.destroy() 

class mainWindow(): 

    def __init__(self, splash): 
     self.splash = splash 

     #Tons of slow initialization steps 
     #That take several seconds 

     #Finally, make the splashscreen OK button clickable 
     #You could probably call splash.window.destroy() here too 
     self.splash.button.set_sensitive(True) 

if __name__ == "__main__": 

    splScr = splashScreen() 

    #Leaving this here, has no noticeable effect 
    while Gtk.events_pending(): 
     Gtk.main_iteration() 

    #I pass the splashscreen as an argument to my main window 
    main = mainWindow(splScr) 

    #And make the splashscreen present to make sure it is in front 
    #otherwise it gets hidden 
    splScr.window.present() 

    Gtk.main()