【问题标题】:Python: How to display Pandas DataFrame to a Gtk TreeView widget made in GladePython:如何将 Pandas DataFrame 显示到 Glade 制作的 Gtk TreeView 小部件
【发布时间】:2017-09-06 21:42:43
【问题描述】:

我正在构建一个 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()

非常感谢任何输入或指向有用示例的链接!

【问题讨论】:

    标签: python treeview gtk gnome glade


    【解决方案1】:

    也许它对某人有用。 它应该可以在没有林间空地 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()
    

    【讨论】:

    • 欢迎来到 StackOverflow,请考虑为您的答案添加更多解释。
    【解决方案2】:

    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()
    

    你应该没事的

    【讨论】:

    • 我做到了。我在将我的列表存储连接到 Glade UI 中的 TreeView 小部件时仍然遇到问题。
    • 应该是self.AllDataTreeView.append_column(column)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多