pythontkinterframefillexpand

Why is a Tkinter frame not showing all three buttons unless gui is dragged into wider/bigger size


I have a gui which is not showing the buttons the way I would like. The frame is being cropped unless the gui is stretched.enter image description here

Like this, where finally I see the three buttons desired: enter image description here

I want to have the frame with the buttons (frame2) always show the three buttons and maintain the same size irregardless of the size the gui is enlarged to. Any idea of where I am going wrong?

Code

import tkinter as tk
import tkinter                  
from tkinter import ttk 
import matplotlib
matplotlib.use("TkAgg") 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure

#=====================================================================
# ROOT FIGURE FOR GUI
#=====================================================================
root = tk.Tk() 
root.title("Tab Widget")
root.geometry("600x450") 
tabControl = ttk.Notebook(root) 
  
tab1 = ttk.Frame(tabControl) 
tab2 = ttk.Frame(tabControl)
 
tabControl.add(tab1, text ='Circle Cal') 
tabControl.add(tab2, text ='OPW')

tk.Grid.rowconfigure(root, 0, weight=1)
tk.Grid.columnconfigure(root, 0, weight=1)
tabControl.grid(column=0, row=0, sticky=tk.E+tk.W+tk.N+tk.S)

#MAKE A FIGURE OBJECT
my_figure1 = Figure(figsize = (4, 4), dpi = 100) 

#MAKE A FRAME WIDGET
frame1 = tk.Frame(tab1, bd=2, relief=tk.GROOVE)
frame1.pack(side=tk.LEFT, anchor=tk.N, fill=tk.BOTH, expand=True)
#create another frame(frame2) 
frame2 = tk.Frame(tab1, bd=2, relief=tk.GROOVE)
frame2.pack(side=tk.RIGHT, anchor=tk.E, fill=tk.BOTH)

#MAKE A CANVAS OBJECT
my_canvas1 = FigureCanvasTkAgg(my_figure1, master = frame1) # creating the Tkinter canvas containing the Matplotlib figure  

# TURN THE CANVAS OBJECT INTO A CANVAS WIDGET
my_canvas1.get_tk_widget().pack(side = tkinter.TOP, fill = tkinter.BOTH, expand = 1) # placing the canvas on the Tkinter window
my_canvas1.draw()

def plotData():
    pass
def clearPlot():
    pass

# MAKE BUTTON TO PLOT GRAPH
button1 = tk.Button(frame2, text = "Plot", command = plotData, relief = tk.GROOVE, padx =20, pady =20 )
button1.grid(row = 0, column = 0)
# MAKE BUTTON TO CLEAR PLOT
button2 = tk.Button(frame2, text = "Clear", command = clearPlot, relief = tk.GROOVE, padx =20, pady =20 )
button2.grid(row = 0, column = 1)
# MAKE BUTTON TO close
button2 = tk.Button(frame2, text = "Close", command = clearPlot, relief = tk.GROOVE, padx =20, pady =20 )
button2.grid(row = 0, column = 2)

root.mainloop()

Solution

  • The answer is simply that there isn't enough room for the buttons. You're forcing the size of the window to be a specific width and the width is just too small.

    When you force a window to be a specific size, pack will need to shrink one or more widgets to make all of the widgets fit. It does this in the reverse order that the widgets were added with pack.

    Since you want the canvas to be the widget that grows and shrinks, you need to pack the frame that contains it last. So, call pack on frame2 before calling pack on frame1.

    This is easiest if you group your calls to pack together rather than interlacing them with widget creation.

    frame2.pack(side=tk.RIGHT, anchor=tk.E, fill=tk.BOTH)
    frame1.pack(side=tk.LEFT, anchor=tk.N, fill=tk.BOTH, expand=True)