如何使用 PyQt 在按钮单击时调用 python 脚本 [英] how to call a python script on button click using PyQt

查看:236
本文介绍了如何使用 PyQt 在按钮单击时调用 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PyQt4 创建了一个带有按钮的表单.在这个按钮上,我想调用另一个 python 脚本,如下所示:

I have created a form using PyQt4 which has a push button. On this push button I want to call another python script which looks like this:

File1.py:

import sys
from PyQt4 import QtCore, QtGui


from file1_ui import Ui_Form


class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())

File1_ui.py

File1_ui.py

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(120, 200, 95, 20))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Form.close)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("Form", "Close", None, QtGui.QApplication.UnicodeUTF8))

File2.py

import sys
from PyQt4 import Qt
from taurus.qt.qtgui.application import TaurusApplication

app = TaurusApplication(sys.argv)
panel = Qt.QWidget()
layout = Qt.QHBoxLayout()
panel.setLayout(layout)

from taurus.qt.qtgui.panel import TaurusForm

panel = TaurusForm()

model = [ 'test/i1/1/%s' % p for p in props ]
panel.setModel(model)

panel.show()
sys.exit(app.exec_())

File1_ui.py 是从 Qtdesigner 创建的,然后我使用 File1.py 来执行它.所以 File2.py 单独执行时会打开一个面板并显示几个属性.我希望在单击按钮时调用此脚本在我使用 Qtdesigner 创建的第一个表单 (file1.py) 中.您能告诉我如何实现此功能吗?谢谢.

File1_ui.py is created from the Qtdesigner and then I am using File1.py to execute it.So File2.py when executed alone opens up a panel and displays few attributes.I want this script to be called on the button click in the first form(file1.py) which I created using Qtdesigner.Could you let me know how I could achieve this functionality.Thanks.

推荐答案

您需要对 File2.py 进行一些修改,以根据它是否独立运行进行适当的调用.当您通过 File1.py 启动脚本时,已经有一个 QApplication 实例在运行事件循环,因此尝试创建另一个并运行其事件循环会导致问题.

You will need to make some modifications to File2.py to make the appropriate calls depending on whether it is running standalone or not. When you are launching the script via File1.py there will already be a QApplication instance with event loop running, so trying to create another and run its event loop will cause problems.

首先,将脚本的核心部分移到它自己的函数中.这将允许您从 File1.py 轻松调用它.然后,您可以处理脚本独立运行并需要创建 QApplication 实例并启动其事件循环的情况.(我不熟悉你使用的 taurus 库,但你可以替换 TaurusApplication for QtGui.QApplication)

Firstly, move the core part of your script into its own function. This will allow you to easily call it from File1.py. You can then handle the case where the script is running standalone and needs to create a QApplication instance and start its event loop. (I am not familiar the the taurus library you are using, but you can probably substitute TaurusApplication for QtGui.QApplication)

File2.py:

import sys
from PyQt4 import QtCore, QtGui

def runscript():
    panel = QtGui.QWidget()
    layout = QtGui.QHBoxLayout(panel)
    return panel # Must return reference or panel will be deleted upon return


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    panel = runscript()
    panel.show()
    sys.exit(app.exec_())

<小时>

假设您的文件在同一目录中,您只需编写 import File2 并使用 File2.runscript() 来运行您的代码.然后,您只需将该函数连接到您的按钮 clicked() 信号即可运行它.这里唯一的问题是,如果您直接连接到 runscript,从 runscript() 函数返回的对 QWidget 的引用将丢失(并且对象被删除)().为此,我创建了一个方法 launch_script(),它在 MyForm 中保存了一个引用.


Assuming your files are in the same directory you can simply write import File2 and use File2.runscript() to run your code. You then just need to connect the function to your pushbuttons clicked() signal to run it. The only problem here is that the reference to the QWidget returned from the runscript() function will be lost (and the object deleted) if you connect directly to runscript(). For this reason I created a method launch_script() which saves a reference in MyForm.

File1.py:

import sys
from PyQt4 import QtCore, QtGui

from file1_ui import Ui_Form
import File2

class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        # This is a bit of a hack.
        self.ui.pushButton.clicked.connect(self.launch_script)

    def launch_script(self):
        self.panel = File2.runscript()
        self.panel.show()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())

我不使用 Qt Designer,所以我不知道将信号连接到 launch_script() 的正确方法.我写的代码应该可以工作,但显然违反了 OOP 原则,并且依赖于软件分配的按钮小部件的名称.

I don't use Qt Designer, so I don't know the correct way to go about connecting the signal to launch_script(). The code I have written should work, but obviously violates OOP principles and is dependent on the name of the pushbutton widget assigned by the software.

这篇关于如何使用 PyQt 在按钮单击时调用 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆