首页 > 解决方案 > 如何在一个 Tkinter 框架中添加多个表

问题描述

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

这是图形用户界面 在此处输入图像描述

为什么我的返回按钮会卡在桌子上,如果我把它放在不同的行上并且我的体重设置为 1 应该将它们分开 50% 正确?还有什么是组织这两个网格/代码相关的最佳方法,以确保 GUI 有 6 个相等的表格/图表,左侧 3 个,右侧 3 个?谢谢!

标签: pythontkintertkinter-canvas

解决方案


好吧,让我们一步一步来,

  1. 用一个彩色框架替换了你的Table类,对我来说没有任何重叠,所以这可能只是由于表格本身的布局 - 快速修复可能是添加一些填充

  2. weight选项用于在调整窗口大小时分配额外空间,有关更多信息,您可以查看这里,这里也有一个很好的可视化

为了weight用于控制实际使用的widthheight您的小部件,您可能需要查看查看选项sticky以获得非常好的概述,您还可以检查对于两者都非常详细grid

  1. 对于您的总体布局,您可以采用简单的组合方式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:33。如果您稍后想要在框架中添加类似菜单栏的东西,您可能希望将您的表格框架捆绑在另一个框架中,以便让它们调整大小相同


推荐阅读