我们有一个带有大量小部件3-4k的Python GTK应用程序。
当从GTK2移植到GTK3时,我们发现在以下情况下会有相当大的性能影响:
我的问题是:这是一个错误,还是在GTK3应用程序中使用数千个小部件是不合理的?
以下测试计划perf.py
显示了两个问题:
import sys
import gi
import time
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class DialogExample(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, "My Dialog", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
label = Gtk.Label("The snappiness of this dialog depends on the number of \nwidgets in the main program.")
self.get_content_area().add(label)
self.show_all()
class DialogWindow(Gtk.Window):
def __init__(self, count):
Gtk.Window.__init__(self, title="Dialog Example")
box = Gtk.Box()
self.add(box)
button = Gtk.Button("Open dialog")
button.connect("clicked", self.on_button_clicked)
box.pack_start(button, expand=False, fill=True, padding=0)
# Add dummy widgets to show effect
for n in xrange(count):
button = Gtk.Button(str(n))
button.set_no_show_all(True)
t = time.time()
box.pack_start(button, expand=False, fill=True, padding=0)
print('{}\t{:.3f}'.format(n, 1000*(time.time()-t)))
def on_button_clicked(self, widget):
dialog = DialogExample(self)
dialog.run()
dialog.destroy()
count = int(sys.argv[1])
win = DialogWindow(count)
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()