文本框未在Matplotlib图上绘图

时间:2019-03-06 03:38:31

标签: python user-interface matplotlib pyqt5

运行以下脚本时出现问题,它无法完成self.add_box函数中的assign_text()命令。我已经对其进行了测试,它确实完全通过了add_box()函数,只是它没有在图中绘制文本框。任何帮助,将不胜感激!

我希望发生的事情是打开2个窗口,第一个是pyplot图,第二个是QWidget。当您点击QWidget上的按钮时,应该在pyplot图上放置一个新的文本框。现在,您按下按钮,pyplot图上没有任何新内容。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPlainTextEdit, QPushButton
import matplotlib.pyplot as plt

class Failure_Tree():
    def __init__(self):
        self.fig, self.ax = plt.subplots()
        self.ax.set_xlim([0, 100])
        self.ax.set_ylim([0, 100])
        self.fig.show()

    def add_box(self, text, posx, posy):
        # these are matplotlib.patch.Patch properties
        props = dict(boxstyle='round', facecolor='wheat', alpha=1)
        # place a text box
        self.ax.text(posx, posy, text, bbox=props, fontsize=14, horizontalalignment='center', verticalalignment='center')

    def window(self):
        self.app = QApplication(sys.argv)
        self.w = QWidget()

        self.w.resize(640, 480)

        self.textBox = QPlainTextEdit(self.w)
        self.textBox.move(250, 120)

        button = QPushButton("click me", self.w)
        button.move(20, 80)

        self.w.show()

        button.clicked.connect(
            lambda: self.assign_text())

    def assign_text(self):
        text = self.textBox.document().toPlainText()
        self.add_box('hello World', 50, 50)
        self.w.close()


F = Failure_Tree()
F.window()
F.add_box('Bye World!', 0, 0)

更新:

我已经解决了原始问题,但是现在在IDLE中运行代码时发生了问题。更新的代码如下,它在Spyder中可以正常使用,但是在创建新文本框后在IDLE中崩溃。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPlainTextEdit, QPushButton
import matplotlib.pyplot as plt

class Failure_Tree():
    def __init__(self):
        self.fig, self.ax = plt.subplots()
        self.ax.set_xlim([0, 100])
        self.ax.set_ylim([0, 100])
        self.fig.show()

    def add_box(self, text, posx, posy):
        # these are matplotlib.patch.Patch properties
        props = dict(boxstyle='round', facecolor='wheat', alpha=1)
        # place a text box
        self.ax.text(posx, posy, text, bbox=props, fontsize=14, horizontalalignment='center', verticalalignment='center')

        self.fig.canvas.draw()

    def window(self):
        self.app = QApplication(sys.argv)
        self.w = QWidget()

        self.w.resize(640, 480)

        self.textBox = QPlainTextEdit(self.w)
        self.textBox.move(250, 120)

        button = QPushButton("click me", self.w)
        button.move(20, 80)

        self.w.show()

        button.clicked.connect(
            lambda: self.assign_text())


    def assign_text(self):

        text = self.textBox.document().toPlainText()
        self.add_box('hello World', 50, 50)
        self.w.close()
        self.app.exec_()


F = Failure_Tree()
F.window()
F.add_box('Bye World!', 0, 0)

0 个答案:

没有答案