【问题标题】:How To Add Multiple Tables In One Tkinter Frame如何在一个 Tkinter 框架中添加多个表
【发布时间】:2021-07-10 03:04:30
【问题描述】:

我在 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 个?谢谢!

【问题讨论】:

    标签: python tkinter tkinter-canvas


    【解决方案1】:

    好,我们一步步来,

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

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

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

    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。 如果您稍后想在框架中添加菜单栏之类的东西,您可能希望将表格框架捆绑在另一个框架中,以便它们调整大小相同

    【讨论】:

    • 您好,谢谢您的回复!如果我想添加 6 个不同的图表,但它们之间的间隔都是均匀的,我不希望它们都接触我希望在整个屏幕上有 6 个图表,它们之间的空间均匀?我删除了粘性,因为我不相信我希望它们填充整个列/行但仍然遇到一些问题
    • 您可以添加任意数量的填充,您也可以在内部和外部之间进行选择,see@LaneFloyd
    猜你喜欢
    • 2018-10-31
    • 1970-01-01
    • 2014-01-21
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 2021-12-24
    • 2022-12-08
    相关资源
    最近更新 更多