0

I have a simple tkinter app that plots a sine curve. The problem I am facing is that when I press the PLOT button, original root window shrinks in size. I want to keep the window size fixed, irrespective of pressing the PLOT button or not. what shall I do?

import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np

# Function to handle button click event
def plot_sine_wave():
    # Generate random data for the sine wave
    x = np.linspace(0, 2 * np.pi, 100)
    y = np.sin(x + np.random.uniform(-0.5, 0.5))

    # Create a new window for the plot
    plot_window = tk.Toplevel(root)
    plot_window.title("Random Sine Wave Plot")

    # Create the plot
    plt.figure()
    plt.plot(x, y)
    plt.xlabel("x")
    plt.ylabel("y")
    plt.title("Random Sine Wave")
    plt.grid(True)

    # Display the plot in the new window
    canvas = FigureCanvasTkAgg(plt.gcf(), master=plot_window)
    canvas.draw()
    canvas.get_tk_widget().pack()

# Create the main window
root = tk.Tk()
root.title("Sine Wave Plot")
root.geometry("200x100")
root.resizable(False, False)  # Set resizable attribute to False

# Create the 'PLOT' button
plot_button = tk.Button(root, text="PLOT", command=plot_sine_wave)
plot_button.pack(pady=20)

# Run the Tkinter event loop
root.mainloop()
KawaiKx
  • 9,558
  • 19
  • 72
  • 111
  • 1
    copied and pasted the code and the main window size never changed, is there any other code that could be affecting it? – Shorn May 12 '23 at 05:20
  • running in windows 10 environment. no other python app running. using pycharm. – KawaiKx May 12 '23 at 05:30
  • You can try using `minsize` or try some other [geometry management](https://stackoverflow.com/a/63536506/13629335) choices. – Thingamabobs May 12 '23 at 17:00

1 Answers1

0

The problem is that when the plot is displayed in a new window, the size determines according to size of the new window and the contents inside it. Solution is to make the new window size to a fixed value.

import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np

# Function to handle button click event
def plot_sine_wave():
    # Generate random data for the sine wave
    x = np.linspace(0, 2 * np.pi, 100)
    y = np.sin(x + np.random.uniform(-0.5, 0.5))

    # Create a new window for the plot
    plot_window = tk.Toplevel(root)
    plot_window.title("Random Sine Wave Plot")
    plot_window.geometry("420x300")  # <-- Set the size of the new window

    # Create the plot
    plt.figure()
    plt.plot(x, y)
    plt.xlabel("x")
    plt.ylabel("y")
    plt.title("Random Sine Wave")
    plt.grid(True)

    # Display the plot in the new window
    canvas = FigureCanvasTkAgg(plt.gcf(), master=plot_window)
    canvas.draw()
    canvas.get_tk_widget().pack()

# Create the main window
root = tk.Tk()
root.title("Sine Wave Plot")
root.geometry("200x100")
root.resizable(False, False)  # Set resizable attribute to False

# Create the 'PLOT' button
plot_button = tk.Button(root, text="PLOT", command=plot_sine_wave)
plot_button.pack(pady=20)

# Run the Tkinter event loop
root.mainloop()