【问题标题】:How to embed gnuplot qt in pyqt?如何在pyqt中嵌入gnuplot qt?
【发布时间】:2021-05-03 12:28:29
【问题描述】:

gnuplot 终端 qt 接受 widget id (http://www.bersch.net/gnuplot-doc/complete-list-of-terminals.html#qt)。 如何使用此 ID 号将 gnuplot qt 窗口嵌入到 pyqt5 应用程序中?我将不胜感激一个最小的工作代码。

关于 c++ 的类似问题没有答案 (Embed gnuplot inside existing QtWidget)。

为了比较,tcl/tk (program wish) + x11 终端下面的作品

set unit [open "| gnuplot 2>@1" w+]  
frame .x11 -bg "white" -width 640 -height 450
button .b1 -text "some button"
pack .x11 .b1

puts $unit "set term x11 window '[winfo id .x11]'"
flush $unit

puts $unit "pl sin(x)"
flush $unit

【问题讨论】:

  • c++ 问题有答案——第一条评论
  • @mugiseyebrows 从技术上讲不是答案,而是评论
  • 但是它在 pyqt 中看起来如何?

标签: python pyqt gnuplot


【解决方案1】:

您可以使用 QProcess 发送命令,并在这些命令中将窗口 ID 传递给它:

import sys
from functools import cached_property

from PyQt5 import QtCore, QtWidgets


class GnuPlotManager(QtCore.QObject):
    @cached_property
    def process(self):
        qprocess = QtCore.QProcess()
        qprocess.setProgram("gnuplot")
        return qprocess

    def start(self):
        self.process.start()

    def send_command(self, command):
        self.process.write((command + "\n").encode())


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)

        button = QtWidgets.QPushButton("Send Command")

        lay = QtWidgets.QVBoxLayout(central_widget)
        lay.addWidget(self.command_le)
        lay.addWidget(button)
        lay.addWidget(self.container)

        button.clicked.connect(self.handle_clicked)

        self.gnuplot_manager.start()
        self.gnuplot_manager.send_command("set terminal qt")
        wid = int(self.container.winId())
        self.gnuplot_manager.send_command(f"set term x11 window '{wid:x}'")

        self.gnuplot_manager.send_command("clear")

        self.command_le.setText("pl sin(x)")

        self.resize(640, 480)

    @cached_property
    def container(self):
        return QtWidgets.QWidget()

    @cached_property
    def command_le(self):
        return QtWidgets.QLineEdit()

    @cached_property
    def gnuplot_manager(self):
        return GnuPlotManager()

    def handle_clicked(self):
        self.gnuplot_manager.send_command(self.command_le.text())
        self.updateGeometry()


def main(args):
    app = QtWidgets.QApplication(args)

    w = MainWindow()
    w.show()

    ret = app.exec_()
    sys.exit(ret)


if __name__ == "__main__":
    main(sys.argv)

【讨论】:

  • 感谢您的回复。看起来挺好的。然而,我得到了ImportError: cannot import name 'cached_property'
  • @Friedrich 你用的是什么版本的python?如果你使用低于python 3.8的版本那么你必须安装backports.cached-property:python -m pip install backports.cached-property并将from functools import cached_property更改为from backports.cached_property import cached_property
  • 我有 Python 3.6.9。在您的帮助下,cached_property 错误消失了。但是,x11 窗口没有被绘制到 qt 应用程序中(可能太快了?)。它也适用于qt 终端吗?
  • @Friedrich 你说的qt终端是什么意思?你按下按钮了吗?
  • gnuplot 的qt 终端在bersch.net/gnuplot-doc/complete-list-of-terminals.html#qt 中描述。然后在您的代码中set term x11 window '{wid:x}' 可能应该替换为set term qt widget '{wid:x}'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 2012-07-15
  • 2012-09-09
相关资源
最近更新 更多