【发布时间】:2017-10-20 20:09:39
【问题描述】:
我的一个应用程序需要一段时间来收集它需要的所有数据并显示窗口。因此,我决定创建一个简单的闪屏来通知用户发生了一些事情。不幸的是,启动窗口没有完全绘制:它显示一个黑色矩形,完成后消失。
我使用 this 示例代码 (python 2.7) 作为基础。
这是我的(简化代码):
#! /usr/bin/env python3
# Make sure the right Gtk version is loaded
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from time import sleep
class Splash(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
# Set position and decoration
self.set_position(Gtk.WindowPosition.CENTER)
self.set_decorated(False)
# Add box and label
self.box = Gtk.Box()
self.add(self.box)
self.lbl = Gtk.Label()
self.lbl.set_label("My app is loading...")
self.box.pack_start(self.lbl, True, True, 0)
# Show the splash screen without causing startup notification
# https://developer.gnome.org/gtk3/stable/GtkWindow.html#gtk-window-set-auto-startup-notification
self.set_auto_startup_notification(False)
self.show_all()
self.set_auto_startup_notification(True)
# Ensure the splash is completely drawn before moving on
while Gtk.events_pending():
Gtk.main_iteration()
if __name__ == '__main__':
# Initiate and show the splash screen
splash = Splash()
# Simulate the start of my app which takes a while
sleep(5)
# Destroy the splash window
splash.destroy()
我什至尝试使用 GObject.timeout_add 来线程显示函数(包含“显示初始屏幕”注释中的代码),但这并没有解决问题。
我在看什么?
【问题讨论】:
-
你初始化Gtk并启动主循环了吗?
-
我做到了(据我所知)。我添加的最后一个答案是接受的答案。也许你可以看看它。
标签: python-3.x gtk splash-screen