Created
July 2, 2025 00:21
-
-
Save AlejoDuarte23/0083473809a70fc06a6006d28c8d86f9 to your computer and use it in GitHub Desktop.
Demo PyQt5 to VIKTOR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import numpy as np | |
| from PyQt5.QtWidgets import ( | |
| QApplication, | |
| QMainWindow, | |
| QWidget, | |
| QVBoxLayout, | |
| QFormLayout, | |
| QDoubleSpinBox, | |
| QPushButton, | |
| ) | |
| from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas | |
| from matplotlib.figure import Figure | |
| class SinePlotWidget(QWidget): | |
| """Central widget with inputs and a Matplotlib canvas.""" | |
| def __init__(self, parent=None): | |
| super().__init__(parent) | |
| # --- Matplotlib setup --- | |
| self.figure = Figure() | |
| self.canvas = FigureCanvas(self.figure) | |
| # --- Input controls --- | |
| self.amp_spin = QDoubleSpinBox() | |
| self.amp_spin.setRange(0.0, 10.0) | |
| self.amp_spin.setSingleStep(0.1) | |
| self.amp_spin.setValue(1.0) | |
| self.freq_spin = QDoubleSpinBox() | |
| self.freq_spin.setRange(0.1, 10.0) | |
| self.freq_spin.setSingleStep(0.1) | |
| self.freq_spin.setValue(1.0) | |
| self.phase_spin = QDoubleSpinBox() | |
| self.phase_spin.setRange(-np.pi, np.pi) | |
| self.phase_spin.setSingleStep(0.1) | |
| self.phase_spin.setValue(0.0) | |
| # Arrange inputs in a form | |
| form = QFormLayout() | |
| form.addRow("Amplitude", self.amp_spin) | |
| form.addRow("Frequency", self.freq_spin) | |
| form.addRow("Phase (rad)", self.phase_spin) | |
| # Plot button | |
| self.plot_button = QPushButton("Plot") | |
| self.plot_button.clicked.connect(self.plot_sine) | |
| # --- Layout --- | |
| layout = QVBoxLayout() | |
| layout.addLayout(form) | |
| layout.addWidget(self.plot_button) | |
| layout.addWidget(self.canvas) | |
| self.setLayout(layout) | |
| # Initial plot | |
| self.plot_sine() | |
| def plot_sine(self): | |
| """Draw the sine curve with current parameters.""" | |
| amp = self.amp_spin.value() | |
| freq = self.freq_spin.value() | |
| phase = self.phase_spin.value() | |
| self.figure.clf() | |
| ax = self.figure.add_subplot(111) | |
| x = np.linspace(0.0, 2.0 * np.pi, 400) | |
| y = amp * np.sin(freq * x + phase) | |
| ax.plot(x, y) | |
| ax.set_xlabel("x") | |
| ax.set_ylabel("sin(x)") | |
| ax.set_title( | |
| f"Sine Wave, A={amp:.2f}, f={freq:.2f}, φ={phase:.2f} rad" | |
| ) | |
| self.canvas.draw() | |
| class MainWindow(QMainWindow): | |
| """Main window that hosts the SinePlotWidget.""" | |
| def __init__(self): | |
| super().__init__() | |
| self.setWindowTitle("Interactive Sine Plot") | |
| self.setCentralWidget(SinePlotWidget()) | |
| def main(): | |
| app = QApplication(sys.argv) | |
| window = MainWindow() | |
| window.resize(700, 550) | |
| window.show() | |
| sys.exit(app.exec_()) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment