[英]PyQt5: Using QtDesigner, how do you connect a signal with a slot/callable defined in a module
我是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 ..在主窗口中添加了一個插槽,如圖所示,然后將按鈕的Pressed信號與主窗口的插槽連接起來
當我嘗試運行該應用程序時,出現此錯誤:
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生成了自動模塊,並添加了函數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")
您需要像這樣重組代碼:
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_())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.