在Python中使用Tinkter,我如何使用网格来显示PandasTable而不是pack?

fnvucqvd  于 2023-05-12  发布在  Python
关注(0)|答案(1)|浏览(210)

我正在研究如何使用Tkinter(CustomTkinter),我想使用Tkinter GRID布局显示pandastable,而不是PACK布局。下面的代码将显示表格,但它占用了整个框架。
整个项目非常庞大和复杂,但以下是相关代码:

import customtkinter as ctk
import pandas as pd
from tkinter import END
from pandastable import Table

class DisplayTable(ctk.CTkFrame):
    def __init__(self, parent):
        ctk.CTkFrame.__init__(self, parent)
        
        label = ctk.CTkLabel(self, text="DisplayTable")
        label.grid(row=0, column=1, padx=10, pady=10, columnspan=4)

        df = pd.read_csv("data/data_points.csv")
        self.table = Table(self, dataframe=df, showtoolbar=True, showstatusbar=True)
        self.table.grid(row=1, column=1, padx=10, pady=10, columnspan=4)
        self.table.show()

我的问题是如何将GRID布局应用到pandastable,以便在屏幕顶部有一个标签,而pandastable在下面?

7tofc5zh

7tofc5zh1#

首先,我认为CustomTkinter中的绑定可能有一些问题,因为我得到了与这里相同的错误:AttributeError in Ctk由于某些原因,Ctk不允许bind_all
解决方案应该是将表的主帧作为一个单独的框架。当我使用普通的Tkinter时,这个效果很好(没有这个额外的框架,表也占据了整个窗口):

import customtkinter as ctk
import pandas as pd
from tkinter import END
import tkinter as tk
from pandastable import Table

class DisplayTable(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent, relief="sunken")

        label = tk.Label(self, text="DisplayTable", relief="sunken")
        label.grid(row=0, column=1, padx=10, pady=10, columnspan=4)

        df = pd.read_csv("data_points.csv")

        self.table_FRAME = tk.Frame(self)
        self.table = Table(self.table_FRAME,
                           dataframe=df,
                           showtoolbar=True,
                           showstatusbar=True)
        self.table.grid(row=0, column=0, padx=10, pady=10, columnspan=4)
        self.table_FRAME.grid(row=1, column=1, padx=30, pady=10, columnspan=4,
                              rowspan=10)
        # self.table_FRAME.grid_propagate(False)
        # self.table_FRAME.configure(width=10, height=20)
        self.table.show()

if __name__ == '__main__':
    # Declare root window first to be able to get screen information
    root = tk.Tk()

    app = DisplayTable(parent=root)
    app.pack(fill="both", expand=True)

    root.mainloop()

如果我用CustomTkinter做同样的事情,我会得到我在开始时提到的AttributeError。
无论如何都要使用CustomTkinter的解决方案(使用风险自担,因为它涉及更改代码作者考虑的行为):
使用Ctk的代码:

import customtkinter as ctk
import pandas as pd
from tkinter import END
import tkinter as tk
from pandastable import Table

class DisplayTable(ctk.CTkFrame):
    def __init__(self, parent):
        ctk.CTkFrame.__init__(self, parent)

        label = ctk.CTkLabel(self, text="DisplayTable")
        label.grid(row=0, column=1, padx=10, pady=10, columnspan=4)

        df = pd.read_csv("data_points.csv")

        self.table_FRAME = ctk.CTkFrame(self)
        self.table = Table(self.table_FRAME,
                           dataframe=df,
                           showtoolbar=True,
                           showstatusbar=True)
        self.table.grid(row=0, column=0, padx=10, pady=10, columnspan=4)
        self.table_FRAME.grid(row=1, column=1, padx=30, pady=10, columnspan=4,
                              rowspan=10)
        # self.table_FRAME.grid_propagate(False)
        # self.table_FRAME.configure(width=10, height=20)
        self.table.show()

if __name__ == '__main__':
    # Declare root window first to be able to get screen information
    root = tk.Tk()

    app = DisplayTable(parent=root)
    app.pack(fill="both", expand=True)

    root.mainloop()

当你得到这个错误:

File "C:\Dev\Python\Lib\site-packages\customtkinter\windows\widgets\core_widget_classes\ctk_base_class.py", line 253, in bind_all
    raise AttributeError("'bind_all' is not allowed, could result in undefined behavior")
AttributeError: 'bind_all' is not allowed, could result in undefined behavior

按照bind_all方法的链接,注解掉raise AttributeError,只添加pass

def bind_all(self, sequence=None, func=None, add=None):
    # raise AttributeError("'bind_all' is not allowed, could result in undefined behavior")
    pass

这让它工作,但我不知道为什么bind_all可以result in undefined behavior使用CustomTkinter所以再次,使用风险自担。

相关问题