PyQt button start another script from another file

2019-09-09 13:19发布

问题:

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.

Edit

I did it like this:

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.

回答1:

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()

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.



回答2:

There are 2 ways to do this.

  1. import file and call the function as file.functionName(). Highly recommended. Note that if your file is called file.py, your import should not include the .py extension at the end.

  2. Using a shell process: os.system('python file.py'). You need to import os for this one.



回答3:

#If your are not expecting this answer, sorry.

    def button_OutsideScript (self) :
        import OutsideScript_Name as osScript
        reload (osScript)

        #If you have class inside the Outside Script use below line
        osScript.className ()