我已经使用Qt Designer设计了2 ui,并将它们导入到我用作主程序的新.py文件中。一个是主窗口,另一个是自定义弹出对话框。我能够毫无困难地运行MainWindow
。我也可以运行Popup (QDialog, Modal)
但是一旦显示,小部件就不会响应
很抱歉这个问题,但在线上看之后,我无法让它工作,最大的问题是编译器没有向我显示任何错误。代码运行没有麻烦,但小部件不会更新/响应
如果代码有点乱,我很抱歉,我上个月学习了Python,上周学了PyQT。
以下是该计划的代码:
import sys
import _pickle as cPickle
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget,QVBoxLayout
from PyQt5 import QtCore, QtGui, QtWidgets
from PedidoUI import Ui_MainWindow
from Emergente1 import Ui_Dialog
#I define a PopUp Dialog
class PopUp(Ui_Dialog):
"""docstring for PopUp"""
def __init__(self,Dialog):
Ui_Dialog.__init__(self)
self.setupUi(Dialog)
self.botonAceptar.clicked.connect(self.AgregarItems)
#It should add the string "Prueba" as an Item to the list.
def AgregarItems(self):
self.listWidget.addItem("Prueba")
#This is the Main Class
class ProgramaPedido(Ui_MainWindow):
def __init__(self, MainWindow):
Ui_MainWindow.__init__(self)
self.setupUi(MainWindow)
self.Dialog = QtWidgets.QDialog()
self.ui = PopUp(self.Dialog)
self.ui.setupUi(self.Dialog)
self.toolButton.clicked.connect(self.open_dialog)
#I call the PopUp
def open_dialog(self):
self.Dialog.exec_()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
prog = ProgramaPedido(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
以下是MainWindow的用户界面:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(642, 912)
MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)
MainWindow.setUnifiedTitleAndToolBarOnMac(False)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(80, 10, 133, 20))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 10, 55, 19))
font = QtGui.QFont()
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(20, 510, 81, 21))
font = QtGui.QFont()
font.setPointSize(15)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.botonEnviar = QtWidgets.QPushButton(self.centralwidget)
self.botonEnviar.setGeometry(QtCore.QRect(440, 540, 91, 41))
self.botonEnviar.setObjectName("botonEnviar")
self.botonDel = QtWidgets.QPushButton(self.centralwidget)
self.botonDel.setGeometry(QtCore.QRect(540, 540, 41, 41))
self.botonDel.setObjectName("botonDel")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(450, 610, 61, 21))
font = QtGui.QFont()
font.setPointSize(12)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(520, 610, 69, 22))
self.comboBox.setObjectName("comboBox")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(440, 640, 181, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.botonAdd = QtWidgets.QPushButton(self.centralwidget)
self.botonAdd.setGeometry(QtCore.QRect(230, 10, 75, 23))
self.botonAdd.setObjectName("botonAdd")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(20, 840, 101, 16))
self.label_4.setObjectName("label_4")
self.BotonAdj = QtWidgets.QPushButton(self.centralwidget)
self.BotonAdj.setGeometry(QtCore.QRect(20, 470, 75, 23))
self.BotonAdj.setObjectName("BotonAdj")
self.botonAdjT = QtWidgets.QPushButton(self.centralwidget)
self.botonAdjT.setGeometry(QtCore.QRect(100, 470, 81, 23))
self.botonAdjT.setObjectName("botonAdjT")
self.BotonQuit = QtWidgets.QPushButton(self.centralwidget)
self.BotonQuit.setGeometry(QtCore.QRect(190, 470, 81, 23))
self.BotonQuit.setObjectName("BotonQuit")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(10, 80, 621, 381))
self.listWidget.setObjectName("listWidget")
self.listWidget_2 = QtWidgets.QListWidget(self.centralwidget)
self.listWidget_2.setGeometry(QtCore.QRect(10, 540, 421, 291))
self.listWidget_2.setObjectName("listWidget_2")
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(10, 50, 171, 22))
self.comboBox_2.setObjectName("comboBox_2")
self.toolButton = QtWidgets.QToolButton(self.centralwidget)
self.toolButton.setGeometry(QtCore.QRect(190, 50, 21, 21))
self.toolButton.setObjectName("toolButton")
self.toolButton_2 = QtWidgets.QToolButton(self.centralwidget)
self.toolButton_2.setGeometry(QtCore.QRect(590, 610, 21, 21))
self.toolButton_2.setObjectName("toolButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 642, 21))
self.menubar.setObjectName("menubar")
self.menuConfiguracion = QtWidgets.QMenu(self.menubar)
self.menuConfiguracion.setObjectName("menuConfiguracion")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionCorreo = QtWidgets.QAction(MainWindow)
self.actionCorreo.setObjectName("actionCorreo")
self.actionPesta_as = QtWidgets.QAction(MainWindow)
self.actionPesta_as.setObjectName("actionPesta_as")
self.menubar.addAction(self.menuConfiguracion.menuAction())
self.label.setBuddy(self.lineEdit)
self.label_3.setBuddy(self.comboBox)
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", "Articulo"))
self.label_2.setText(_translate("MainWindow", "Pedido:"))
self.botonEnviar.setText(_translate("MainWindow", "Enviar"))
self.botonDel.setText(_translate("MainWindow", "B"))
self.label_3.setText(_translate("MainWindow", "Destino:"))
self.botonAdd.setText(_translate("MainWindow", "Agregar"))
self.label_4.setText(_translate("MainWindow", "Nahuel Varela Blanco"))
self.BotonAdj.setText(_translate("MainWindow", "Adjuntar"))
self.botonAdjT.setText(_translate("MainWindow", "Adjuntar Todo"))
self.BotonQuit.setText(_translate("MainWindow", "Quitar"))
self.toolButton.setText(_translate("MainWindow", "..."))
self.toolButton_2.setText(_translate("MainWindow", "..."))
self.menuConfiguracion.setTitle(_translate("MainWindow", "Configuracion"))
self.actionCorreo.setText(_translate("MainWindow", "Correo"))
self.actionPesta_as.setText(_translate("MainWindow", "Listas"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
以下是PopUp对话框的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(356, 277)
self.listWidget = QtWidgets.QListWidget(Dialog)
self.listWidget.setGeometry(QtCore.QRect(40, 10, 256, 192))
self.listWidget.setObjectName("listWidget")
self.botonAceptar = QtWidgets.QPushButton(Dialog)
self.botonAceptar.setGeometry(QtCore.QRect(270, 250, 75, 23))
self.botonAceptar.setObjectName("botonAceptar")
self.botonAgr = QtWidgets.QPushButton(Dialog)
self.botonAgr.setGeometry(QtCore.QRect(40, 210, 75, 23))
self.botonAgr.setObjectName("botonAgr")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(120, 210, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.botonBor = QtWidgets.QPushButton(Dialog)
self.botonBor.setGeometry(QtCore.QRect(40, 240, 75, 23))
self.botonBor.setObjectName("botonBor")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.botonAceptar.setText(_translate("Dialog", "Aceptar"))
self.botonAgr.setText(_translate("Dialog", "Agregar"))
self.botonBor.setText(_translate("Dialog", "Borrar"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
问题是您正在创建不同类的对象。 Qt Designer只提供一个填充窗口小部件的类,因此它不是窗口小部件,所以正确的方法是创建一个继承相应窗口小部件并使用该设计的类,通常该类应具有以下结构:
class Your_Widget(Widget_Class, Design_Class):
def __init__(self,*args, **kwargs):
Widget_Class.__init__(self, *args, **kwargs)
self.setupUi(self)
然后我们以下列方式使用它:
#I define a PopUp Dialog
class PopUp(QDialog, Ui_Dialog):
def __init__(self,*args, **kwargs):
QDialog.__init__(self, *args, **kwargs)
self.setupUi(self)
self.botonAceptar.clicked.connect(self.AgregarItems)
#It should add the string "Prueba" as an Item to the list.
def AgregarItems(self):
self.listWidget.addItem("Prueba")
#This is the Main Class
class ProgramaPedido(QMainWindow, Ui_MainWindow):
def __init__(self,*args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.Dialog = PopUp()
self.toolButton.clicked.connect(self.open_dialog)
#I call the PopUp
def open_dialog(self):
self.Dialog.exec_()
if __name__ == '__main__':
app = QApplication(sys.argv)
prog = ProgramaPedido()
prog.show()
sys.exit(app.exec_())