链接两个QMainWindow pyqt5

时间:2018-10-20 09:30:04

标签: python-3.x pyqt5

人们正在尝试编写一个程序,该程序从一个窗口获取详细信息并将其导入到另一个窗口的配置文件中。我希望它在同一应用程序上...我所看到的只是qdialog类,我不想使用它

am从第一个窗口获取数据,然后将其导入第二个窗口

这是我的第一个代码

def loader(self):       
    widget = QWidget()
    self.setCentralWidget(widget)
    #layouts
    self.layout = QFormLayout()
    self.descriLayout = QVBoxLayout()
    self.buttonLayout = QHBoxLayout()

    #QFormLayout dealz
    self.name = QLabel('name')
    items = ['male' , 'female' , 'none']
    self.sexchooser = QComboBox()
    for item in items:
        self.sexchooser.addItem(item)

    self.age = QLabel('age')
    self.optcourse = QLabel('Opted Course')
    self.nameEdit = QLineEdit()
    #self.nameEdit.editingFinished()
    self.nameEdit.setPlaceholderText('enter name here')
    self.coursEdit = QLineEdit()
    self.coursEdit.setPlaceholderText('Mt || Ph || St')
    self.sexLabel = QLabel('sex')
    #age selector
    self.ageSelector = QComboBox()
    for x in range(18 , 40):
        self.ageSelector.addItem(str(x))
    self.descriptor = QPlainTextEdit()

    self.descriptor.setPlaceholderText('describe yourself here')
    self.descriptor.setUndoRedoEnabled(True)
    self.layout.addRow(self.name , self.nameEdit)
    self.layout.addRow(self.optcourse , self.coursEdit)
    self.layout.addRow(QLabel('sex') , self.sexchooser)
    self.layout.addRow(QLabel('Age') , self.ageSelector)

    #buttons dealz
    self.SubmitButton = QPushButton('&Submit')
    self.SubmitButton.clicked.connect(self.detailer)
    self.cancelButton = QPushButton("Can&cel")
    self.cancelButton.clicked.connect(self.close)
    self.buttonLayout.addWidget(self.SubmitButton)
    self.buttonLayout.addWidget(self.cancelButton)


    self.descriLayout.addLayout(self.layout)
    self.descriLayout.addWidget(self.descriptor)
    self.descriLayout.addLayout(self.buttonLayout)

    self.show()
    widget.setLayout(self.descriLayout)
    self.setMinimumSize(300 , 350)
    self.setMaximumSize(300 , 350)
    self.setWindowTitle('DETAILS')
def detailer(self):
    #the second window called here

预先感谢

1 个答案:

答案 0 :(得分:0)

尝试一下:

import sys
from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import *


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.loader()

    def loader(self):       
        widget = QWidget()
        self.setCentralWidget(widget)
        #layouts
        self.layout = QFormLayout()
        self.descriLayout = QVBoxLayout()
        self.buttonLayout = QHBoxLayout()

        #QFormLayout dealz
        self.name = QLabel('name')
        items = ['male' , 'female' , 'none']
        self.sexchooser = QComboBox()
        for item in items:
            self.sexchooser.addItem(item)

        self.age = QLabel('age')
        self.optcourse = QLabel('Opted Course')
        self.nameEdit = QLineEdit()
        #self.nameEdit.editingFinished()
        self.nameEdit.setPlaceholderText('enter name here')
        self.coursEdit = QLineEdit()
        self.coursEdit.setPlaceholderText('Mt || Ph || St')
        self.sexLabel = QLabel('sex')
        #age selector
        self.ageSelector = QComboBox()
        for x in range(18 , 40):
            self.ageSelector.addItem(str(x))
        self.descriptor = QPlainTextEdit()

        self.descriptor.setPlaceholderText('describe yourself here')
        self.descriptor.setUndoRedoEnabled(True)
        self.layout.addRow(self.name , self.nameEdit)
        self.layout.addRow(self.optcourse , self.coursEdit)
        self.layout.addRow(QLabel('sex') , self.sexchooser)
        self.layout.addRow(QLabel('Age') , self.ageSelector)

        #buttons dealz
        self.SubmitButton = QPushButton('&Submit')
        self.SubmitButton.clicked.connect(self.detailer)
        self.cancelButton = QPushButton("Can&cel")
        self.cancelButton.clicked.connect(self.close)
        self.buttonLayout.addWidget(self.SubmitButton)
        self.buttonLayout.addWidget(self.cancelButton)


        self.descriLayout.addLayout(self.layout)
        self.descriLayout.addWidget(self.descriptor)
        self.descriLayout.addLayout(self.buttonLayout)

        self.show()
        widget.setLayout(self.descriLayout)
        self.setMinimumSize(300 , 350)
        self.setMaximumSize(300 , 350)
        self.setWindowTitle('DETAILS')

    def detailer(self):
        print("#the second window called here")
        self.statusBar().showMessage("Switched to window 2")

        valueText = " {} \n {} \n {} \n {} \n {}"\
                    .format(self.nameEdit.text(), 
                              self.coursEdit.text(),
                              self.sexchooser.currentText(),
                              self.ageSelector.currentText(),
                              self.descriptor.toPlainText())

        self.cams = Window2(valueText, self) 
        self.cams.show()        


class Window2(QDialog):
    def __init__(self, value, parent=None):
        super().__init__(parent)
        self.setGeometry(750, 100, 300, 350)
        self.parent = parent
        self.setWindowTitle('Window2')
        self.setWindowIcon(self.style().standardIcon(QStyle.SP_FileDialogInfoView))

        label1 = QLabel(value)
        self.button = QPushButton()
        self.button.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding)
        self.button.setIcon(self.style().standardIcon(QStyle.SP_ArrowLeft))
        self.button.setIconSize(QSize(200, 200))

        layoutV = QVBoxLayout()
        self.pushButton = QPushButton(self)
        self.pushButton.setStyleSheet('background-color: rgb(0,0,255); color: #fff')
        self.pushButton.setText('Click me!')
        self.pushButton.clicked.connect(self.goMainWindow)
        layoutV.addWidget(self.pushButton)

        layoutH = QHBoxLayout()
        layoutH.addWidget(label1)
        layoutH.addWidget(self.button)
        layoutV.addLayout(layoutH)
        self.setLayout(layoutV)

    def goMainWindow(self):
        self.parent.show()
        self.close()         


if __name__=='__main__':
    app = QApplication(sys.argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec_())        

enter image description here