【发布时间】:2018-06-09 05:22:05
【问题描述】:
我正在尝试使用 Python GUI 和 gnuplot 创建绘图。 我正在用 Python 生成代码并将其发送到 gnuplot。 这基本上适用于将数据管道传输到 gnuplot,但是:
缺点:
- Python 程序在您关闭 gnuplot 之前被阻止
- 每次绘制似乎需要额外时间(在慢速计算机上)的绘图时,您都必须一次又一次地加载/启动 gnuplot
我的问题:
- 如何让 Python 程序保持响应?
- 有没有办法让 gnuplot 启动一次并保持运行?
- 如果有新的绘图,如何更新 gnuplot 终端?
感谢您的提示和链接。
这是我的代码:
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPlainTextEdit, QPushButton
import subprocess
class MyWindow(QWidget):
def __init__(self):
super(MyWindow,self).__init__()
self.setGeometry(100,100,400,200)
self.myTextEdit = QPlainTextEdit()
self.myTextEdit.setPlainText("plot sin(x)")
self.button = QPushButton('Plot code',self)
self.button.clicked.connect(self.on_button_click)
vbox = QVBoxLayout(self)
vbox.addWidget(self.myTextEdit)
vbox.addWidget(self.button)
self.setLayout(vbox)
@pyqtSlot()
def on_button_click(self):
gnuplot_str = self.myTextEdit.document().toPlainText() + "\n"
gnuplot_path = r'C:\Programs\gnuplot\bin\gnuplot.exe'
plot = subprocess.Popen([gnuplot_path,'-p'],stdin=subprocess.PIPE)
plot.communicate(gnuplot_str.encode())
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
【问题讨论】:
-
你需要使用线程。
-
谢谢,@101,这个概念对我来说是新概念。我去看看。
标签: python python-3.x gnuplot pyqt5