我正在创建一个GUI,并且我有一个带有不同M的窗口,并且希望能够通过单击“添加新M”在M列表的中间添加M。 但是,这意味着我必须更新所有值并在由indexnr确定的列表中插入新的M。 我想知道如何更新窗口? 该窗口由Controll类运行。
我尝试了self.update(),但是它不起作用。
这是我的代码(为了简化起见,已删除很多,但可重现)。保留一个窗口,以获取更多见识。
import sys
from PyQt5 import QtCore, QtWidgets
class WindowTwo(QtWidgets.QWidget):
switch_window = QtCore.pyqtSignal()
def __init__(self, M, S):
QtWidgets.QWidget.__init__(self)
self.S = S
self.M = M
self.setupUi ( self )
self.setWindowTitle ( 'M' )
def setupUi(self, CM):
CM.setEnabled ( True )
CM.setFocusPolicy ( QtCore.Qt.TabFocus )
layout = QtWidgets.QGridLayout()
self.checkBoxM = []
self.lineEditColor = []
self.lineEditDescription = []
for i in range(0,self.M+1):
if (i==0):
layout.addWidget(QtWidgets.QLabel("Ms: " ),1,0)
layout.addWidget(QtWidgets.QLabel("check M: " ),2,i)
layout.addWidget(QtWidgets.QLabel("Color: " ),3,i)
layout.addWidget(QtWidgets.QLabel("Description: " ),4,i)
else:
#INCLUDE MASTERS checkbox FIXME make configs disabled if not checked and checked by default
self.checkBoxM.append(QtWidgets.QCheckBox (self) )
self.lineEditColor.append (QtWidgets.QLineEdit (self) )
self.lineEditColor[i-1].setText ('MM')
#lineEditDescription
self.lineEditDescription.append (QtWidgets.QLineEdit (self) )
self.lineEditDescription[i-1].setText('descr: ' )
layout.addWidget( QtWidgets.QLabel ( 'M' ), 1, i )
layout.addWidget ( self.checkBoxM [ i-1 ], 2, i )
layout.addWidget ( self.lineEditColor [ i-1 ], 3, i )
layout.addWidget ( self.lineEditDescription[i-1], 4, i)
layout.addWidget(QtWidgets.QLabel("M: " ),5,0)
layout.addWidget(QtWidgets.QLabel("S: " ),5,1)
self.QAddMButton = QtWidgets.QPushButton ( "+: Add another M" )
self.QuitButton = QtWidgets.QPushButton ( "Quit" )
self.QContinueButton = QtWidgets.QPushButton ( "Continue" )
self.QuitButton.clicked.connect ( CM.close )
self.QContinueButton.clicked.connect( lambda: self.windowtwo(self.M))
self.QAddMButton.clicked.connect ( self.addNewM )
layout.addWidget ( self.QuitButton, 10, 1 )
layout.addWidget ( self.QContinueButton, 10, 2 )
layout.addWidget ( self.QAddMButton, 0, self.M+1)
self.setLayout ( layout )
def addNewM(self):
self.indexnr = self.M
text, self.okPressed = QtWidgets.QInputDialog.getText(None, "Get text", "Index:", QtWidgets.QLineEdit.Normal, "")
if self.okPressed:
if text=='':
self.indexnr = self.M
self.M+=1
#Want to update
else:
self.indexnr = int(text)
self.M+=1
#Want to update
else:
return
def windowtwo( self, M):
self.M = M
self.switch_window.emit()
class MS(QtWidgets.QWidget):
switch_window = QtCore.pyqtSignal(int)
def __init__(self, M, S):
QtWidgets.QWidget.__init__(self)
self.S = S
self.M = M
self.setupUi ( self )
self.setWindowTitle ( 'M' )
def setupUi(self, CM):
CM.setEnabled ( True )
CM.setFocusPolicy ( QtCore.Qt.TabFocus )
layout = QtWidgets.QGridLayout()
layout.addWidget(QtWidgets.QLabel("M: " ),0,0)
layout.addWidget(QtWidgets.QLabel("S: " ),0,1)
self.QWindowTwoButton = QtWidgets.QPushButton ( ' Go to other window')
self.QuitButton = QtWidgets.QPushButton ( "Quit" )
self.QContinueButton = QtWidgets.QPushButton ( "Continue" )
self.QuitButton.clicked.connect ( CM.close )
self.QWindowTwoButton.clicked.connect( lambda b=0, a= 400 : self.windowms(a))
layout.addWidget( self.QuitButton, 10, 1 )
layout.addWidget ( self.QContinueButton, 10, 2 )
layout.addWidget ( self.QWindowTwoButton, 10, 3 )
self.setLayout ( layout )
def windowms( self, a):
a = 100
self.switch_window.emit(a)
class Controller:
def __init__(self):
self.M = 5
self.S = 7
self.A = 8
# pass
def show_window_two(self):
self.window_two = WindowTwo(self.M, self.S)
self.window_two.switch_window.connect(self.show_window_three)
self.window_two.show()
def show_window_three(self):
try:
self.A = self.window_four.A
except:
pass
self.window_three = MS(self.M, self.S)
self.mscrollArea = QtWidgets.QScrollArea()
self.mscrollArea.setWidget(self.window_three)
self.mscrollArea.setDisabled(0)
self.window_three.switch_window.connect(self.show_window_four)
self.window_three.QuitButton.clicked.connect(self.mscrollArea.close)
self.mscrollArea.show()
self.mscrollArea.resize(700, 500)
self.mscrollArea.setWindowTitle("MS")
self.window_two.close()
try:
self.window_four.close()
except:
pass
def show_window_four(self, a):
if (a == 100):
self.window_four = WindowTwo(self.M, self.S)
self.window_four.switch_window.connect(self.show_window_three)
self.mscrollArea.setDisabled(1)
self.window_four.QuitButton.clicked.connect(lambda: self.window_four.close)
self.window_four.QuitButton.clicked.connect(lambda: self.mscrollArea.setDisabled(0))
self.window_four.show()
#Here is an else if a is other values to open other windows
def main():
app = QtWidgets.QApplication(sys.argv)
controller = Controller()
controller.show_window_two()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
请让我知道是否还要添加其他内容。