如何使用PyQt在单独的文件中启动单独的脚本,如按钮信号等。
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()
我阅读并看到如何制作按钮,但我没有看到如何从该按钮启动外部功能。
修改
我是这样做的:
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()
我从文件中重命名了该函数,而不是
this: if __name__ == '__main__':<---
to this: --->def dadada():
非常感谢大家,我知道我会一如既往地得到正确答案。
答案 0 :(得分:2)
你可以这样做:使用信号和插槽。信号可以从任何地方发出和接收,并且是线程安全的。您可以在单独的文件中导入您需要运行的外部脚本/函数/任何内容,并将按钮的clicked()
信号连接到该函数。例如,如果您需要在单击按钮时运行名为myScript()
的脚本:
import myScript
...
self.myButton.clicked.connect(self.run_myScript)
def run_myScript(self):
myScript()
单击该按钮时,它将运行myScript
。如果你想在一个单独的线程中运行它,你可以像你一直在做的那样。
答案 1 :(得分:1)
有两种方法可以做到这一点。
import file
并将该函数调用为file.functionName()
。强烈推荐。请注意,如果您的文件名为file.py
,则您的import
不会在最后包含.py
扩展名。
使用shell进程:os.system('python file.py')
。您需要import os
这个。
答案 2 :(得分:0)
#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 ()