Python – Adding a Tkinter Graph to a PyQt Widget

Question:

We currently have a fully functional Gui written created using PyQt. My partner wrote a function that graphs a dataSet in Tkinter. My question is, how do we combine the two so they work together?

Here is the graphing function:

def createGraph(self):
        import tkinter as tk

        # Send in data as param, OR
        #data = [17, 20, 15, 10, 7, 5, 4, 3, 2, 1, 1, 0]        

        # Recieve data within function 
        s.send("loadgraph")

        inputString = repr(s.recv(MSGSIZE))
        #inputString = "-20 15 10 7 5 -4 3 2 1 1 0"
        print(inputString)
        data = [int(x) for x in inputString.split()]

        root = tk.Tk()
        root.title("FSPwners")
        screen_width = 400
        screen_height = 700
        screen = tk.Canvas(root, width=screen_width, height=screen_height, bg= 'white')
        screen.pack()

        # highest y = max_data_value * y_stretch
        y_stretch = 15
        # gap between lower canvas edge and x axis
        y_gap = 350
        # stretch enough to get all data items in
        x_stretch = 10
        x_width = 20
        # gap between left canvas edge and y axis
        x_gap = 20


        for x, y in enumerate(data):
            # calculate reactangle coordinates (integers) for each bar
            x0 = x * x_stretch + x * x_width + x_gap
            y0 = screen_height - (y * y_stretch + y_gap)
            x1 = x * x_stretch + x * x_width + x_width + x_gap
            y1 = screen_height - y_gap
            # draw the bar
            print(x0, y0, x1, y1)
            if y < 0:
                screen.create_rectangle(x0, y0, x1, y1, fill="red")
            else:
                screen.create_rectangle(x0, y0, x1, y1, fill="green")

            # put the y value above each bar
            screen.create_text(x0+2, y0, anchor=tk.SW, text=str(y))

        root.mainloop()

When that method is run by itself, it creates a popup box with the graph. Now we want it to create a popup graph when a button is pressed in our current gui. How can we get it to work? If we just call createGraph() when a button is clicked in our GUI, we get the error:
unrecognized selector sent to instance x009x…

What is the problem? Thanks!

Asked By: CHawk

||

Answers:

Qt and Tkinter don’t play along quite well, as you can perceive –
I had once played along Python graphical toolkits, and wrote
a 4 operation calculator that would run in either Qt, GTK or Tkinter –
or even display all at once.

In order to have both the Tkinter and Qt versions working simultaneously,
I had to fork the process – and start each toolkit in a separate
running instance;

Your case is not identical, as the Qt GUI will be already running, but maybe
having this to start up with you can come along with a work-around.

The 3-calculators code listing can be found here:

https://web.archive.org/web/20101122232402/http://www.python.org.br/wiki/CalculadoraTkGtkQt

Answered By: jsbueno

Here’s a PyQt port:

from PyQt4 import QtCore, QtGui

class Graph(QtGui.QWidget):
    def __init__(self, data, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self._data = data
        self.resize(400, 700)
        self.setWindowTitle('FSPwners')
        self.setAutoFillBackground(True)
        self.setBackgroundRole(QtGui.QPalette.Base)

    def paintEvent(self, event):
        painter = QtGui.QPainter()
        painter.begin(self)

        screen_width = self.width()
        screen_height = self.height()

        # highest y = max_data_value * y_stretch
        y_stretch = 15
        # gap between lower canvas edge and x axis
        y_gap = 350
        # stretch enough to get all data items in
        x_stretch = 10
        x_width = 20
        # gap between left canvas edge and y axis
        x_gap = 20

        for x, y in enumerate(self._data):
            # calculate reactangle coordinates (integers) for each bar
            x0 = x * x_stretch + x * x_width + x_gap
            y0 = screen_height - (y * y_stretch + y_gap)
            x1 = x0 + x_width
            y1 = screen_height - y_gap
            if y < 0:
                painter.setBrush(QtCore.Qt.red)
            else:
                painter.setBrush(QtCore.Qt.green)
            painter.drawRect(QtCore.QRectF(
                QtCore.QPointF(x0, y0), QtCore.QPointF(x1, y1)))
            print (x0, y0, x1, y1)

            # put the y value above each bar
            painter.drawText(x0 + 2, y0 - 2, str(y))

        painter.end()

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    # data to be graphed
    data = [-20, 15, 10, 7, 5, -4, 3, 2, 1, 1, 0]
    window = Graph(data)
    window.show()
    sys.exit(app.exec_())
Answered By: ekhumoro
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.