私はPyqt5で2つのGUIを作成しました。& Qt Designer。最初のGUIは、コンボボックスに人名を表示することです。 2番目の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_())
感謝。私はちょうどPythonの正しい方法でコーディングする方法を学んでおり、あなたの助言は本当に役に立ちます。 – Ahmad
私は質問があります:.__ init __(self、* args、** kwargs)同じように.__ init __(self、parent = parent)?? – Ahmad