PyQt 按钮从另一个文件启动另一个脚本 [英] PyQt button start another script from another file

查看:108
本文介绍了PyQt 按钮从另一个文件启动另一个脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 PyQt 在单独的文件中启动单独的脚本,例如按钮信号或其他东西.

How can i start a separate script in a separate file using PyQt like a button signal or something.

from everywhere* import everthing*

def bJeff(driver):
    ...

def bLexi(driver):
    ...

def browser(url, id, user1, parolauser1, user2, parolauser2):
    ...
    #starting 2 browsers and passing them for further processing
    bLexi(driver)
    ...
    bJeff(driver)
    return

if __name__ == '__main__':
    jobs = []
    user_Lexi = "user1@mail.com"
    parola_user_Lexi = "pass1"
    user_Jeff = "user@mail.com"
    parola_user_Jeff = "pass2"
    sites = ["http://www.didi.com", "http://www.didi.com/"]
    id = 0
    for pagina in sites:
        p = multiprocessing.Process(target=browser, args=(pagina, id, user_Lexi, parola_user_Lexi, user_Jeff, parola_user_Jeff))
        id+=1
        jobs.append(p)
        p.start()

我阅读并看到了如何制作按钮,但我没有看到如何从该按钮启动外部功能.

I read and saw how can i make a button but i didn't saw how can i start an outside function from that button.

编辑

我是这样做的:

import os, sys, subprocess, filetoinclude
from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui

class QButton(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        # create objects
        self.name='me'
        label = QLabel(self.tr("Enter command and press Return"))
        self.le = QLineEdit()
        self.te = QTextEdit()
        self.button = QtGui.QPushButton('Button', self)
        # layout
        layout = QVBoxLayout(self)
        layout.addWidget(label)
        layout.addWidget(self.le)
        layout.addWidget(self.button)
        layout.addWidget(self.te)
        self.setLayout(layout) 
        # create connection
        self.button.clicked.connect(self.calluser)
        self.connect(self.le, SIGNAL("returnPressed(void)"), self.run_command)
    def calluser(self):
        print(self.name)
        filetoinclude.dadada()

    def run_command(self):
        cmd = str(self.le.text())
        result = self.system_call(cmd)
        self.te.setText(result)

    def system_call(self, command):
        p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
        return p.stdout.read()

def demo_QButton():
    app = QtGui.QApplication(sys.argv)
    tb = QButton()
    tb.show()
    app.exec_()

if __name__=='__main__':
    demo_QButton()

我从文件中重命名了函数而不是

And i renamed the function from the file instead of

this: if __name__ == '__main__':<---

to this: --->def dadada():

非常感谢大家,我知道我会像往常一样在这里得到正确的答案.

Thank you very much everyone, i knew i will get the right answers here, as always.

推荐答案

您可以通过以下方式做到:使用信号和槽.信号可以从任何地方发射和接收,并且是线程安全的.您可以导入外部脚本/函数/任何需要在单独文件中运行的内容,并将按钮的 clicked() 信号连接到该函数.例如,如果您需要在单击按钮时运行一个名为 myScript() 的脚本:

You can do it by exactly that: using signals and slots. Signals can be emitted and received from anywhere and are thread safe. You can import your foreign script/function/whatever you need to run in the separate file, and connect your button's clicked() signal to that function. For example, if you need to run a script called myScript() when the button is clicked:

import myScript

...
self.myButton.clicked.connect(self.run_myScript)
def run_myScript(self):
    myScript()

当按钮被点击时,它会运行 myScript.如果你想在一个单独的线程中运行它,你可以像你一直在做的那样做.

When the button is clicked, it'll run myScript. If you want to run it in a separate thread, you can do it like you've been doing.

这篇关于PyQt 按钮从另一个文件启动另一个脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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