pyside2用槽函数在界面上显示文本代码
时间: 2023-04-07 20:03:23 浏览: 74
可以使用QLabel控件来显示文本,然后在槽函数中使用setText()方法来设置文本内容。具体代码如下:
```python
from PySide2.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QPushButton
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.label = QLabel("Hello, World!")
self.button = QPushButton("Change Text")
self.button.clicked.connect(self.change_text)
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.button)
self.setLayout(layout)
def change_text(self):
self.label.setText("New Text")
if __name__ == "__main__":
app = QApplication([])
widget = MyWidget()
widget.show()
app.exec_()
```
在这个例子中,我们创建了一个QWidget窗口,其中包含一个QLabel控件和一个QPushButton控件。当按钮被点击时,会调用change_text()槽函数,该函数会将QLabel控件的文本内容设置为"New Text"。