2016-12-02 83 views
2

我嘗試更改列的寬度。當我將它們設置爲自動大小,但我不能讓它們變小。自動調整Python Gtk3 TreeViewColumn,但使它們可調整大小

然後我將列設置爲固定大小,但現在水平滾動條不再出現。

所以也許有可能開始autosize列的寬度,但也有可能以後更改寬度?

#!/usr/bin/env python 

import gi 
gi.require_version('Gtk', '3.0') 
from gi.repository import Gtk 

window = Gtk.Window(Gtk.WindowType.TOPLEVEL) 
window.set_size_request(400, 200) 
mainbox = Gtk.VBox() 
window.add(mainbox) 

scrolled_window = Gtk.ScrolledWindow() 
mainbox.pack_start(scrolled_window, True, True, 0) 

transactions_liststore = Gtk.ListStore(str, str, str, str, str, str) 
for n in range(30): 
    transactions_liststore.append(["A", "2016-10-10", "Ulrich Farmer", "99,99 EUR", "A short Text", "A longer Text with much much more more content"]) 
treeview = Gtk.TreeView(Gtk.TreeModelSort(transactions_liststore)) 
scrolled_window.add(treeview) 

for n, name in enumerate(["Type", "Date", "Name", "Value", "Info1", "Info2"]): 
    cell = Gtk.CellRendererText() 
    column = Gtk.TreeViewColumn(name, cell, text=n) 

    if True: 
     column.set_sizing(Gtk.TreeViewColumnSizing.FIXED) 
     column.set_fixed_width(50) 
     column.set_min_width(50) 
     column.set_expand(True) 

    column.set_resizable(True) 
    column.set_reorderable(True) 
    treeview.append_column(column) 

window.show_all() 
Gtk.main() 

編輯: 上https://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreeViewColumn.html#Gtk.TreeViewColumn.set_resizable

發現如果可調整大小是真與列的大小調整模式是 Gtk.TreeViewColumnSizing.AUTOSIZE,然後上漿模式更改爲 Gtk.TreeViewColumnSizing.GROW_ONLY 。

回答

1

試試這個代碼:

if True: 
    column.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE) 
    #column.set_fixed_width(50) 

編輯:

import gi 
gi.require_version('Gtk', '3.0') 
from gi.repository import Gtk, GdkPixbuf, Gdk 
import os, sys 


class GUI: 
    def __init__(self): 
     window = Gtk.Window(Gtk.WindowType.TOPLEVEL) 
     window.set_size_request(400, 200) 
     mainbox = Gtk.VBox() 
     window.add(mainbox) 
     window.connect('destroy', self.on_window_destroy) 

     scrolled_window = Gtk.ScrolledWindow() 
     mainbox.pack_start(scrolled_window, True, True, 0) 

     transactions_liststore = Gtk.ListStore(str, str, str, str, str, str)  
     for n in range(30): 
      transactions_liststore.append(["A", "2016-10-10", "Ulrich Farmer", "99,99 EUR", "A longer Text with much much more more content", "A short Text"]) 
     treeview = Gtk.TreeView(Gtk.TreeModelSort(transactions_liststore)) 
     scrolled_window.add(treeview) 

     for n, name in enumerate(["Type", "Date", "Name", "Value", "Info1", "Info2"]): 
      cell = Gtk.CellRendererText() 
      column = Gtk.TreeViewColumn(name, cell, text=n) 

      column.set_min_width(50) 
      column.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE) 
      column.set_resizable(True) 
      column.set_reorderable(True) 
      treeview.append_column(column) 
     window.show_all() 

    def on_window_destroy(self, window): 
     Gtk.main_quit() 
def main(): 
    app = GUI() 
    Gtk.main() 

if __name__ == "__main__": 
    sys.exit(main()) 

您仍然無法改變最後一列,但它的工作原理來調整其他列。希望這可以幫助。

+0

感謝您的評論。這是我的問題:當我將它們設置爲自動大小,但我不能讓它們變小。 – oxidworks

+0

你想讓它們比列名小嗎?你想讓哪一列變小? – theGtknerd

+0

不小於列名稱,然後是內容文本。例如,想想Info1有一行文本內容很長,但是您希望該行非常小,因爲它更需要查看Info2行:-) – oxidworks