通过导入 PyQt5 为按钮添加功能 Ui
Adding Functions to Buttons via Importing PyQt5 Ui
我有一个名为 guiNext.py 和 next.py 的 PyQt5 Ui,它引用了 UI。如何向 UI 按钮添加功能?这就是我所拥有的,当我 运行 next.py 并单击 HELLO 按钮时没有任何反应。
guiNext.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'guiNext_001.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_nextGui(object):
def setupUi(self, nextGui):
nextGui.setObjectName("nextGui")
nextGui.resize(201, 111)
nextGui.setMinimumSize(QtCore.QSize(201, 111))
nextGui.setMaximumSize(QtCore.QSize(201, 111))
self.centralwidget = QtWidgets.QWidget(nextGui)
self.centralwidget.setObjectName("centralwidget")
self.helloBtn = QtWidgets.QPushButton(self.centralwidget)
self.helloBtn.setGeometry(QtCore.QRect(10, 10, 181, 91))
self.helloBtn.setObjectName("helloBtn")
nextGui.setCentralWidget(self.centralwidget)
self.retranslateUi(nextGui)
QtCore.QMetaObject.connectSlotsByName(nextGui)
def retranslateUi(self, nextGui):
_translate = QtCore.QCoreApplication.translate
nextGui.setWindowTitle(_translate("nextGui", "MainWindow"))
self.helloBtn.setText(_translate("nextGui", "HELLO"))
这是主文件 next.py:
#!usr/bin/env python
#-*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from guiNext import Ui_nextGui
class mainProgram(Ui_nextGui):
def __init__(self, parent=None):
Ui_nextGui.__init__(self)
self.setupUi(nextGui)
self.helloBtn.clicked.connect(self.hello)
def hello(self):
print ("HELLO")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
nextGui = QtWidgets.QMainWindow()
ui = Ui_nextGui()
ui.setupUi(nextGui)
nextGui.show()
sys.exit(app.exec_())
你的程序结构不太正确。 use the ui files created by Qt Designer有几种方法。多重继承方法可能是最直观的。您的代码应该如下所示:
from PyQt5 import QtCore, QtGui, QtWidgets
from guiNext import Ui_nextGui
class mainProgram(QtWidgets.QMainWindow, Ui_nextGui):
def __init__(self, parent=None):
super(mainProgram, self).__init__(parent)
self.setupUi(self)
self.helloBtn.clicked.connect(self.hello)
def hello(self):
print ("HELLO")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
nextGui = mainProgram()
nextGui.show()
sys.exit(app.exec_())
我有一个名为 guiNext.py 和 next.py 的 PyQt5 Ui,它引用了 UI。如何向 UI 按钮添加功能?这就是我所拥有的,当我 运行 next.py 并单击 HELLO 按钮时没有任何反应。
guiNext.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'guiNext_001.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_nextGui(object):
def setupUi(self, nextGui):
nextGui.setObjectName("nextGui")
nextGui.resize(201, 111)
nextGui.setMinimumSize(QtCore.QSize(201, 111))
nextGui.setMaximumSize(QtCore.QSize(201, 111))
self.centralwidget = QtWidgets.QWidget(nextGui)
self.centralwidget.setObjectName("centralwidget")
self.helloBtn = QtWidgets.QPushButton(self.centralwidget)
self.helloBtn.setGeometry(QtCore.QRect(10, 10, 181, 91))
self.helloBtn.setObjectName("helloBtn")
nextGui.setCentralWidget(self.centralwidget)
self.retranslateUi(nextGui)
QtCore.QMetaObject.connectSlotsByName(nextGui)
def retranslateUi(self, nextGui):
_translate = QtCore.QCoreApplication.translate
nextGui.setWindowTitle(_translate("nextGui", "MainWindow"))
self.helloBtn.setText(_translate("nextGui", "HELLO"))
这是主文件 next.py:
#!usr/bin/env python
#-*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from guiNext import Ui_nextGui
class mainProgram(Ui_nextGui):
def __init__(self, parent=None):
Ui_nextGui.__init__(self)
self.setupUi(nextGui)
self.helloBtn.clicked.connect(self.hello)
def hello(self):
print ("HELLO")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
nextGui = QtWidgets.QMainWindow()
ui = Ui_nextGui()
ui.setupUi(nextGui)
nextGui.show()
sys.exit(app.exec_())
你的程序结构不太正确。 use the ui files created by Qt Designer有几种方法。多重继承方法可能是最直观的。您的代码应该如下所示:
from PyQt5 import QtCore, QtGui, QtWidgets
from guiNext import Ui_nextGui
class mainProgram(QtWidgets.QMainWindow, Ui_nextGui):
def __init__(self, parent=None):
super(mainProgram, self).__init__(parent)
self.setupUi(self)
self.helloBtn.clicked.connect(self.hello)
def hello(self):
print ("HELLO")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
nextGui = mainProgram()
nextGui.show()
sys.exit(app.exec_())