Как добавить переместить значение переменной из одного метода в другой? Пытаюсь написать приложение для построения графика функции
У меня возникла сложность с тем, чтобы взять значение переменной textboxValue из метода on_click и передать в метод initUI
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") # в скобках должна быть переменная
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_())
https://github.com/PyScientist/Plot_function/blob/master/Plot_function.py вопрос решен, пользуйтесь кому нужно)
Ответы (2 шт):
Автор решения: Andrew Holovko
→ Ссылка
У меня возникла сложность с тем, чтобы переместить переменную textboxValue метода on_click в метод initUI.
class App():
textboxValue: str
def __init__(self):
self.on_click()
self.initUI()
def initUI(self):
print(self.textboxValue)
def on_click(self):
self.textboxValue = 'some text...'
a = App()
Автор решения: S. Nick
→ Ссылка
Попробуйте так:
import sys
import numpy as np
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, \
QSizePolicy, QMessageBox, QWidget, QPushButton, QLineEdit
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
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.axes.clear() # +++
self.axes.plot(x, y) # +++
self.draw()
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.clicked.connect(self.on_click)
self.button.move(400, 400)
self.m = PlotCanvas(self, width=5, height=4) # +++ self
# m.plot("x**2") # в скобках должна быть переменная
# self.m.plot(f"x**{self.textboxValue}")
def on_click(self):
textboxValue = self.textbox.text()
print(textboxValue)
self.m.plot(f"x**{textboxValue}")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.resize(800, 600)
ex.show()
sys.exit(app.exec_())
