如何将 QPushButton 连接到函数

How to connect a QPushButton to a function

我想用 PyQt GUI 创建一个简单的程序。为此,我使用了 Qt Designer,并制作了一个带有名为 OpenImage 的按钮的 window。此外,我有一个名为 rony.py 的文件,它有一个函数 show。所以我希望当用户点击按钮时,图像会出现。

这是我的代码:

from PyQt4 import QtCore, QtGui
import rony

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.OpenImage = QtGui.QPushButton(self.centralwidget)
        self.OpenImage.setObjectName(_fromUtf8("OpenImage"))
        self.horizontalLayout.addWidget(self.OpenImage)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.OpenImage.setText(_translate("MainWindow", "PushButton", None))


OpenImage.connect(show()) # Why this line don't work


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

rony.py

from PIL import Image 
img = Image.open("pipe.png")

def show():
    img.show()

从脚本调用函数时必须引用脚本的名称。它作为一个库工作:

OpenImage.connect(rony.show())