如何在一个tkinter框架中添加多个表

lf5gs5x2  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(421)

我在python中使用了一个名为tkintertable的库,它本质上使我能够有效地将表添加到tkinter应用程序中。我正在尝试设置6个不同的表,其中6个图表与此框架(基本上是我的主页)中的每个表一起使用,但我对tkinter来说是全新的,因此在组织它时遇到了一些问题,目前我的代码如下所示:

import tkinter as tk
from tkintertable import TableCanvas, TableModel

class MainApplication(tk.Tk):
    """Main application class"""
    def __init__(self, *args,**kwargs):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(TickerInput, None)

    def switch_frame(self, frame_class, ticker):
        """Destroys current frame and replaces it with a new one."""

        if ticker is not None:
           new_frame = frame_class(self, ticker) 
        else:
            new_frame = frame_class(self)

        if self._frame is not None:
            self._frame.destroy()

        self._frame = new_frame
        self._frame.grid()

class TickerInput(tk.Frame):
    """Ticker input page that allows input of ticker and redirects to main indicator page"""
    def __init__(self, master):
        tk.Frame.__init__(self, master, background="#212020")

        # Centers the frame
        self.master.columnconfigure(0, weight=1)
        self.master.rowconfigure(0, weight=1)

        ticker_label = tk.Label(self, text="Enter ticker..")
        ticker_label.grid(row=0, column=1, columnspan=2)

        ticker_input = tk.Entry(self, width=35)
        ticker_input.grid(row=0, column=3)

        button = tk.Button(self, text="Search", command=lambda: master.switch_frame(Indicator, ticker_input.get()))
        button.grid(row=1, column=1, columnspan=3)

class Indicator(tk.Frame):
    """Indicator page that shows the indicators and whether its a buy or sell"""
    def __init__(self, master, ticker):
        tk.Frame.__init__(self, master)
        self.ticker = ticker
        self.master.columnconfigure(0, weight=0)
        self.master.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)

        button = tk.Button(self, text="Go Back", command=lambda: master.switch_frame(TickerInput, None))
        button.grid(column=0, row=0)

        ticker_label = tk.Label(self, text=("Current Ticker: " + self.ticker))
        ticker_label.grid(column=1, row=0)

        tableOne = Table(self)
        tableOne.grid(column=0, row=1)
        tableOne.createTable()

        # tableTwo = Table(self)
        # tableTwo.createTable()

class Table(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.master = master

    def createTable(self):
        data = {'rec1': {'col1': 99.88, 'col2': 108.79, 'label': 'rec1'},
       'rec2': {'col1': 99.88, 'col2': 108.79, 'label': 'rec2'}
       } 

        model = TableModel()
        table = TableCanvas(self.master, model=model, data=data)
        table.show()

        return table

if __name__ == "__main__":
    app = MainApplication()
    app.title("Indicator Trading Confirmation Tool")
    app.geometry("1920x1080")
    app.config(background="#121212")
    app.mainloop()

这是gui

为什么我的返回按钮卡在table上,如果我把它放在另一排,并且把我的体重设置为1,这应该将它们分开50%正确?还有,组织这两个网格/代码的最佳方式是什么,以确保gui有6个相等的表/图表,3个在左边,3个在右边?非常感谢。

yhived7q

yhived7q1#

好的,让我们一步一步走,
取代了你的 Table 类只有一个彩色框架,对我来说没有重叠,所以这可能只是由于表本身的布局——快速修复可能是添加了一些填充
这个 weight 选项用于在调整窗口大小时分配额外的空间,有关更多信息,您可以在此处查看,这里还有一个很好的可视化功能
为了使用 weight 用于控制实际使用的 widthheight 在您的小部件中,您可能需要查看 sticky 选择 grid 有关非常好的概述,请参见。您还可以检查这一点,这两个方面都非常详细
对于您的总体布局,您可以选择简单的组合路线 weightsticky 类似这样的选项:

self.l1 = tk.Frame(self, background="red")
        self.l2 = tk.Frame(self, background="orange")
        self.l3 = tk.Frame(self, background="green")
        self.l4 = tk.Frame(self, background="blue")
        self.l5 = tk.Frame(self, background="brown")
        self.l6 = tk.Frame(self, background="yellow")

        self.rowconfigure(0, weight = 1)
        self.rowconfigure(1, weight = 1)
        self.rowconfigure(2, weight = 1)
        self.columnconfigure(0, weight = 1)
        self.columnconfigure(1, weight = 1)

        self.l1.grid(row=0, column=0, sticky="nsew")
        self.l2.grid(row=1, column=0, sticky="nsew")
        self.l3.grid(row=2, column=0, sticky="nsew")
        self.l4.grid(row=0, column=1, sticky="nsew")
        self.l5.grid(row=1, column=1, sticky="nsew")
        self.l6.grid(row=2, column=1, sticky="nsew")

宽度为50:50,高度为33:33。如果以后要在框架内添加菜单栏之类的内容,可能需要将表格框架捆绑在另一个框架内,以便使它们的大小相等

相关问题