I am trying to use a GUI for button and plot. When I include show_graph() function inside tabss the window is resized, also widgets and text resized to smaller.
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from tkinter import *
from tkinter import ttk
import numpy as np
# Function to toggle the motor button state
def togglebutton():
if Controlbutton["text"] == "Disable":
Controlbutton["text"] = "Enable"
Controlbutton["bg"] = "orange"
else:
Controlbutton["text"] = "Disable"
Controlbutton["bg"] = "red"
def show_graph(tab):
spd_frame = Frame(tab)
spd_frame.grid(row=0, column=0)
fig, ax = plt.subplots(dpi=100)
time = np.linspace(0, 10, 100)
speed = 3 * time + 2
ax.plot(time, speed)
ax.set_xlabel("X (s)")
ax.set_ylabel("Y")
canvas = FigureCanvasTkAgg(fig, master=spd_frame)
canvas.draw()
canvas.get_tk_widget().grid(row=0, column=0, padx=5, pady=5)
canvas.get_tk_widget().config(width=500, height=200)
# Function to create tabs and plot graphs
def tabss(root):
# Tabs window
tabcontrol = ttk.Notebook(root)
operationtab = ttk.Frame(tabcontrol)
plottab = ttk.Frame(tabcontrol)
tabcontrol.add(operationtab, text='Operations')
tabcontrol.add(plottab, text='Plot')
tabcontrol.pack(expand=1, fill='both')
# Operation content START
optframe = Frame(operationtab)
optframe.grid(row=0, column=1)
Label(optframe, text="State:").grid(row=0, column=0, padx=5, pady=5)
global Controlbutton
Controlbutton = Button(optframe, text="Disable", command=togglebutton, bg="red", width=20, height=2)
Controlbutton.grid(row=0, column=1, padx=10, pady=30)
Label(optframe, text="Control Mode:").grid(row=1, column=0, padx=5, pady=5)
tkvar = StringVar(optframe)
ControlModeoptions = ["Open", "close"]
ControlMode = OptionMenu(optframe, tkvar, *ControlModeoptions)
ControlMode.grid(row=1, column=1, padx=30, pady=10)
ControlMode.config(width=15)
tkvar.set(ControlModeoptions) # Set default value
#show_graph(plottab)
if __name__ == "__main__":
# Basic setting for main window
root = Tk()
root.title("GUI V0.1")
root.geometry("880x600")
# Call the function to create tabs and plots
tabss(root)
root.mainloop()
To resolve this I used
pack_propagate(False)
grid_rowconfigure(0, weight=1)
grid_columnconfigure(0, weight=1)
without including show_graph function
after including show_graph GUI is resized to smaller where should I configure to resolve this.
I ran both the code with and without show_graph function bigger one is without show_graph function and smaller one is with show_graph function.
As @JasonYang suggested I replaced
matplotlib.pyplot.figure
with
matplotlib.figure.Figure
It worked and no resize or scaling issue