亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在兩個類“QWidget”之間共享屬性

在兩個類“QWidget”之間共享屬性

溫溫醬 2024-01-16 10:42:28
我正在嘗試將屬性從 QWidget 類“發送”到另一個 QWidget 類。在下面的示例中,我嘗試將屬于“Widget1”類的 QLineEdit“self.edit”的文本設置為屬于“Widget2”類的 QLabel“self.label”的文本。此嘗試是在函數“setLabel”中進行的。我無法弄清楚的部分是“Widget2.label.setText(text)”在函數的類中擁有一個類...我有點困惑如何實現這一點...import sysfrom 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()任何幫助將不勝感激,如果我的示例或解釋不清楚,我將提供進一步的解釋。
查看完整描述

1 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

您可以使用自定義信號來完成此操作。


import sys

from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QVBoxLayout, QWidget, QPushButton, QLabel, QLineEdit)

from PyQt5 import QtCore



class Main_UI(QWidget):

? ? def __init__(self, parent=None):

? ? ? ? super(Main_UI, self).__init__(parent)

? ? ? ? self.initUI()

? ??

? ? def initUI(self):

? ? ? ? layout = QVBoxLayout()

? ? ? ? widget1 = Widget1()

? ? ? ? widget2 = Widget2()

? ? ? ? layout.addWidget(widget1)

? ? ? ? layout.addWidget(widget2)

? ? ? ? self.setLayout(layout)

? ? ? ??

? ? ? ? widget1.button_signal.connect(widget2.label.setText)? # Connecting the label to the custom signal.

? ? ? ??

? ? ? ? self.show()



class Widget1(QWidget):

? ? button_signal = QtCore.pyqtSignal(str)? # Creating a signal.

? ??

? ? def __init__(self, parent=None):

? ? ? ? super(Widget1, self).__init__(parent)

? ? ? ? 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):

? ? ? ? """Emit button signal with text.

? ? ? ??

? ? ? ? This could have been solved with a lambda.

? ? ? ??

? ? ? ? """? ? ? ??

? ? ? ? self.button_signal.emit(self.edit.text())? # Emitting Signal.



class Widget2(QWidget):

? ? def __init__(self, parent=None):

? ? ? ? super(Widget2, self).__init__(parent)

? ? ? ? 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()

查看完整回答
反對 回復 2024-01-16
  • 1 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號