简体   繁体   English

ttk.Notebook每个选项卡上的不同网格大小

[英]ttk.Notebook different grid size on each tab

I'm creating a GUI using Tkinter and I've created multiple tabs using ttk.Notebook(). 我正在使用Tkinter创建一个GUI,并且已经使用ttk.Notebook()创建了多个选项卡。 On Tab1 there are a few Labels and Entry boxes, and on Tab2 I have a matplotlib plot. 在Tab1上有一些Labels和Entry框,在Tab2上有matplotlib图。 I am using the grid layout manager exclusively. 我专门使用网格布局管理器。

The problem I've run into is that when I place the plot on Tab2 (in row=0) it seems to have increased the size of row 0 on Tab 1 as well, creating a lot of space between the 2 labels (which should be right on top of one another). 我遇到的问题是,当我将绘图放置在Tab2上(行= 0)时,它似乎也增加了Tab 1上第0行的大小,在2个标签之间创建了很多空间(应该彼此对上)。

(Very) Minimal version of the code is below. (非常)下面是该代码的最低版本。 What am I missing? 我想念什么? How can I independently control the row height on each tab so widgets on Tab2 don't set the row height on Tab1? 如何独立控制每个选项卡上的行高,以便Tab2上的小部件不设置Tab1上的行高? Thanks in advance for the help. 先谢谢您的帮助。

import tkinter as tk
from tkinter import ttk

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.figure import Figure
matplotlib.use("TkAgg")


class MainGUI(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title('Title')
        self.geometry('750x500')

        # Adds tabs to main window
        self.nb = ttk.Notebook(self)
        self.nb.grid(row=0, column=0, columnspan=5, rowspan=4, sticky='NESW')
        self.tab1 = ttk.Frame(self.nb)
        self.nb.add(self.tab1, text='Tab1')
        self.tab2 = ttk.Frame(self.nb)
        self.nb.add(self.tab2, text='Tab2')

        # defines a grid 10 x 5 cells in the main window & tabs
        rows = 0
        cols = 0
        while rows < 10:
            while cols < 5:
                self.rowconfigure(rows, weight=1)
                self.columnconfigure(cols, weight=1)
                self.tab1.rowconfigure(rows, weight=1)
                self.tab1.columnconfigure(cols, weight=1)
                self.tab2.rowconfigure(rows, weight=1)
                self.tab2.columnconfigure(cols, weight=1)
                cols += 1
            rows += 1

        self.tab1Label = tk.Label(self.tab1, text="This is a Label")
        self.tab1Label.grid(column=0, row=0, sticky='NW')
        self.tab1Label2 = tk.Label(self.tab1, text="This is also a Label")
        self.tab1Label2.grid(column=0, row=1, sticky='NW')

        self.makePlot()

    def makePlot(self):
        f = Figure(figsize=(5, 5), dpi=100)
        a = f.add_subplot(111)
        a.plot([1, 2, 3, 4, 5, 6, 7, 8], [5, 6, 1, 3, 8, 9, 3, 5])
        canvas = FigureCanvasTkAgg(f, self.tab2)
        canvas.draw()
        canvas.get_tk_widget().grid(column=2, row=0, columnspan=2, sticky='NSEW')


def main():
    MainGUI().mainloop()


if __name__ == '__main__':
    main()

If you comment out this line: 如果您注释掉这一行:

self.tab1.rowconfigure(rows, weight=1)

You will notice the issue is gone. 您会发现问题已消失。 Your problem comes from your while loop. 您的问题来自您的while循环。 I can also say that your while loop is not doing what you think it is doing. 我也可以说您的while循环没有按照您认为的那样做。 You only configure row 0 to have a weight and this is why your 2nd label is sitting on the bottom of the screen. 您只需将第0行配置为具有权重,这就是为什么第二个标签位于屏幕底部的原因。

Lets break down what your while statement is doing. 让我们分解一下while语句正在做什么。

On the first loop where row = 0 you tell the loop to do another while loop based on cols . row = 0的第一个循环中,您告诉循环基于cols进行另一个while循环。 So it then does rowconfigure(0, weight=1) 5 times and also does columnconfig(0 through 4, weight=1) . 因此,它将执行rowconfigure(0, weight=1) 5次,也执行columnconfig(0 through 4, weight=1) However the problem comes on the next loop. 但是问题出在下一个循环。 Because cols = 5 now your 2nd while loop will always be false and therefor never configures rows 1 through 9. 因为现在cols = 5 ,所以您的2nd while循环将始终为false,因此永远不会配置第1至9行。

What you should do is something like this. 您应该做的就是这样。

for i in range(10):
    self.tab1.rowconfigure(i, weight=1)
    self.tab2.rowconfigure(i, weight=1)
for i in range(5):
    self.tab1.columnconfigure(i, weight=1)
    self.tab2.columnconfigure(i, weight=1)

This way you can be sure to apply a weight to all rows and columns. 这样,您可以确保对所有行和列都施加权重。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM