我使用Qt设计器在Python中制作了一个GUI,并将其转换为Pyqt5,我的GUI的体系结构是:一个主窗口,当我单击一个菜单栏项时,中央小部件被另一个QtWidget(具有另一个类)取代,问题是当我更换中央小部件时,按钮不起作用,并且如果我直接执行此QtWidget,它将起作用。
这是我的主要代码:
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtCore import QCoreApplication
import os
import testModelC
class Ui_DzSenti(object):
def setupUi(self, DzSenti):
DzSenti.setObjectName("DzSenti")
DzSenti.resize(900, 500)
self.centralwidget = QtWidgets.QWidget(DzSenti)
self.centralwidget.setObjectName("centralwidget")
#***************************************************************************************************
self.centralwidget = QtWidgets.QWidget(DzSenti)
self.centralwidget.setObjectName("centralwidget")
DzSenti.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(DzSenti)
self.menubar.setEnabled(True)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
self.menubar.setNativeMenuBar(False)
self.menubar.setObjectName("menubar")
self.menuTeste = QtWidgets.QMenu(self.menubar)
self.menuTeste.setObjectName("menuTeste")
DzSenti.setMenuBar(self.menubar)
self.actionTeste_de_mod_le = QtWidgets.QAction(DzSenti)
self.actionTeste_de_mod_le.setObjectName("actionTeste_de_mod_le")
self.menuTeste.addAction(self.actionTeste_de_mod_le)
self.menubar.addAction(self.menuTeste.menuAction())
self.retranslateUi(DzSenti)
QtCore.QMetaObject.connectSlotsByName(DzSenti)
def retranslateUi(self, DzSenti):
_translate = QtCore.QCoreApplication.translate
DzSenti.setWindowTitle(_translate("DzSenti", "DzSenti"))
self.menuTeste.setTitle(_translate("DzSenti", "Teste"))
self.actionTeste_de_mod_le.setText(_translate("DzSenti", "Teste de modèle"))
self.actionTeste_de_mod_le.triggered.connect(self.move_testeModel)
def move_testeModel(self):
Form = QtWidgets.QWidget()
ui = testModelC.Ui_testeModel()
ui.setupUi(Form)
DzSenti.setCentralWidget(Form)
#Method 2 doesn't work either by calling the qtwidget in another window (dialog)
'''testModel1 = QtWidgets.QWidget()
ui = testModelC.Ui_testeModel()
ui.setupUi(testModel1)
self.dialog = testModel1
self.dialog.show()'''
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
DzSenti = QtWidgets.QMainWindow()
ui = Ui_DzSenti()
ui.setupUi(DzSenti)
DzSenti.show()
sys.exit(app.exec_())
这是我的QtWidget代码之一:
# -*- coding: utf-8 -*-
from PyQt5.QtCore import pyqtSlot
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox
class Ui_testeModel(object):
def setupUi(self, testeModel):
testeModel.setObjectName("testeModel")
testeModel.resize(815, 460)
self.texteTeste_plainTextEdit = QtWidgets.QPlainTextEdit(testeModel)
self.texteTeste_plainTextEdit.setGeometry(QtCore.QRect(70, 80, 721, 201))
self.texteTeste_plainTextEdit.setObjectName("texteTeste_plainTextEdit")
self.label_13 = QtWidgets.QLabel(testeModel)
self.label_13.setGeometry(QtCore.QRect(30, 50, 201, 17))
self.label_13.setObjectName("label_13")
self.label_9 = QtWidgets.QLabel(testeModel)
self.label_9.setGeometry(QtCore.QRect(200, 10, 441, 31))
font = QtGui.QFont()
font.setPointSize(20)
self.label_9.setFont(font)
self.label_9.setAlignment(QtCore.Qt.AlignCenter)
self.label_9.setObjectName("label_9")
self.Button_vider = QtWidgets.QPushButton(testeModel)
self.Button_vider.setGeometry(QtCore.QRect(500, 350, 181, 41))
self.Button_vider.setObjectName("Button_voirPrecision")
self.Button_categoriser = QtWidgets.QPushButton(testeModel)
self.Button_categoriser.setGeometry(QtCore.QRect(180, 350, 171, 41))
self.Button_categoriser.setObjectName("Button_categoriser")
self.Button_categoriser.clicked.connect(self.wrapper)
self.Button_vider.clicked.connect(self.wrapper2)
self.retranslateUi(testeModel)
QtCore.QMetaObject.connectSlotsByName(testeModel)
def retranslateUi(self, testeModel):
_translate = QtCore.QCoreApplication.translate
testeModel.setWindowTitle(_translate("testeModel", "Tester le modèle"))
self.label_13.setText(_translate("testeModel", "Entrer un texte :"))
self.label_9.setText(_translate("testeModel", "Tester le modèle"))
self.Button_vider.setText(_translate("testeModel", "Vider"))
self.Button_categoriser.setText(_translate("testeModel", "Catégoriser"))
def wrapper(self):
self.texteTeste_plainTextEdit.setPlainText("Hello it work")
def wrapper2(self):
self.texteTeste_plainTextEdit.setPlainText("")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
testeModel = QtWidgets.QWidget()
ui = Ui_testeModel()
ui.setupUi(testeModel)
testeModel.show()
sys.exit(app.exec_())
最佳答案
我实际上不知道这里发生了什么..但是重写move_testeModel
实际上有帮助:
def move_testeModel(self):
try:
Form = QtWidgets.QWidget()
ui = Ui_testeModel()
ui.setupUi(Form)
ui.Button_categoriser.clicked.connect(lambda :ui.wrapper())
DzSenti.setCentralWidget(Form)
except Exception as e:
print(e)
这将两次连接
Button_categoriser
单击的信号,并神奇地修复第二个按钮信号。我认为这可能与
retranslateUI
函数有关。希望其他人可以指出真正的问题是什么。