从单独的 Python 脚本对 Python 脚本执行命令? [英] Execute command to a Python script from separate Python script?

查看:75
本文介绍了从单独的 Python 脚本对 Python 脚本执行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将命令从一个 python 脚本发送到在终端中运行的另一个脚本.

I'm trying to send a command from one python script to another running in terminal.

我在运行 Raspbian 的 RPi 上运行两个 python 脚本.第一个脚本是一个循环,它等待用户输入一个数字并将其添加到总数中.第二个脚本使用 PySide2 在 QPushButton 被释放时打印一个数字.

I'm running two python scripts on an RPi running Raspbian. The first script is a loop that waits for the user to enter a number and adds it to a total. The second script uses PySide2 to print a number when a QPushButton is released.

我怎样才能让 QPushButton 被释放时运行的函数将命令(或变量)发送到等待的第一个脚本并执行它?

How can i make it so that the function that runs when the QPushButton is released, sends a command (or variable) into the waiting first script and executes it?

我已经阅读了一些关于使用 subprocess.callos.system 的内容,但我不确定我在用这些命令做什么,或者它们适合我想做的事情.

I've read some things about using subprocess.call and os.system but i'm not really sure what i'm doing with these commands, or if they're appropriate for what i want to do.

第一个脚本:

x = 0
while x < 10:
    y = int(input("enter number:"))
    x += y
    print(x)

print("x is ten!")

第二个脚本:

import sys
from PySide2.QtWidgets import *

class MainWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.btn = QPushButton("test")

        layout = QVBoxLayout()
        layout.addWidget(self.btn)

        self.setLayout(layout)

        self.btn.released.connect(self.btnpress)

    def btnpress(self):
        print(1)

app = QApplication(sys.argv)

window = MainWindow()
window.show()
app.exec_()

推荐答案

如果您想运行脚本并通过 stdin 发送信息,那么在 Qt 中最好的选择是使用 QProcess:

If you want to run a script and send information through stdin then in Qt the best option is to use QProcess:

import os.path
import sys

from PySide2.QtCore import QProcess
from PySide2.QtWidgets import QApplication, QPushButton, QSpinBox, QVBoxLayout, QWidget

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))


class MainWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.btn = QPushButton("test")
        self.spinbox = QSpinBox()

        self.process = QProcess()
        self.process.readyReadStandardError.connect(self.handle_readyReadStandardError)
        self.process.readyReadStandardOutput.connect(
            self.handle_readyReadStandardOutput
        )
        self.process.setProgram(sys.executable)
        script_path = os.path.join(CURRENT_DIR, "script.py")
        self.process.setArguments([script_path])
        self.process.start()

        layout = QVBoxLayout(self)
        layout.addWidget(self.spinbox)
        layout.addWidget(self.btn)
        self.btn.released.connect(self.btnpress)

    def btnpress(self):
        number = self.spinbox.value()

        msg = "{}\n".format(number)
        self.process.write(msg.encode())

    def handle_readyReadStandardError(self):
        print(self.process.readAllStandardError().data().decode())

    def handle_readyReadStandardOutput(self):
        print(self.process.readAllStandardOutput().data().decode())


app = QApplication(sys.argv)

window = MainWindow()
window.show()
app.exec_()

注意:您不必在第二个 shell 中运行脚本,因为脚本本身会启动它.

Note: You don't have to run the script in a second shell as the script itself starts it.

这篇关于从单独的 Python 脚本对 Python 脚本执行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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