A
A
Alex32112021-03-24 00:31:45
Python
Alex3211, 2021-03-24 00:31:45

How to add move variable value from one method to another?

I'm having difficulty getting the value of the textboxValue variable from the on_click method and passing it to the initUI method

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget, \
    QPushButton, QLineEdit
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure


class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'Тест'
        self.left = 60
        self.top = 60
        self.width = 800
        self.height = 600
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.textbox = QLineEdit(self)
        self.textbox.move(500, 500)
        self.textbox.resize(280, 40)
        self.button = QPushButton('Показать', self)
        self.button.move(400, 400)
        m = PlotCanvas(self, width=5, height=4)

        self.button.clicked.connect(self.on_click)

        m.plot("x**2") #сюда надо передать значение textboxValue

        self.show()
    def on_click(self):
        self.textboxValue = self.textbox.text()
        print(self.textboxValue)

class PlotCanvas(FigureCanvas):

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
                QSizePolicy.Expanding,
                QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)


    def plot(self, m):
        x = np.linspace(0, 10, 50)

        y = eval(m)
        #y = x**2
        ax = self.figure.add_subplot(111)
        ax.plot(x, y)

        self.draw()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-03-24
@Alex3211

Add this variable to init:

def __init__(self):
    ...
    self.textboxValue = 'x**2'

...
#и тогда можно будет напрямую использовать в процедуре
m.plot(self.textboxValue)

But if the goal is to update the graph of the function when the button is pressed, then the plot procedure must also be called again. In its current form, the graph is drawn only once at the very beginning.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question