python-3.x - 如何在 tkinter 中显示数据框

标签 python-3.x dataframe tkinter

我是 Python 的新手,甚至是 tkinter 的新手。

我使用了来自 stackoverflow ( Switch between two frames in tkinter ) 的代码来生成一个程序,根据用户选择的选项,在其中调用新框架并将其放置在彼此的顶部。我的代码的精简版本如下。还有很多帧。

import tkinter as tk                
from tkinter import font  as tkfont 
import pandas as pd

class My_GUI(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")


        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, Page_2):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame


            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Welcome to....", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Option selected",
                            command=lambda: controller.show_frame("Page_2"))
        button1.pack()



class Page_2(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="The payment options are displayed below", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

        #I want the able to be display the dataframe here

        button = tk.Button(self, text="Restart",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()

a = {'Option_1':[150,82.50,150,157.50,78.75],
     'Option2':[245,134.75,245,257.25,128.63]}
df = pd.DataFrame(a,index=['a',
                    'b',
                    'c',
                    'd',
                    'e']) 

print(df.iloc[:6,1:2])

if __name__ == "__main__":
    app = My_GUI()
    app.mainloop()

当 Page_2 出现时,我希望它显示一个带有以下代码的数据框。
a = {'Option_1':[150,82.50,150,157.50,78.75],
     'Option2':[245,134.75,245,257.25,128.63]}
df = pd.DataFrame(a,index=['a',
                    'b',
                    'c',
                    'd',
                    'e']) 

print(df.iloc[:6,1:2])

我已经搜索过例如How to display a pandas dataframe in a tkinter window (tk frame to be precise) (未提供答案)和其他网站以回答类似问题但没有成功。

当我选择 Page_2 时,我将如何以及在哪里放置我的数据框代码选择以显示在我想要的区域中?

最佳答案

退房 pandastable .
这是一个非常漂亮的库,用于显示和处理 Pandas 表。

这是一个代码示例 from their documentation :

from tkinter import *
from pandastable import Table, TableModel

class TestApp(Frame):
        """Basic test frame for the table"""
        def __init__(self, parent=None):
            self.parent = parent
            Frame.__init__(self)
            self.main = self.master
            self.main.geometry('600x400+200+100')
            self.main.title('Table app')
            f = Frame(self.main)
            f.pack(fill=BOTH,expand=1)
            df = TableModel.getSampleData()
            self.table = pt = Table(f, dataframe=df,
                                    showtoolbar=True, showstatusbar=True)
            pt.show()
            return

app = TestApp()
#launch the app
app.mainloop()

这里有一个截图(还有 from their docs ):

Screenshot from the pandastable documentation

关于python-3.x - 如何在 tkinter 中显示数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44798950/

相关文章:

python-3.x - 当没有写入权限时,Python tempfile.TemporaryFile 在 Windows 上挂起

r - 如何使用 R 环境中的 data.frames 制作功能列表?

python - 在 Tkinter (Python) 中创建一个根据分数改变大小的圆圈

python - Tkinter 文档与 PEP 8 相矛盾

python-3.x - Pandas Series 值包含列表,如何计算唯一值并将其作为字典返回

python - 如何根据 Pandas Python 中特定列中的重复值检索行?

python - Pandas:创建一列,其中行等于另一列中下面的行

Python Tkinter : create a list with Entry, 并将更新的列表发送到另一个类

python - Python 2.7 和 3.6 之间的不同 @patch 行为(使用 mock)

python - 保留名称为整数且满足特定条件的数据框的列