从其他Dialog将字符串设置为comboBox

时间:2017-12-17 21:06:04

标签: python python-3.x pyqt pyqt5 qcombobox

我在Pyqt5&中做了两个GUI。 Qt设计师。第一个GUI是在comboBox中显示人名。第二个GUI将通过单击+按钮打开,然后用户可以写下我以后需要的名称,年龄和工作。

我的问题是,如何从2. GUI获取新名称并将其设置在1. GUI的comboBox中。我在下面的代码中尝试过,但它不起作用。

整个代码:

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

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(363, 165)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(30, 30, 68, 19))
        self.label.setObjectName("label")
        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(120, 30, 131, 25))
        self.comboBox.setObjectName("comboBox")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(280, 30, 41, 34))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 363, 31))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "person"))
        self.pushButton.setText(_translate("MainWindow", "+"))

    def InputDialog1(self):
        self.Dialog = QtWidgets.QDialog()
        self.ui=Ui_Dialog()
        self.ui.setupUi(self.Dialog)
        self.Dialog.setWindowTitle('Add Infos')
        self.Dialog.show()

class Person_data(object):
    def __init__(self,parent=None):
         self.Person_1 = {'-':[0,0]}

class MainWindow(QMainWindow, Ui_MainWindow,Person_data):

    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent=parent)
        Person_data.__init__(self, parent=parent)
        self.setupUi(self)
        self.initMe()

    def initMe(self):
        self.pushButton.clicked.connect(self.InputDialog1)

        Person_list={}
        for i in self.Person_1.keys():
                Person_list[i] = ''
                Person_list_2 = list(Person_list)
        print(Person_list_2)
        self.comboBox.addItems(Person_list_2)



class Ui_Dialog(Person_data):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(344, 231)
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(170, 160, 112, 34))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(Dialog)
        self.pushButton_2.setGeometry(QtCore.QRect(30, 160, 112, 34))
        self.pushButton_2.setObjectName("pushButton_2")
        self.gridLayoutWidget = QtWidgets.QWidget(Dialog)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(30, 20, 271, 121))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.gridLayout.addWidget(self.lineEdit_2, 1, 1, 1, 1)
        self.lineEdit = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.lineEdit.setObjectName("lineEdit")
        self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
        self.lineEdit_3 = QtWidgets.QLineEdit(self.gridLayoutWidget)
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.gridLayout.addWidget(self.lineEdit_3, 2, 1, 1, 1)
        self.label_2 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2, 2, 0, 1, 1)
        self.label_3 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_3.setObjectName("label_3")
        self.gridLayout.addWidget(self.label_3, 1, 0, 1, 1)
        self.label = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        self.pushButton.clicked.connect(self.okpress)
        self.pushButton_2.clicked.connect(Dialog.close)

    def okpress(self):
        aa1=float(self.lineEdit_2.text());aa2=float(self.lineEdit_3.text())
        self.Person_1[str(self.lineEdit.text())] = [aa1,aa2]
        i = True
        while i == True:
            bb = MainWindow()
            Person_list={}
            for i in self.Person_1.keys():
                Person_list[i] = ''
                Person_list_2 = list(Person_list)
            print(Person_list_2)
            bb.comboBox.addItems(Person_list_2)
            i=False  #just to exit while


    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.pushButton.setText(_translate("Dialog", "add"))
        self.pushButton_2.setText(_translate("Dialog", "Cancel"))
        self.label_2.setText(_translate("Dialog", "Age"))
        self.label_3.setText(_translate("Dialog", "Job"))
        self.label.setText(_translate("Dialog", "Name"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.setWindowTitle('Main Person Windows')
    w.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

不建议您修改Qt Designer生成的代码,因为除了弄乱代码之外,您还可能有意外的行为。从我的角度来看,适当的是创建从适当的小部件继承并使用QtDesigner设计的类,所以我的答案包括重构代码以使其更具可读性。

在你的代码中分析我发现你将继承与共享数据的想法混为一谈,当它从一个类继承它在每个类中创建相似但不相等的元素时,即使这些类的每个对象都创建了类似的对象。但是它们不一样,不是相同的变量所以它们指向不同的内存空间,在你的情况下你认为当Ui_Dialog和MainWindow继承自Person_data,那么两个类中的变量Person_1是相同的,实际上它不是。

您的代码的另一个问题是验证,必须尝试用户放置适当的值并且必要时必须与某些元素交互,例如,如果文本为空或不是数字,则转换为float会导致问题,只有在正确填写所有字段时才应启用添加按钮,因为此Qt提供了诸如QIntValidator之类的类。

QDialog是一个专门用于表单的类,我们应尽量使用exec_(),因为如果表单被接受,它会返回,但为此,你必须在按钮时使用accept和reject方法是合适的。

代码:

class PersonDialog(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, *args, **kwargs):
        QtWidgets.QDialog.__init__(self, *args, **kwargs)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.accept)
        self.pushButton_2.clicked.connect(self.reject)
        self.lineEdit_3.setValidator(QtGui.QIntValidator(0, 100))
        self.lineEdit.textChanged.connect(self.enableButtonAccept)
        self.lineEdit_2.textChanged.connect(self.enableButtonAccept)
        self.lineEdit_3.textChanged.connect(self.enableButtonAccept)
        self.enableButtonAccept()

    def enableButtonAccept(self):
        if self.lineEdit.text() != "" and self.lineEdit_2.text() != "" and self.lineEdit_3.text() != "":
            self.pushButton.setEnabled(True)
        else:
            self.pushButton.setEnabled(False)

    def getPerson(self):
        values = self.lineEdit.text(), self.lineEdit_2.text(), int(self.lineEdit_3.text())
        self.lineEdit.clear()
        self.lineEdit_2.clear()
        self.lineEdit_3.clear()
        return values

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)
        self.Person_1 = {}
        self.initMe()

    def initMe(self):
        self.personDialog = PersonDialog(self)
        self.pushButton.clicked.connect(self.InputDialog1)

    def InputDialog1(self):
        if self.personDialog.exec_() == PersonDialog.Accepted:
            name, job, edad = self.personDialog.getPerson()
            self.Person_1[name] = [job, edad]
            self.comboBox.addItem(name)