【发布时间】:2020-10-24 15:59:33
【问题描述】:
我正在尝试将命令从一个 python 脚本发送到另一个在终端中运行的脚本。
我正在运行 Raspbian 的 RPi 上运行两个 python 脚本。第一个脚本是一个循环,等待用户输入一个数字并将其添加到总数中。第二个脚本使用 PySide2 在释放 QPushButton 时打印一个数字。
如何使 QPushButton 释放时运行的函数向等待的第一个脚本发送命令(或变量)并执行它?
我已经阅读了一些关于使用 subprocess.call 和 os.system 的内容,但我不确定我在用这些命令做什么,或者它们是否适合我想做的事情。
第一个脚本:
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_()
【问题讨论】:
-
因为这两个脚本都在python中,你能不能只用
import第一个脚本中的第二个脚本而不是使用subprocess.call或os.system? -
如果我这样做,我必须等待第一个脚本完成运行,然后再启动第二个脚本并等待按钮出现。
-
@Welshhobo 我不明白你为什么删除了你以前的帖子,因为我正在研究一个答案,它实现了使用 dbus 和 Qt 的事件循环的使用,恕我直言在这方面更优雅但无论如何都很好案例我已经根据您提供的内容提出了解决方案:使用 QProcess 。
-
@eyllanesc 啊抱歉,我认为脚本依赖于需要先安装的一堆模块,所以我认为它会更简单,我会在发布一个更简单的示例时得到更多答案。如果您仍然拥有它,那将非常方便。
标签: python python-3.x subprocess pyside2