"Allocating size to..." 在 Gtk.ScrolledWindow 中使用 Gtk.TreeView 时出现 GTK 警告
"Allocating size to..." GTK Warning when using Gtk.TreeView inside Gtk.ScrolledWindow
我在我的 GTK 3 应用程序中收到以下警告:
Gtk-WARNING **: Allocating size to __main__+MCVEWindow 0000000004e93b30
without calling gtk_widget_get_preferred_width/height(). How does the code know the size to allocate?
当包含 Gtk.TreeView
的 Gtk.ScrolledWindow
附加到网格,网格本身附加到 gtk.ApplicationWindow
并且有足够的元素使滚动条实际出现时,会出现警告。如果没有足够的元素使其可滚动,则不会出现警告。
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk as gtk
class MCVEWindow(gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._tree_view = gtk.TreeView()
self._tree_view.set_hexpand(True)
self._tree_view.set_vexpand(True)
self.populate_tree_view() # populate tree view with fake items
window_column = gtk.TreeViewColumn(
"Window", gtk.CellRendererText(),
text=0
)
window_column.set_resizable(True)
handle_column = gtk.TreeViewColumn(
"Handle", gtk.CellRendererText(),
text=1
)
class_column = gtk.TreeViewColumn(
"Class name", gtk.CellRendererText(),
text=2
)
self._tree_view.append_column(window_column)
self._tree_view.append_column(handle_column)
self._tree_view.append_column(class_column)
scrolled_tree_view = gtk.ScrolledWindow()
scrolled_tree_view.add(self._tree_view)
toolbar = gtk.Toolbar()
expand_tree_view_button = gtk.ToolButton(icon_name="list-add")
expand_tree_view_button.connect(
"clicked",
lambda e: self._tree_view.expand_all()
)
collapse_tree_view_button = gtk.ToolButton(icon_name="list-remove")
collapse_tree_view_button.connect(
"clicked",
lambda e: self._tree_view.collapse_all()
)
toolbar.insert(expand_tree_view_button, -1)
toolbar.insert(collapse_tree_view_button, -1)
status_bar = gtk.Statusbar()
status_bar.push(
status_bar.get_context_id("Status message"),
"A status message."
)
self._master_grid = gtk.Grid()
self._master_grid.attach(toolbar, 0, 0, 1, 1)
self._master_grid.attach(scrolled_tree_view, 0, 1, 1, 1)
self._master_grid.attach(status_bar, 0, 2, 1, 1)
self.add(self._master_grid)
self.connect("delete-event", gtk.main_quit)
self.show_all()
def populate_tree_view(self):
tree_store = gtk.TreeStore(str, str, str)
# Warnings don't occur when there are less than 100 "root" items
for i in range(100):
item1 = tree_store.append(
None,
["Window " + str(i + 1), "12345678", "ClassName"]
)
for j in range(3):
item2 = tree_store.append(
item1,
["Window " + str(i + 1) + str(i + 2),
"12345678",
"ClassName"]
)
for k in range(5):
tree_store.append(
item2,
["Window " + str(i + 1) + str(j + 1) + str(k + 1),
"12345678",
"ClassName"]
)
self._tree_view.set_model(tree_store)
class MCVEApp(gtk.Application):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def do_activate(self):
MCVEWindow()
gtk.main()
if __name__ == "__main__":
MCVEApp().run()
如果您设置了环境,您应该能够复制、粘贴和运行此代码。
警告不遵循任何特定模式,有时有一个警告,有时有两个或更多。每当我展开所有树项目时,警告也会弹出。
GTK 版本为 3.22.18
什么可能导致这些警告?
我在 GTK App 开发邮件列表上收到了答案,这让我找到了解决方案:
将 TreeView 附加到 GTK 网格,然后将其添加到 ScrolledWindow 解决了我的问题。
而不是这个
scrolled_tree_view = gtk.ScrolledWindow()
scrolled_tree_view.add(self._tree_view)
您需要执行以下操作
scrolled_tree_view = gtk.ScrolledWindow()
grid = gtk.Grid()
grid.attach(self._tree_view, 0, 0, 1, 1)
scrolled_tree_view.add(grid)
不幸的是,这在任何地方都没有记录。
我在我的 GTK 3 应用程序中收到以下警告:
Gtk-WARNING **: Allocating size to
__main__+MCVEWindow 0000000004e93b30
without calling gtk_widget_get_preferred_width/height(). How does the code know the size to allocate?
当包含 Gtk.TreeView
的 Gtk.ScrolledWindow
附加到网格,网格本身附加到 gtk.ApplicationWindow
并且有足够的元素使滚动条实际出现时,会出现警告。如果没有足够的元素使其可滚动,则不会出现警告。
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk as gtk
class MCVEWindow(gtk.ApplicationWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._tree_view = gtk.TreeView()
self._tree_view.set_hexpand(True)
self._tree_view.set_vexpand(True)
self.populate_tree_view() # populate tree view with fake items
window_column = gtk.TreeViewColumn(
"Window", gtk.CellRendererText(),
text=0
)
window_column.set_resizable(True)
handle_column = gtk.TreeViewColumn(
"Handle", gtk.CellRendererText(),
text=1
)
class_column = gtk.TreeViewColumn(
"Class name", gtk.CellRendererText(),
text=2
)
self._tree_view.append_column(window_column)
self._tree_view.append_column(handle_column)
self._tree_view.append_column(class_column)
scrolled_tree_view = gtk.ScrolledWindow()
scrolled_tree_view.add(self._tree_view)
toolbar = gtk.Toolbar()
expand_tree_view_button = gtk.ToolButton(icon_name="list-add")
expand_tree_view_button.connect(
"clicked",
lambda e: self._tree_view.expand_all()
)
collapse_tree_view_button = gtk.ToolButton(icon_name="list-remove")
collapse_tree_view_button.connect(
"clicked",
lambda e: self._tree_view.collapse_all()
)
toolbar.insert(expand_tree_view_button, -1)
toolbar.insert(collapse_tree_view_button, -1)
status_bar = gtk.Statusbar()
status_bar.push(
status_bar.get_context_id("Status message"),
"A status message."
)
self._master_grid = gtk.Grid()
self._master_grid.attach(toolbar, 0, 0, 1, 1)
self._master_grid.attach(scrolled_tree_view, 0, 1, 1, 1)
self._master_grid.attach(status_bar, 0, 2, 1, 1)
self.add(self._master_grid)
self.connect("delete-event", gtk.main_quit)
self.show_all()
def populate_tree_view(self):
tree_store = gtk.TreeStore(str, str, str)
# Warnings don't occur when there are less than 100 "root" items
for i in range(100):
item1 = tree_store.append(
None,
["Window " + str(i + 1), "12345678", "ClassName"]
)
for j in range(3):
item2 = tree_store.append(
item1,
["Window " + str(i + 1) + str(i + 2),
"12345678",
"ClassName"]
)
for k in range(5):
tree_store.append(
item2,
["Window " + str(i + 1) + str(j + 1) + str(k + 1),
"12345678",
"ClassName"]
)
self._tree_view.set_model(tree_store)
class MCVEApp(gtk.Application):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def do_activate(self):
MCVEWindow()
gtk.main()
if __name__ == "__main__":
MCVEApp().run()
如果您设置了环境,您应该能够复制、粘贴和运行此代码。
警告不遵循任何特定模式,有时有一个警告,有时有两个或更多。每当我展开所有树项目时,警告也会弹出。
GTK 版本为 3.22.18
什么可能导致这些警告?
我在 GTK App 开发邮件列表上收到了答案,这让我找到了解决方案:
将 TreeView 附加到 GTK 网格,然后将其添加到 ScrolledWindow 解决了我的问题。
而不是这个
scrolled_tree_view = gtk.ScrolledWindow()
scrolled_tree_view.add(self._tree_view)
您需要执行以下操作
scrolled_tree_view = gtk.ScrolledWindow()
grid = gtk.Grid()
grid.attach(self._tree_view, 0, 0, 1, 1)
scrolled_tree_view.add(grid)
不幸的是,这在任何地方都没有记录。