我正在尝试将属性从 QWidget 类“发送”到另一个 QWidget 类。
在下面的示例中,我尝试将属于“Widget1”类的 QLineEdit“self.edit”的文本设置为属于“Widget2”类的 QLabel“self.label”的文本。
此尝试是在函数“setLabel”中进行的。我无法弄清楚的部分是“Widget2.label.setText(text)”
在函数的类中拥有一个类...我有点困惑如何实现这一点...
import sys
from PySide2.QtWidgets import (QApplication, QHBoxLayout, QVBoxLayout, QWidget, QPushButton, QLabel, QLineEdit)
class Main_UI(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
layout = QVBoxLayout()
widget1 = Widget1()
widget2 = Widget2()
layout.addWidget(widget1)
layout.addWidget(widget2)
self.setLayout(layout)
self.show()
class Widget1(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
layout = QHBoxLayout()
self.edit = QLineEdit("")
button = QPushButton("Set value")
button.clicked.connect(self.setLabel)
layout.addWidget(self.edit)
layout.addWidget(button)
self.setLayout(layout)
def setLabel(self):
text = self.edit.text()
Widget2.label.setText(text)
class Widget2(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
layout = QHBoxLayout()
self.label = QLabel("")
layout.addWidget(self.label)
self.setLayout(layout)
def main():
app = QApplication(sys.argv)
ex = Main_UI()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
任何帮助将不胜感激,如果我的示例或解释不清楚,我将提供进一步的解释。
叮当猫咪
相关分类