简体   繁体   中英

ttk.Notebook different grid size on each tab

I'm creating a GUI using Tkinter and I've created multiple tabs using ttk.Notebook(). On Tab1 there are a few Labels and Entry boxes, and on Tab2 I have a matplotlib plot. 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).

(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? 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. I can also say that your while loop is not doing what you think it is doing. You only configure row 0 to have a weight and this is why your 2nd label is sitting on the bottom of the screen.

Lets break down what your while statement is doing.

On the first loop where row = 0 you tell the loop to do another while loop based on cols . So it then does rowconfigure(0, weight=1) 5 times and also does 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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