0

I am new to stack overflow. This is my second post so please do correct me out if I am wrong. Will be precise and explain my issue. I am trying to embed a graph into Qt Framework but I can only manage to show it individually in a separate window.

UI CODE:(dt.py)

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s
try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(431, 199)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(20, 20, 71, 20))
        self.label.setObjectName(_fromUtf8("label"))
        self.X1 = QtGui.QLineEdit(self.centralwidget)
        self.X1.setGeometry(QtCore.QRect(20, 50, 71, 22))
        self.X1.setObjectName(_fromUtf8("X1"))
        self.label_3 = QtGui.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(350, 20, 46, 20))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.Y1 = QtGui.QLineEdit(self.centralwidget)
        self.Y1.setGeometry(QtCore.QRect(180, 50, 71, 22))
        self.Y1.setObjectName(_fromUtf8("Y1"))
        self.Generate = QtGui.QPushButton(self.centralwidget)
        self.Generate.setGeometry(QtCore.QRect(160, 110, 101, 31))
        self.Generate.setObjectName(_fromUtf8("Generate"))
        self.label_2 = QtGui.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(180, 20, 71, 20))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.R1 = QtGui.QLineEdit(self.centralwidget)
        self.R1.setGeometry(QtCore.QRect(330, 50, 71, 22))
        self.R1.setObjectName(_fromUtf8("R1"))
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.label.setText(_translate("MainWindow", "X-Co-ordinate", None))
        self.label_3.setText(_translate("MainWindow", "Radii", None))
        self.Generate.setText(_translate("MainWindow", "Generate", None))
        self.label_2.setText(_translate("MainWindow", "Y-Co-ordinate", None))

Python code:

import os
import sys
import time
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PyQt4 import QtGui,QtCore

from dt import Ui_MainWindow

class demoMainClass(QtGui.QMainWindow,Ui_MainWindow):
    aX1 =  ("0")
    aY1 =  ("0")
    aR1 =  ("0")
    def __init__(self,parent=None):  
        super(demoMainClass,self).__init__()
        self.setupUi(self)
        self.demoWindowInitialize()
        self.demoFunction()
    def demoWindowInitialize(self):
        self.X1.insert(self.aX1)
        self.Y1.insert(self.aY1)
        self.R1.insert(self.aR1)
        self.Generate.clicked.connect(lambda: self.demoFunction())
    def demoFunction(self):
        x1=float(self.X1.text())
        y1=float(self.Y1.text())
        r1=float(self.R1.text())
        fig2 = plt.figure()
        ax2 = fig2.add_subplot(111, aspect='equal')
        ax2.add_patch(
            patches.Circle(
                (x1, y1),
                 r1,
                 fill=False
            )
        )
        plt.show()
        pass
if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = demoMainClass()
    ui.show()
    sys.exit(app.exec_())

I want to embed the graph into my QT window itself.Tried with QGraphicsView but was not getting a proper result.

2
  • There are enough examples to embedd a matplotlib plot into a Qt GUI. Even the matplotlib page has examples on it. To be on the save side, do not even import pyplot at all, this will ensure that you are not tempted to use it. Commented Jan 25, 2018 at 12:12
  • Alrite!Thanks will go through the duplicates. Commented Jan 25, 2018 at 12:15

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.