Feb-20-2024, 07:09 PM
I am trying to create a virtual signal-generator(function-generator) - GUI with the cutomtkinter, matplotlib libs. The graph should be updated after a button click continuosly. I have managed to get a graph updated continuosly with a grid in the backround, but the x,y - xaxis and x,y - labels are not visible?
Here is the button code:
![]()
';" src="
" alt="İmage" id="maximage" title="Click Photo To Enlarge">
Here is the code of the whole module:
Here is the button code:
def update_window(self):
startx, endx = 0, SIG_ARRAY_SIZE + 1
starty, endy = -SIG_ARRAY_DEFAULT_AMPLITUDE - 1, SIG_ARRAY_DEFAULT_AMPLITUDE + 1
plt.axis([startx, endx, starty, endy])
plt.xlabel('t [msec]')
plt.ylabel('U [V]')
fig, ax = plt.subplots()
ax = fig.add_subplot(111)
while True:
fig, ax = plt.subplots()
ax.set_title('Signal')
plt.axis([startx, endx, starty, endy])
ax.set_xlabel('t [msec]', fontweight=12)
ax.set_ylabel('U [V]', fontsize=12)
x_val: list = list(range(0, SIG_ARRAY_SIZE, 1))
# generating random data values
sig.sigSetSignalName("Sinus")
sig.sigCreateSinusSignal()
y_val = sig.sig_arr
ax.axis([startx, endx, starty, endy])
ax.grid()
line, = plt.plot(x_val, y_val, 'b')
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0)
canvas = FigureCanvasTkAgg(fig, master=self.root)
canvas.draw()
canvas.get_tk_widget().place(relx=0.33, rely=0.025)
plt.close(fig)
self.root.update()Here is a snapshot of the GUI:';" src="
" alt="İmage" id="maximage" title="Click Photo To Enlarge">
Here is the code of the whole module:
from signal_generator import SignalGenerator
import tkinter as tk
import customtkinter as ctk
from tkinter import Button
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from operator import methodcaller
import time
SIG_ARRAY_SIZE = 360
SIG_ARRAY_DEFAULT_VALUE = 5
SIG_ARRAY_DEFAULT_AMPLITUDE = 10
sig: SignalGenerator = SignalGenerator()
class ctkApp:
def __init__(self):
ctk.set_appearance_mode("dark")
self.root = ctk.CTk()
self.root.geometry("1000x600+200x200")
self.root.title("Signal Scope")
self.root.update()
self.frame = ctk.CTkFrame(master=self.root,
height=self.root.winfo_height() * 0.95,
width=self.root.winfo_width() * 0.66,
fg_color="darkblue")
self.frame.place(relx=0.33, rely=0.025)
self.input = ctk.CTkEntry(master=self.root,
placeholder_text=100,
justify='center',
width=300,
height=50,
fg_color="darkblue")
self.input.insert(0, 100)
self.input.place(relx=0.025, rely=0.5)
self.slider = ctk.CTkSlider(master=self.root,
width=300,
height=20,
from_=1,
to=1000,
number_of_steps=999,
command=self.update_surface)
self.slider.place(relx=0.025, rely=0.75)
self.button = ctk.CTkButton(master=self.root,
text="Update Graph",
width=300,
height=50,
command=self.update_window)
self.button.place(relx=0.025, rely=0.25)
self.root.mainloop()
def update_window(self):
startx, endx = 0, SIG_ARRAY_SIZE + 1
starty, endy = -SIG_ARRAY_DEFAULT_AMPLITUDE - 1, SIG_ARRAY_DEFAULT_AMPLITUDE + 1
plt.axis([startx, endx, starty, endy])
plt.xlabel('t [msec]')
plt.ylabel('U [V]')
fig, ax = plt.subplots()
ax = fig.add_subplot(111)
while True:
fig, ax = plt.subplots()
ax.set_title('Signal')
plt.axis([startx, endx, starty, endy])
ax.set_xlabel('t [msec]', fontweight=12)
ax.set_ylabel('U [V]', fontsize=12)
x_val: list = list(range(0, SIG_ARRAY_SIZE, 1))
# generating random data values
sig.sigSetSignalName("Sinus")
sig.sigCreateSinusSignal()
y_val = sig.sig_arr
ax.axis([startx, endx, starty, endy])
ax.grid()
line, = plt.plot(x_val, y_val, 'b')
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0)
canvas = FigureCanvasTkAgg(fig, master=self.root)
canvas.draw()
canvas.get_tk_widget().place(relx=0.33, rely=0.025)
plt.close(fig)
self.root.update()
def update_surface(self, other):
fig, ax = plt.subplots()
fig.set_size_inches(11, 5.3)
ax.scatter(x, y, s * self.slider.get(), c)
ax.axis("off")
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, wspace=0, hspace=0)
canvas = FigureCanvasTkAgg(fig, master=self.root)
canvas.draw()
canvas.get_tk_widget().place(relx=0.33, rely=0.025)
self.root.update()
if __name__ == "__main__":
CTK_Window = ctkApp()
