我正在构建一个UI,我可以让用户操作数据并选择他们想要使用的参数。我无法将Pandas DataFrame显示到我在Glade中创建的TreeView小部件。
下面是我的Glade XML文件,其中包含Gtk TreeView小部件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.19.0 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkWindow" id="MainWindow">
<property name="can_focus">False</property>
<child>
<object class="GtkTreeView" id="AllDataTreeView">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection1"/>
</child>
</object>
</child>
</object>
</interface>
这是我的Python脚本调用和构建Gtk-Glade UI,我希望显示Pandas DataFrame。
import pandas as pd
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
#%% dataframe to be displayed:
d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']), 'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
#%%
class Handler:
#############################
## INITIALIZATIONS/EXITS ##
#############################
def __init__(self): #initializes the glade file used as well as any windows or text/image viewers#
self.gladefile = "ShowDataExample.glade"
self.builder = Gtk.Builder()
self.builder.add_from_file(self.gladefile)
self.builder.connect_signals(self)
self.window = self.builder.get_object("MainWindow")
# TreeView
self.AllDataTreeView = self.builder.get_object("AllDataTreeView")
# show glade setup
self.window.show()
def on_gtk_quit_activate(self, menuitem, data=None): #closes window when you click on quit from the file chooser#
print "quit from menu"
Gtk.main_quit()
def onDeleteWindow(self, *args): #closes window when you click on the x at the top of the window#
Gtk.main_quit(*args)
if __name__ == "__main__": #Calls on the UI#
main = Handler()
Gtk.main()
非常感谢任何输入或有用示例的链接!
答案 0 :(得分:1)
也许它对某人有用。 它应该可以在没有 Glade UI 的情况下工作。
#import
import pandas as pd
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']), 'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print(df)
class PandasViewer(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="PandasViewer")
# Create a layout
self.box = Gtk.Box(spacing = 6)
self.add(self.box)
# Create the ListStore model
self.pd_ls_model = Gtk.ListStore(str, float, float)
for row in df.itertuples():
self.pd_ls_model.append(list(row))
# Create the TreeView and add the columns
self.treeview = Gtk.TreeView.new_with_model(self.pd_ls_model)
for i, column_title in enumerate(df.columns, start=1):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(column_title, renderer, text=i)
self.treeview.append_column(column)
# Place TreeView inside the layout
self.box.add(self.treeview)
self.show()
win = PandasViewer()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
答案 1 :(得分:0)
在https://python-gtk-3-tutorial.readthedocs.io/en/latest/treeview.html
查看Gtk.TreeView# create model like this
store = Gtk.ListStore(str, str, str)
# fill it with your data
for row in df.itertuples():
store.append([row[0], row[1], row[2]])
# set it as TreeView model
self.AllDataTreeView.set_model(store)
# Create and append columns
for i, col in enumerate(df.columns, start=1):
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn(col, renderer, text=i)
self.AllDataTreeView.append_column(col)
# in yours init function change from this
self.window.show()
# to this
self.window.show_all()
你应该没事。