import tkinter as tk from tkinter import ttk import math from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg root = tk.Tk() root.title("Sine Wave Generator") root.geometry("800x600") # ----------------------------- # Matplotlib # ----------------------------- fig = Figure(figsize=(7, 5), dpi=100) ax = fig.add_subplot(111) canvas = FigureCanvasTkAgg(fig, master=root) canvas.get_tk_widget().pack( side="top", fill="both", expand=True ) # ----------------------------- # Frequency # ----------------------------- frequency = tk.DoubleVar(value=1.0) def update_plot(value): f = float(value) # Update the label frequency_label.config( text=f"Frequency: {f:.1f} Hz" ) # Create time values x = [ i / 999 for i in range(1000) ] # Create sine wave y = [ math.sin(2 * math.pi * f * t) for t in x ] # Update plot ax.clear() ax.plot(x, y) ax.set_title(f"Sine Wave - {f:.1f} Hz") ax.set_xlabel("Time (seconds)") ax.set_ylabel("Amplitude") ax.set_xlim(0, 1) ax.set_ylim(-1.2, 1.2) ax.grid(True) canvas.draw_idle() # ----------------------------- # Frequency label # ----------------------------- frequency_label = ttk.Label( root, text="Frequency: 1.0 Hz", font=("Arial", 14) ) frequency_label.pack( side="bottom", pady=5 ) # ----------------------------- # Slider # ----------------------------- slider = ttk.Scale( root, from_=0.1, to=10.0, orient="horizontal", variable=frequency, command=update_plot ) slider.pack( side="bottom", fill="x", padx=20, pady=10 ) # ----------------------------- # Initial plot # ----------------------------- update_plot(str(frequency.get())) root.mainloop()