class MyApp(QMainWindow): signal = pyqtSignal(str, str, int, str) def init(self, parent= None): super(MyApp, self).init(parent) self.setWindowTitle("Matplotlib in PyQt6") MainWindow.resize(self, 800, 600) self.star() self.initUI() def star(self): btn1 = QPushButton("Button 1", self) btn1.move(30, 50) btn2 = QPushButton("Button 2", self) btn2.move(150, 50) btn1.clicked.connect(self.buttonClicked1) btn2.clicked.connect(self.buttonClicked2) # create textbox self.textbox1 = QLineEdit(self) self.textbox1.setText("123") self.textbox2 = QLineEdit(self) self.textbox1.move(20, 20) self.textbox1.resize(280, 40) self.textbox2.move(20, 150) self.textbox2.resize(280, 40) self.show() def initUI(self): # 创建一个QWidget对象 central_widget = QWidget(self) # 将该QWidget设置为主窗口的中心组件 self.setCentralWidget(central_widget) # 创建需要显示的QWidget组件,例如QPushButton和QLabel button = QPushButton("Button", central_widget) label = QLabel("Label", central_widget) label.setFixedSize(100, 10) # 创建一个Matplotlib的Figure对象 self.figure = Figure(figsize=(5, 4), dpi=100) # 在Figure中添加一个Axes对象 self.axes = self.figure.add_subplot(111) self.axes.set_xlabel("X-axis") self.axes.set_ylabel("Y-axis") self.axes.set_title("Matplotlib in PyQt6") # 创建一个FigureCanvas对象,并将其放置在QWidget中 canvas = FigureCanvas(self.figure) canvas.resize(self, 200, 100) canvas.updateGeometry() # 将FigureCanvas添加到QWidget中 central_widget = QWidget(self) self.setCentralWidget(central_widget) # 创建一个QVBoxLayout对象,将需要显示的QWidget组件添加到其中 layout = QVBoxLayout(central_widget) layout.addWidget(button) layout.addWidget(label) layout.addWidget(label) layout.addWidget(canvas) @pyqtSlot() def buttonClicked1(self): text = self.textbox1.text() self.statusBar().showMessage(text) @pyqtSlot() def buttonClicked2(self): text = self.textbox1.text() self.textbox2.setText(text) self.statusBar().showMessage(text)如何把def sta里的textbox获得的参数通过他的监听把参数传值到def init上,使得def init 上可以获得外部的值作为变量传入调用
时间: 2024-03-11 14:02:59 浏览: 146
你可以在 `star()` 方法中将 `self.textbox1` 和 `self.textbox2` 的文本内容保存为实例变量,然后在 `buttonClicked1()` 和 `buttonClicked2()` 方法中更新这些实例变量。然后你就可以在 `initUI()` 方法中使用这些实例变量了。下面是修改后的代码:
```
class MyApp(QMainWindow):
signal = pyqtSignal(str, str, int, str)
def __init__(self, parent=None):
super(MyApp, self).__init__(parent)
self.setWindowTitle("Matplotlib in PyQt6")
self.resize(800, 600)
self.textbox1_text = ""
self.textbox2_text = ""
self.star()
self.initUI()
def star(self):
btn1 = QPushButton("Button 1", self)
btn1.move(30, 50)
btn2 = QPushButton("Button 2", self)
btn2.move(150, 50)
btn1.clicked.connect(self.buttonClicked1)
btn2.clicked.connect(self.buttonClicked2)
self.textbox1 = QLineEdit(self)
self.textbox1.setText("123")
self.textbox1.move(20, 20)
self.textbox1.resize(280, 40)
self.textbox1.textChanged.connect(self.textbox1Changed)
self.textbox2 = QLineEdit(self)
self.textbox1.move(20, 150)
self.textbox1.resize(280, 40)
self.textbox2.textChanged.connect(self.textbox2Changed)
self.show()
def initUI(self):
central_widget = QWidget(self)
self.setCentralWidget(central_widget)
button = QPushButton("Button", central_widget)
label = QLabel("Label", central_widget)
label.setFixedSize(100, 10)
self.figure = Figure(figsize=(5, 4), dpi=100)
self.axes = self.figure.add_subplot(111)
self.axes.set_xlabel("X-axis")
self.axes.set_ylabel("Y-axis")
self.axes.set_title("Matplotlib in PyQt6")
self.canvas = FigureCanvas(self.figure)
self.canvas.resize(self, 200, 100)
self.canvas.updateGeometry()
layout = QVBoxLayout(central_widget)
layout.addWidget(button)
layout.addWidget(label)
layout.addWidget(label)
layout.addWidget(self.canvas)
@pyqtSlot()
def buttonClicked1(self):
text = self.textbox1_text
self.statusBar().showMessage(text)
@pyqtSlot()
def buttonClicked2(self):
text = self.textbox2_text
self.textbox2.setText(text)
self.statusBar().showMessage(text)
@pyqtSlot(str)
def textbox1Changed(self, text):
self.textbox1_text = text
@pyqtSlot(str)
def textbox2Changed(self, text):
self.textbox2_text = text
```
阅读全文