在Python中使用Tkinter,我怎样使用网格(grid)来显示PandasTable而不是pack?

huangapple go评论68阅读模式
英文:

In Python with Tinkter, how can I use a grid to display a PandasTable instead of pack?

问题

我正在研究如何在Tkinter(CustomTkinter)中使用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?

英文:

I am researching how to use Tkinter ( CustomTkinter ) and I would like to display a pandastable using the Tkinter GRID layout, instead of the PACK layout. The code below will show the table but it is taking up the entire frame.

The entire project is very large and complex but here is the relevant code:

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

My question is how to apply the GRID layout to the pandastable so that I have a label at the top of the screen and panddastable below?

答案1

得分: 1

以下是您要翻译的内容:

首先,我认为CustomTkinter中的绑定可能存在一些问题,因为我遇到了与此处相同的错误:Ctk中的AttributeError
由于某种原因,Ctk不允许使用 bind_all

解决方案应该是将Table的主体作为一个单独的框架。
当我使用常规的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.show()


if __name__ == '__main__':
    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.show()


if __name__ == '__main__':
    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

这可以让它正常工作,但我不知道为什么使用CustomTkinter时bind_all会导致undefined behavior,所以请自行决定是否使用。

英文:

First of all, I think there might be some issue with bindings in CustomTkinter because I got the same error as here: AttributeError in Ctk
For some reason, Ctk doesn't allow bind_all

The solution should be to have the master of the Table as a separate frame.
This worked great when I used regular Tkinter
(without this extra frame, the Table took up the whole window also):

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

在Python中使用Tkinter,我怎样使用网格(grid)来显示PandasTable而不是pack?

If I try the same thing with CustomTkinter I get the AttributeError I mentioned in the beginning.


Workaround with using CustomTkinter anyway (use at your own risk because it involves changing behaviors that the author of the code took into consideration):

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

Now when you get this error:

  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

Follow the link to the bind_all method, comment out the raise AttributeError and just add pass

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

在Python中使用Tkinter,我怎样使用网格(grid)来显示PandasTable而不是pack?

This got it to work but I have no idea why bind_all could result in undefined behavior using CustomTkinter so again, use at your own risk.

huangapple
  • 本文由 发表于 2023年5月8日 01:47:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定