GTK - Python Treeview 排序列数据(文件大小 - 字节数据)
GTK - Python Treeview Sort Column Data (File Size - Byte Data)
我有一个带有 Python 代码的 GTK GUI。 GUI 上有一个树视图。我需要在树视图上列出文件大小,并通过单击该列按文件大小对它们进行排序。但它使用大小作为字符串对文件进行排序。
排序文件的 common way
是什么?
- 使用字符串文件大小值(使用单元格函数或自定义排序函数)。
- 使用整数文件大小值(使用单元格函数或自定义排序函数)。
- 另一种方式?
这里是简化的代码:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
builder = Gtk.Builder()
builder.add_from_file('test1.glade')
window1 = builder.get_object('window1')
treeview1 = builder.get_object('treeview1')
class Signals:
def on_window1_destroy(self, widget):
Gtk.main_quit()
file_data = [["file1", 12345],
["file2", 1234567890],
["file3", 1234567],
["file4", 1234]]
# Some code to convert data
# .
# .
# .
file_data_converted = [["file1", "12.3 KB"],
["file2", "1.23 GB"],
["file3", "1.23 MB "],
["file4", "1.23 KB"]]
treestore1 = Gtk.TreeStore(str, str)
piter1 = treestore1.append(None, file_data_converted[0])
piter2 = treestore1.append(None, file_data_converted[1])
piter3 = treestore1.append(None, file_data_converted[2])
piter4 = treestore1.append(None, file_data_converted[3])
treeview1.set_model(treestore1)
for i, column_title in enumerate(["File", "Size"]):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(column_title, renderer, text=i)
column.set_sort_column_id(i)
treeview1.append_column(column)
builder.connect_signals(Signals())
window1.show_all()
Gtk.main()
这是 Glade 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="default_width">300</property>
<property name="default_height">300</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child internal-child="selection">
<object class="GtkTreeSelection"/>
</child>
</object>
</child>
</object>
</interface>
软件信息:Python 3.7, GTK3.
您需要在商店中有一个隐藏列,其中文件大小为整数,仅用于排序。这是一个例子:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
builder = Gtk.Builder()
builder.add_from_file('gtk.ui')
window1 = builder.get_object('window1')
treeview1 = builder.get_object('treeview1')
class Signals:
def on_window1_destroy(self, widget):
Gtk.main_quit()
file_data_converted = [["file1", 12345, "12.3 KB"],
["file2", 1234567890, "1.23 GB"],
["file3", 1234567, "1.23 MB "],
["file4", 1234, "1.23 KB"]]
treestore1 = Gtk.TreeStore(str, int, str)
piter1 = treestore1.append(None, file_data_converted[0])
piter2 = treestore1.append(None, file_data_converted[1])
piter3 = treestore1.append(None, file_data_converted[2])
piter4 = treestore1.append(None, file_data_converted[3])
treeview1.set_model(treestore1)
# file name column
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn('File', renderer, text=0)
column.set_sort_column_id(0)
treeview1.append_column(column)
# file size column
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn('Size', renderer, text=2)
column.set_sort_column_id(1)
treeview1.append_column(column)
builder.connect_signals(Signals())
window1.show_all()
Gtk.main()
我有一个带有 Python 代码的 GTK GUI。 GUI 上有一个树视图。我需要在树视图上列出文件大小,并通过单击该列按文件大小对它们进行排序。但它使用大小作为字符串对文件进行排序。
排序文件的 common way
是什么?
- 使用字符串文件大小值(使用单元格函数或自定义排序函数)。
- 使用整数文件大小值(使用单元格函数或自定义排序函数)。
- 另一种方式?
这里是简化的代码:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
builder = Gtk.Builder()
builder.add_from_file('test1.glade')
window1 = builder.get_object('window1')
treeview1 = builder.get_object('treeview1')
class Signals:
def on_window1_destroy(self, widget):
Gtk.main_quit()
file_data = [["file1", 12345],
["file2", 1234567890],
["file3", 1234567],
["file4", 1234]]
# Some code to convert data
# .
# .
# .
file_data_converted = [["file1", "12.3 KB"],
["file2", "1.23 GB"],
["file3", "1.23 MB "],
["file4", "1.23 KB"]]
treestore1 = Gtk.TreeStore(str, str)
piter1 = treestore1.append(None, file_data_converted[0])
piter2 = treestore1.append(None, file_data_converted[1])
piter3 = treestore1.append(None, file_data_converted[2])
piter4 = treestore1.append(None, file_data_converted[3])
treeview1.set_model(treestore1)
for i, column_title in enumerate(["File", "Size"]):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(column_title, renderer, text=i)
column.set_sort_column_id(i)
treeview1.append_column(column)
builder.connect_signals(Signals())
window1.show_all()
Gtk.main()
这是 Glade 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="default_width">300</property>
<property name="default_height">300</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child internal-child="selection">
<object class="GtkTreeSelection"/>
</child>
</object>
</child>
</object>
</interface>
软件信息:Python 3.7, GTK3.
您需要在商店中有一个隐藏列,其中文件大小为整数,仅用于排序。这是一个例子:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
builder = Gtk.Builder()
builder.add_from_file('gtk.ui')
window1 = builder.get_object('window1')
treeview1 = builder.get_object('treeview1')
class Signals:
def on_window1_destroy(self, widget):
Gtk.main_quit()
file_data_converted = [["file1", 12345, "12.3 KB"],
["file2", 1234567890, "1.23 GB"],
["file3", 1234567, "1.23 MB "],
["file4", 1234, "1.23 KB"]]
treestore1 = Gtk.TreeStore(str, int, str)
piter1 = treestore1.append(None, file_data_converted[0])
piter2 = treestore1.append(None, file_data_converted[1])
piter3 = treestore1.append(None, file_data_converted[2])
piter4 = treestore1.append(None, file_data_converted[3])
treeview1.set_model(treestore1)
# file name column
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn('File', renderer, text=0)
column.set_sort_column_id(0)
treeview1.append_column(column)
# file size column
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn('Size', renderer, text=2)
column.set_sort_column_id(1)
treeview1.append_column(column)
builder.connect_signals(Signals())
window1.show_all()
Gtk.main()