如何使最后一个gtk.TreeViewColumn可调整大小?
可以使用 gtk.TreeViewColumn.set_ressized(True) 使列可手动调整大小...除了最后一列 - 它始终占用可用空间。
虽然在大多数情况下这是明智的,但我使用 TreeView-in-a-ScrolledView,并且我也希望能够缩小/展开最后一列。
目前我使用以下拼凑:
def add_dummy_column(treeview):
def put_dummy_last(treeview, dummy):
columns = treeview.get_columns()
last = next(reversed(columns), None)
if not last or last == dummy:
return
if dummy in columns:
treeview.move_column_after(dummy, last)
else:
treeview.append_column(dummy)
dummy = gtk.TreeViewColumn()
dummy.set_min_width(1)
dummy.set_fixed_width(1)
dummy.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
dummy.set_resizable(False)
dummy.set_expand(False)
treeview.connect('columns-changed', put_dummy_last, dummy)
return dummy
但是,这个虚拟列往往总是妨碍:使 TreeView 列循环复杂化等。是否有更好/更优雅的解决方案?
更新:它确实有效
在尝试伪造一个最小的示例时,我发现它在没有我的组装的情况下工作得很好。这是没有 gtk.ScrolledWindow 的最小示例:
import gtk
window = gtk.Window()
window.connect('destroy', lambda *args: gtk.main_quit())
vbox = gtk.VBox()
window.add(vbox)
table = gtk.TreeView(gtk.ListStore(str, str, str))
for i in range(3):
column = gtk.TreeViewColumn('Column {}'.format(1 + i))
column.set_resizable(True)
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
column.set_min_width(20)
column.set_fixed_width(80)
column.set_expand(False)
table.append_column(column)
vbox.pack_start(table)
vbox.pack_start(gtk.Statusbar(), False)
window.set_default_size(300, 300)
window.show_all()
gtk.main()
尝试缩小最后一列 - 这是不可能的。但是,如果更改
vbox.pack_start(table)
为,
container = gtk.ScrolledWindow()
vbox.pack_start(container)
container.add(table)
则可以毫无问题地扩展和收缩最后一列。
One can use gtk.TreeViewColumn.set_resizable(True)
to make column manually resizeable... except the last column -- it always occupies the available space.
While it's sensible in most cases, I use TreeView-in-a-ScrolledView, and I'd like to be able to shrink/expand the last column too.
Currently I use the following kludge:
def add_dummy_column(treeview):
def put_dummy_last(treeview, dummy):
columns = treeview.get_columns()
last = next(reversed(columns), None)
if not last or last == dummy:
return
if dummy in columns:
treeview.move_column_after(dummy, last)
else:
treeview.append_column(dummy)
dummy = gtk.TreeViewColumn()
dummy.set_min_width(1)
dummy.set_fixed_width(1)
dummy.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
dummy.set_resizable(False)
dummy.set_expand(False)
treeview.connect('columns-changed', put_dummy_last, dummy)
return dummy
However, this dummy column tends to always get in the way: complicates the TreeView column loops etc. Is there better/more elegant solution?
Update: it actually works
While trying to forge a minimal example, I discovered that it works fine without my kludge. Here is minimal example without gtk.ScrolledWindow:
import gtk
window = gtk.Window()
window.connect('destroy', lambda *args: gtk.main_quit())
vbox = gtk.VBox()
window.add(vbox)
table = gtk.TreeView(gtk.ListStore(str, str, str))
for i in range(3):
column = gtk.TreeViewColumn('Column {}'.format(1 + i))
column.set_resizable(True)
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
column.set_min_width(20)
column.set_fixed_width(80)
column.set_expand(False)
table.append_column(column)
vbox.pack_start(table)
vbox.pack_start(gtk.Statusbar(), False)
window.set_default_size(300, 300)
window.show_all()
gtk.main()
Try to shrink the last column -- it's impossible. However, if you change
vbox.pack_start(table)
to
container = gtk.ScrolledWindow()
vbox.pack_start(container)
container.add(table)
you can expand and shrink the last column with no problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您发布一个我们可以使用的独立工作示例,那么帮助会更容易! :)
但是 - 如果我正确理解你的问题(说实话,我不是 100% 确定) - 可能你正在以错误的方式思考这个问题:你所说的“调整最后一列的大小”是实际上是“调整树视图容器的大小”。
如果是这种情况,您只需在滚动窗口和树视图之间添加一个可调整大小的容器即可。
哈!
It would be easier to help if you would post a self-contained working example that we could play with! :)
However - if I understand your question correctly (which I'm not 100% sure, to be honest) - it might be that you are thinking to the problem in the wrong terms: what you call "resizing the last column" is actually "resizing the tree-view container".
If that is the case, you should simply add a resizeable container between the scrolled window and the treeview.
HTH!