1

在我的 Python-gtk 应用程序中进行一些繁重的工作时,我在显示启动画面时遇到了一些麻烦。我一直在谷歌上搜索并找到这个链接:http ://code.activestate.com/recipes/577919-splash-screen-gtk/ 。由于这个例子很清楚,我下载了程序并在我的电脑上运行它。但是,启动画面没有显示其内容。会不会是Gtk的一些配置参数设置错了?怎么加代码:

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

在我的情况下显示启动画面后立即不起作用?

谢谢你的帮助

4

3 回答 3

2

我尝试添加:

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

就在self.window.show_all()in之后splashScreen,并且有效。在此之前,它没有显示文本,“这不应该花太长时间...... :)”。

这是有效的,因为它确保了启动画面立即呈现,而不是让它碰运气,我想这必须随机为某些人(编写此代码的人和在此处回复的其他人)而不是其他人(你和我)。

于 2012-04-27T14:22:29.780 回答
0

我测试了您提供的链接中的代码,它确实显示了启动画面的组件。但它可能似乎没有为您显示它们,也许是因为启动画面窗口没有指定大小?我补充说:

self.window.set_size_request(300,300)

到示例代码,果然标签确实出现了。

于 2012-04-27T14:23:06.343 回答
0

我知道这是一个非常古老的问题,但我对 Gtk+3 也有同样的问题,并且使用 Gtk.events_pending() 没有效果,所以我采取了不同的路线。

Basically, I just put a button to manually clear the splash screen, which I've seen plenty of commercial apps do. Then I call window.present() on the splash screen after creating the main window to keep the splash screen in front. This seems to be just the pause Gtk+3 needs to actually show the splash screen content before showing the main window opening behind it.

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()
于 2015-01-22T08:47:50.863 回答