我有两个小部件:第一个(Form1)有一个组合框。如果我使用在控制台上显示的这个组合框。 (#This很好) 但我想在第二个小部件(Form2)上使用此变量,但我不会显示它。 (#这很糟糕)我怎样才能使用这个"文本"我的Form2小部件上的变量?
import sys
from functools import partial
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import QtGui, QtCore
from math import sqrt
from time import gmtime, strftime
text = ""
class Form1(QWidget):
showForm2Signal = pyqtSignal()
def __init__(self, parent=None):
super(Form1, self).__init__(parent)
self.comboBox = QtGui.QComboBox(self)
self.comboBox.addItem("Text 1")
self.comboBox.addItem("Text 2")
self.comboBox.addItem("Text 3")
self.comboBox.addItem("Text 4")
self.comboBox.addItem("Text 5")
self.comboBox.addItem("Text 6")
self.comboBox.addItem("Text 7")
self.comboBox.move(20, 20)
self.comboBox.resize(360,30)
self.comboBox.currentIndexChanged.connect(self.selectionchange)
layout = QVBoxLayout(self)
ok_button = QtGui.QPushButton("OK", self)
ok_button.resize(ok_button.minimumSizeHint())
ok_button.move(0,340)
ok_button.resize(400,60)
ok_button.setStyleSheet("color: #25373D; background-color: #71BA51; font-size: 16pt; font-weight: bold;")
ok_button.clicked.connect(self.showForm2Signal.emit)
def selectionchange(self,i):
text = self.comboBox.currentText()
#This is good
print text
class Form2(QWidget):
showForm1Signal = pyqtSignal()
def __init__(self, parent=None):
super(Form2, self).__init__(parent)
#This is bad
print text
self.backButton = QPushButton("Back", self)
self.backButton.clicked.connect(self.showForm1Signal.emit)
class MainWidget(QWidget):
def __init__(self, parent=None):
super(MainWidget, self).__init__(parent)
self.stack = QStackedWidget()
layout = QVBoxLayout(self)
layout.addWidget(self.stack)
layout.setContentsMargins(0, 0, 0, 0)
self.setGeometry(0, 0, 400, 400)
self.setWindowTitle("TEST")
self.form1 = Form1(self)
self.form2 = Form2(self)
self.stack.addWidget(self.form1)
self.stack.addWidget(self.form2)
self.form1.showForm2Signal.connect(partial(self.stack.setCurrentWidget,self.form2))
self.form2.showForm1Signal.connect(partial(self.stack.setCurrentWidget,self.form1))
self.stack.setCurrentWidget(self.form1)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWidget()
w.show()
app.exec_()
sys.exit()