PyQt5 - 第二个窗口中的按钮在单击时不执行操作
Posted
技术标签:
【中文标题】PyQt5 - 第二个窗口中的按钮在单击时不执行操作【英文标题】:PyQt5 - button in second window is not doing action on click 【发布时间】:2020-07-13 16:30:21 【问题描述】:您好,我有 2 个 python 文件:
第一个叫'first.py',第二个叫'second.py':
我的程序应该引导用户从第一个窗口到第二个窗口。
问题是,第一个窗口的按钮在单击时工作正常。但在第二个窗口中,即使按钮“连接”到某个功能,它们也不起作用。
first.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit
import sys
from second import Ui_second
class Ui_mainwindow(object):
def setupUi(self, mainwindow):
mainwindow.setObjectName("mainwindow")
mainwindow.resize(1039, 717)
self.centralwidget = QtWidgets.QWidget(mainwindow)
self.centralwidget.setObjectName("centralwidget")
self.nextbutton1 = QtWidgets.QPushButton(self.centralwidget)
self.nextbutton1.setGeometry(QtCore.QRect(550, 380, 80, 20))
font = QtGui.QFont()
font.setPointSize(9)
self.nextbutton1.setFont(font)
self.nextbutton1.setObjectName("nextbutton1")
self.patientinfo_label = QtWidgets.QLabel(self.centralwidget)
self.patientinfo_label.setGeometry(QtCore.QRect(360, 230, 191, 31))
font = QtGui.QFont()
font.setPointSize(11)
self.patientinfo_label.setFont(font)
self.patientinfo_label.setObjectName("patientinfo_label")
self.lastname_label = QtWidgets.QLabel(self.centralwidget)
self.lastname_label.setGeometry(QtCore.QRect(360, 270, 100, 31))
font = QtGui.QFont()
font.setPointSize(11)
self.lastname_label.setFont(font)
self.lastname_label.setObjectName("lastname_label")
self.lastname_input = QtWidgets.QLineEdit(self.centralwidget)
self.lastname_input.setGeometry(QtCore.QRect(460, 278, 150, 20))
self.lastname_input.setObjectName("lastname_input")
#self.lastname_input.returnPressed.connect(self.clicked)
#self.lastname_input.returnPressed.connect(self.clicked)
#self.lastname_input.returnPressed.connect(self.clicked)
self.firstname_label = QtWidgets.QLabel(self.centralwidget)
self.firstname_label.setGeometry(QtCore.QRect(360, 300, 100, 31))
font = QtGui.QFont()
font.setPointSize(11)
self.firstname_label.setFont(font)
self.firstname_label.setObjectName("firstname_label")
self.telephone_label = QtWidgets.QLabel(self.centralwidget)
self.telephone_label.setGeometry(QtCore.QRect(360, 330, 100, 31))
font = QtGui.QFont()
font.setPointSize(11)
self.telephone_label.setFont(font)
self.telephone_label.setObjectName("telephone_label")
self.welcome = QtWidgets.QLabel(self.centralwidget)
self.welcome.setGeometry(QtCore.QRect(260, 150, 471, 71))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.welcome.setFont(font)
self.welcome.setObjectName("welcome")
self.lastname_input_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lastname_input_2.setGeometry(QtCore.QRect(460, 308, 150, 20))
self.lastname_input_2.setObjectName("lastname_input_2")
self.lastname_input_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lastname_input_3.setGeometry(QtCore.QRect(460, 338, 150, 20))
self.lastname_input_3.setObjectName("lastname_input_3")
self.nextbutton1_2 = QtWidgets.QPushButton(self.centralwidget)
self.nextbutton1_2.setGeometry(QtCore.QRect(460, 380, 80, 20))
font = QtGui.QFont()
font.setPointSize(9)
self.nextbutton1_2.setFont(font)
self.nextbutton1_2.setObjectName("nextbutton1_2")
mainwindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(mainwindow)
self.statusbar.setObjectName("statusbar")
mainwindow.setStatusBar(self.statusbar)
self.retranslateUi(mainwindow)
QtCore.QMetaObject.connectSlotsByName(mainwindow)
mainwindow.setTabOrder(self.lastname_input, self.lastname_input_2)
mainwindow.setTabOrder(self.lastname_input_2, self.lastname_input_3)
mainwindow.setTabOrder(self.lastname_input_3, self.nextbutton1)
mainwindow.setTabOrder(self.nextbutton1, self.nextbutton1_2)
#self.lastname_input.returnPressed.connect(self.clicked)
#self.lastname_input.returnPressed.connect(self.clicked)
#self.lastname_input_3.returnPressed.connect(self.clicked)
self.nextbutton1.clicked.connect(self.clicked1)
def retranslateUi(self, mainwindow):
_translate = QtCore.QCoreApplication.translate
mainwindow.setWindowTitle(_translate("mainwindow", "VeriMed Tech"))
self.nextbutton1.setText(_translate("mainwindow", "Next"))
self.patientinfo_label.setText(_translate("mainwindow", "Patient Information"))
self.lastname_label.setText(_translate("mainwindow", "Last name:"))
self.firstname_label.setText(_translate("mainwindow", "First name:"))
self.telephone_label.setText(_translate("mainwindow", "Telephone:"))
self.welcome.setText(_translate("mainwindow", "Welcome to VeriMed Technologies"))
self.nextbutton1_2.setText(_translate("mainwindow", "Exit"))
def clicked1(self):
print("passed first clicked test")
mainwindow.close()
ui = Ui_second()
ui.setupUi(mainwindow)
mainwindow.show()
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_())
second.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit
import sys
class Ui_second(object):
def setupUi(self, second):
second.setObjectName("second")
second.resize(1039, 717)
self.centralwidget = QtWidgets.QWidget(second)
self.centralwidget.setObjectName("centralwidget")
self.lastname_input = QtWidgets.QLineEdit(self.centralwidget)
self.imagelabel = QtWidgets.QLabel(self.centralwidget)
self.imagelabel.setGeometry(QtCore.QRect(130, 250, 201, 141))
self.imagelabel.setText("")
self.imagelabel.setPixmap(QtGui.QPixmap("databases/images/cartoon.png"))
self.imagelabel.setScaledContents(True)
self.imagelabel.setWordWrap(False)
self.imagelabel.setObjectName("imagelabel")
self.lastname_input.setGeometry(QtCore.QRect(460, 275, 200, 30))
self.lastname_input.setObjectName("lastname_input")
self.next = QtWidgets.QPushButton(self.centralwidget)
self.next.setGeometry(QtCore.QRect(570, 430, 80, 20))
font = QtGui.QFont()
font.setPointSize(9)
self.next.setFont(font)
self.next.setObjectName("next")
self.patientinfo_label = QtWidgets.QLabel(self.centralwidget)
self.patientinfo_label.setGeometry(QtCore.QRect(360, 230, 191, 31))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.patientinfo_label.setFont(font)
self.patientinfo_label.setObjectName("patientinfo_label")
self.barcode_label = QtWidgets.QLabel(self.centralwidget)
self.barcode_label.setGeometry(QtCore.QRect(360, 270, 100, 31))
font = QtGui.QFont()
font.setPointSize(11)
self.barcode_label.setFont(font)
self.barcode_label.setObjectName("barcode_label")
self.medication_label = QtWidgets.QLabel(self.centralwidget)
self.medication_label.setGeometry(QtCore.QRect(360, 320, 100, 31))
font = QtGui.QFont()
font.setPointSize(11)
self.medication_label.setFont(font)
self.medication_label.setObjectName("medication_label")
font = QtGui.QFont()
font.setPointSize(11)
self.welcome = QtWidgets.QLabel(self.centralwidget)
self.welcome.setGeometry(QtCore.QRect(260, 150, 471, 71))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.welcome.setFont(font)
self.welcome.setObjectName("welcome")
self.exit = QtWidgets.QPushButton(self.centralwidget)
self.exit.setGeometry(QtCore.QRect(480, 430, 80, 20))
font = QtGui.QFont()
font.setPointSize(9)
self.exit.setFont(font)
self.exit.setObjectName("exit")
self.medreal_label = QtWidgets.QLabel(self.centralwidget)
self.medreal_label.setGeometry(QtCore.QRect(460, 323, 200, 30))
self.medreal_label.setObjectName("medreal_label")
second.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(second)
self.statusbar.setObjectName("statusbar")
second.setStatusBar(self.statusbar)
self.retranslateUi(second)
QtCore.QMetaObject.connectSlotsByName(second)
second.setTabOrder(self.lastname_input, self.next)
second.setTabOrder(self.next, self.exit)
self.lastname_input.returnPressed.connect(self.clicked2)
self.next.clicked.connect(self.clicked2)
def retranslateUi(self, second):
_translate = QtCore.QCoreApplication.translate
second.setWindowTitle(_translate("second", "MainWindow"))
self.next.setText(_translate("second", "Next"))
self.patientinfo_label.setText(_translate("second", "Medication"))
self.barcode_label.setText(_translate("second", "Barcode:"))
self.medication_label.setText(_translate("second", "Medication:"))
self.welcome.setText(_translate("second", "Scan medication"))
self.exit.setText(_translate("second", "Exit"))
self.medreal_label.setText(_translate("second",""))
def clicked2(self):
print("passed clicked 2test")
【问题讨论】:
不要修改 Qt Designer 生成的代码,而是创建另一个继承自相应小部件的类并使用初始类来填充它。建议大家仔细阅读官方使用Designer的指南,切记千万不要编辑pyuic的输出riverbankcomputing.com/static/Docs/PyQt5/designer.html 你能说明我需要做什么吗? 【参考方案1】:不要修改Qt Designer
生成的代码,而是创建另一个继承自相应小部件的类并使用初始类来填充它。
我建议您仔细阅读有关使用Designer
的官方指南,并记住永远不要编辑pyuic
的输出
https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit
#from second import Ui_second
class Ui_second(object):
def setupUi(self, second):
second.setObjectName("second")
second.resize(1039, 717)
self.centralwidget = QtWidgets.QWidget(second)
self.centralwidget.setObjectName("centralwidget")
self.lastname_input = QtWidgets.QLineEdit(self.centralwidget)
self.imagelabel = QtWidgets.QLabel(self.centralwidget)
self.imagelabel.setGeometry(QtCore.QRect(130, 250, 201, 141))
self.imagelabel.setText("")
self.imagelabel.setPixmap(QtGui.QPixmap("databases/images/cartoon.png"))
self.imagelabel.setScaledContents(True)
self.imagelabel.setWordWrap(False)
self.imagelabel.setObjectName("imagelabel")
self.lastname_input.setGeometry(QtCore.QRect(460, 275, 200, 30))
self.lastname_input.setObjectName("lastname_input")
self.next = QtWidgets.QPushButton(self.centralwidget)
self.next.setGeometry(QtCore.QRect(570, 430, 80, 20))
font = QtGui.QFont()
font.setPointSize(9)
self.next.setFont(font)
self.next.setObjectName("next")
self.patientinfo_label = QtWidgets.QLabel(self.centralwidget)
self.patientinfo_label.setGeometry(QtCore.QRect(360, 230, 191, 31))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.patientinfo_label.setFont(font)
self.patientinfo_label.setObjectName("patientinfo_label")
self.barcode_label = QtWidgets.QLabel(self.centralwidget)
self.barcode_label.setGeometry(QtCore.QRect(360, 270, 100, 31))
font = QtGui.QFont()
font.setPointSize(11)
self.barcode_label.setFont(font)
self.barcode_label.setObjectName("barcode_label")
self.medication_label = QtWidgets.QLabel(self.centralwidget)
self.medication_label.setGeometry(QtCore.QRect(360, 320, 100, 31))
font = QtGui.QFont()
font.setPointSize(11)
self.medication_label.setFont(font)
self.medication_label.setObjectName("medication_label")
font = QtGui.QFont()
font.setPointSize(11)
self.welcome = QtWidgets.QLabel(self.centralwidget)
self.welcome.setGeometry(QtCore.QRect(260, 150, 471, 71))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.welcome.setFont(font)
self.welcome.setObjectName("welcome")
self.exit = QtWidgets.QPushButton(self.centralwidget)
self.exit.setGeometry(QtCore.QRect(480, 430, 80, 20))
font = QtGui.QFont()
font.setPointSize(9)
self.exit.setFont(font)
self.exit.setObjectName("exit")
self.medreal_label = QtWidgets.QLabel(self.centralwidget)
self.medreal_label.setGeometry(QtCore.QRect(460, 323, 200, 30))
self.medreal_label.setObjectName("medreal_label")
second.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(second)
self.statusbar.setObjectName("statusbar")
second.setStatusBar(self.statusbar)
self.retranslateUi(second)
QtCore.QMetaObject.connectSlotsByName(second)
second.setTabOrder(self.lastname_input, self.next)
second.setTabOrder(self.next, self.exit)
# self.lastname_input.returnPressed.connect(self.clicked2)
# self.next.clicked.connect(self.clicked2)
def retranslateUi(self, second):
_translate = QtCore.QCoreApplication.translate
second.setWindowTitle(_translate("second", "MainWindow"))
self.next.setText(_translate("second", "Next"))
self.patientinfo_label.setText(_translate("second", "Medication"))
self.barcode_label.setText(_translate("second", "Barcode:"))
self.medication_label.setText(_translate("second", "Medication:"))
self.welcome.setText(_translate("second", "Scan medication"))
self.exit.setText(_translate("second", "Exit"))
self.medreal_label.setText(_translate("second",""))
# def clicked2(self):
# print("passed clicked 2test")
class Ui_mainwindow(object):
def setupUi(self, mainwindow):
mainwindow.setObjectName("mainwindow")
mainwindow.resize(1039, 717)
self.centralwidget = QtWidgets.QWidget(mainwindow)
self.centralwidget.setObjectName("centralwidget")
self.nextbutton1 = QtWidgets.QPushButton(self.centralwidget)
self.nextbutton1.setGeometry(QtCore.QRect(550, 380, 80, 20))
font = QtGui.QFont()
font.setPointSize(9)
self.nextbutton1.setFont(font)
self.nextbutton1.setObjectName("nextbutton1")
self.patientinfo_label = QtWidgets.QLabel(self.centralwidget)
self.patientinfo_label.setGeometry(QtCore.QRect(360, 230, 191, 31))
font = QtGui.QFont()
font.setPointSize(11)
self.patientinfo_label.setFont(font)
self.patientinfo_label.setObjectName("patientinfo_label")
self.lastname_label = QtWidgets.QLabel(self.centralwidget)
self.lastname_label.setGeometry(QtCore.QRect(360, 270, 100, 31))
font = QtGui.QFont()
font.setPointSize(11)
self.lastname_label.setFont(font)
self.lastname_label.setObjectName("lastname_label")
self.lastname_input = QtWidgets.QLineEdit(self.centralwidget)
self.lastname_input.setGeometry(QtCore.QRect(460, 278, 150, 20))
self.lastname_input.setObjectName("lastname_input")
#self.lastname_input.returnPressed.connect(self.clicked)
#self.lastname_input.returnPressed.connect(self.clicked)
#self.lastname_input.returnPressed.connect(self.clicked)
self.firstname_label = QtWidgets.QLabel(self.centralwidget)
self.firstname_label.setGeometry(QtCore.QRect(360, 300, 100, 31))
font = QtGui.QFont()
font.setPointSize(11)
self.firstname_label.setFont(font)
self.firstname_label.setObjectName("firstname_label")
self.telephone_label = QtWidgets.QLabel(self.centralwidget)
self.telephone_label.setGeometry(QtCore.QRect(360, 330, 100, 31))
font = QtGui.QFont()
font.setPointSize(11)
self.telephone_label.setFont(font)
self.telephone_label.setObjectName("telephone_label")
self.welcome = QtWidgets.QLabel(self.centralwidget)
self.welcome.setGeometry(QtCore.QRect(260, 150, 471, 71))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.welcome.setFont(font)
self.welcome.setObjectName("welcome")
self.lastname_input_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lastname_input_2.setGeometry(QtCore.QRect(460, 308, 150, 20))
self.lastname_input_2.setObjectName("lastname_input_2")
self.lastname_input_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lastname_input_3.setGeometry(QtCore.QRect(460, 338, 150, 20))
self.lastname_input_3.setObjectName("lastname_input_3")
self.nextbutton1_2 = QtWidgets.QPushButton(self.centralwidget)
self.nextbutton1_2.setGeometry(QtCore.QRect(460, 380, 80, 20))
font = QtGui.QFont()
font.setPointSize(9)
self.nextbutton1_2.setFont(font)
self.nextbutton1_2.setObjectName("nextbutton1_2")
mainwindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(mainwindow)
self.statusbar.setObjectName("statusbar")
mainwindow.setStatusBar(self.statusbar)
self.retranslateUi(mainwindow)
QtCore.QMetaObject.connectSlotsByName(mainwindow)
mainwindow.setTabOrder(self.lastname_input, self.lastname_input_2)
mainwindow.setTabOrder(self.lastname_input_2, self.lastname_input_3)
mainwindow.setTabOrder(self.lastname_input_3, self.nextbutton1)
mainwindow.setTabOrder(self.nextbutton1, self.nextbutton1_2)
#self.lastname_input.returnPressed.connect(self.clicked)
#self.lastname_input.returnPressed.connect(self.clicked)
#self.lastname_input_3.returnPressed.connect(self.clicked)
# self.nextbutton1.clicked.connect(self.clicked1)
def retranslateUi(self, mainwindow):
_translate = QtCore.QCoreApplication.translate
mainwindow.setWindowTitle(_translate("mainwindow", "VeriMed Tech"))
self.nextbutton1.setText(_translate("mainwindow", "Next"))
self.patientinfo_label.setText(_translate("mainwindow", "Patient Information"))
self.lastname_label.setText(_translate("mainwindow", "Last name:"))
self.firstname_label.setText(_translate("mainwindow", "First name:"))
self.telephone_label.setText(_translate("mainwindow", "Telephone:"))
self.welcome.setText(_translate("mainwindow", "Welcome to VeriMed Technologies"))
self.nextbutton1_2.setText(_translate("mainwindow", "Exit"))
# def clicked1(self):
# print("passed first clicked test")
# mainwindow.close()
# ui = Ui_second()
# ui.setupUi(mainwindow)
# mainwindow.show()
class SecondWindow(QtWidgets.QMainWindow, Ui_second):
def __init__(self, parent=None):
super(SecondWindow, self).__init__(parent)
self.setupUi(self)
self.parent = parent
self.lastname_input.returnPressed.connect(self.clicked2)
self.next.clicked.connect(self.clicked2)
def clicked2(self):
print("passed clicked 2test")
# ...
self.parent.show()
self.hide()
class FirstWindow(QtWidgets.QMainWindow, Ui_mainwindow):
def __init__(self):
super(FirstWindow, self).__init__()
self.setupUi(self)
self.nextbutton1.clicked.connect(self.clicked1)
def clicked1(self):
print("passed first clicked test")
self.secondWindow = SecondWindow(self)
self.secondWindow.show()
self.hide()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# mainwindow = QtWidgets.QMainWindow()
# ui = Ui_mainwindow()
# ui.setupUi(mainwindow)
# mainwindow.show()
w = FirstWindow()
w.show()
sys.exit(app.exec_())
【讨论】:
谢谢你 S. Nick,你解决了我的问题,我会看看如何在未来的窗口中模拟这个。你应该得到比这更多的赞美:)以上是关于PyQt5 - 第二个窗口中的按钮在单击时不执行操作的主要内容,如果未能解决你的问题,请参考以下文章
Objective C - 我希望 UIAlertController 在单击按钮时不被删除