我是PyQt5的初学者,请原谅我的无知 我正在尝试连接按钮和我定义的功能
import sys
from PyQt5 import QtGui, QtWidgets,uic
def PrintSomething ():
print("Hello world")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = uic.loadUi("Auto.ui")
window.show()
sys.exit(app.exec_())
为了做到这一点,我使用QtDesigner在MainWindow中添加了一个插槽..如图所示,然后我将按钮的按下信号与主窗口的插槽连接
当我尝试运行该应用时,我收到此错误:
AttributeError:'QMainWindow'对象没有属性'PrintSomething'
我在这里做错了什么?
更新:
我也使用过此代码
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from Auto import Ui_MainWindow
class MainW (QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == '__main__':
#topology=topo.LoadTopology()
app = QApplication(sys.argv)
myapp = MainW()
myapp.show()
sys.exit(app.exec_())
通过pyuic5生成Auto模块并添加了PrintSomething()函数。这也会导致错误
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(667, 487)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(300, 380, 95, 29))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 667, 23))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuExit = QtWidgets.QMenu(self.menubar)
self.menuExit.setObjectName("menuExit")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionSave = QtWidgets.QAction(MainWindow)
self.actionSave.setObjectName("actionSave")
self.actionClose = QtWidgets.QAction(MainWindow)
self.actionClose.setObjectName("actionClose")
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionSave)
self.menuFile.addAction(self.actionClose)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuExit.menuAction())
self.retranslateUi(MainWindow)
self.pushButton.pressed.connect(MainWindow.PrintSomething())
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuExit.setTitle(_translate("MainWindow", "Exit"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionSave.setText(_translate("MainWindow", "Save"))
self.actionClose.setText(_translate("MainWindow", "Close"))
def PrintSomething ():
print("Hello world")
答案 0 :(得分:1)
您需要像这样重构代码:
import sys
from PyQt5 import QtGui, QtWidgets, uic
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
uic.loadUi("Auto.ui", self)
def PrintSomething(self):
print("Hello world")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
但是,我认为最好将代码中的信号连接起来,而不是通过Qt Designer完成所有操作:
class Window(QtWidgets.QMainWindow):
def __init__(self):
...
self.pushButton.clicked.connect(self.PrintSomething)
如果您更改了PrintSomething
插槽的名称,或者想要将按钮连接到其他插槽,则通过Qt Designer进行更改会非常麻烦。在开发应用程序时,信号连接可能会经常发生变化,因此手动编码可能更易于管理。
修改强>:
添加到问题中的另一个示例可以以类似的方式修复。但是,如果您要使用pyuic
,则永远不会编辑它生成的模块。相反,您应该将其导入主脚本,如下所示:
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from Auto import Ui_MainWindow
class MainW (QMainWindow, Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.PrintSomething)
def PrintSomething(self):
print("Hello world")
if __name__ == '__main__':
app = QApplication(sys.argv)
myapp = MainW()
myapp.show()
sys.exit(app.exec_())