noob
noob

Reputation: 788

'QDialog' object is not callable

I'm trying to connect two python folder together the two files contain PYQT5 UI code both of the two files run the codes good but I'm trying to connect the two file with a pushbutton: file_1 called graph_window.py contains a pushbutton which when I click it will show file_2 output UI this a proportion of the code :

from output_graph import Dialog # file_2
  def view_graph_output(self):
        self.w = Dialog()
        self.w.show()

  def setupUi(self, Dialog):
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(304, 162, 91, 31))
        self.pushButton.setObjectName("pushButton")

        # click function for the push button
        self.pushButton.clicked.connect(self.view_graph_output)

file two called output_graph.py


class Ui_Dialog:
   def setupUi(self, Dialog):
       Dialog.setObjectName("Dialog")
       Dialog.resize(900, 500)

       self.pushButton = QtWidgets.QPushButton(Dialog)
       self.pushButton.setGeometry(QtCore.QRect(0, 380, 141, 61))
       self.pushButton.setObjectName("pushButton")
   def retranslateUi(self, Dialog):
       _translate = QtCore.QCoreApplication.translate
       Dialog.setWindowTitle(_translate("Dialog", "output window"))

class Canvas(FigureCanvas):
   def __init__(self, parent=None, width=5, height=5, dpi=100):
       fig = Figure(figsize=(width, height), dpi=dpi)
       self.axes = fig.add_subplot(111)
       FigureCanvas.__init__(self, fig)
       self.setParent(parent)
       self.plot()

   def plot(self):
       x = np.array([50, 30, 40])
       labels = ["Apples", "Bananas", "Melons"]
       self.axes.pie(x, labels=labels)
       self.draw()


class Dialog(QtWidgets.QDialog, Ui_Dialog):

   def __init__(self, parent=None):
       super().__init__(parent)
       self.setupUi(self)
       self.canvas = Canvas(self, width=8, height=4)
       self.canvas.move(0, 0)

if __name__ == "__main__":
   app = QtWidgets.QApplication(sys.argv)
   w = Dialog()
   w.show()
   sys.exit(app.exec_())

when I run the code it gives me this error:

TypeError: 'QDialog' object is not callable

it seems I'm doing wrong when I call the output_graph from the Dialog class and I try to put the QtWidgets.QDialog with the calls and it didn't work

is there a way to call the dialog class from the pushbutton form file_1?

Upvotes: 0

Views: 524

Answers (1)

Enkum
Enkum

Reputation: 922

I think it should go like this:

import sys

from output_graph import Dialog  # file_2
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtCore
from PyQt5 import QtWidgets


class Ui_Dialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi()
        # self.canvas = Canvas(self, width=8, height=4)
        # self.canvas.move(0, 0)

    def view_graph_output(self):
        self.w = Dialog()
        self.w.show()

    def setupUi(self):
        self.pushButton = QtWidgets.QPushButton('push', self)
        self.pushButton.setGeometry(QtCore.QRect(304, 162, 91, 31))
        self.pushButton.setObjectName("pushButton")

        # click function for the push button
        self.pushButton.clicked.connect(self.view_graph_output)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = Ui_Dialog()
    w.show()
    sys.exit(app.exec_())

and output_graph.py should be:

import sys
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

from PyQt5 import QtCore
from PyQt5 import QtWidgets
import numpy as np

class Ui_Dialog:
   def setupUi(self, Dialog):
       Dialog.setObjectName("Dialog")
       Dialog.resize(900, 500)

       self.pushButton = QtWidgets.QPushButton(Dialog)
       self.pushButton.setGeometry(QtCore.QRect(0, 380, 141, 61))
       self.pushButton.setObjectName("pushButton")
       # self.pushButton.clicked.connect(lambda: funcion(12))
   def retranslateUi(self, Dialog):
       _translate = QtCore.QCoreApplication.translate
       Dialog.setWindowTitle(_translate("Dialog", "output window"))

class Canvas(FigureCanvas):
   def __init__(self, parent=None, width=5, height=5, dpi=100):
       fig = Figure(figsize=(width, height), dpi=dpi)
       self.axes = fig.add_subplot(111)
       FigureCanvas.__init__(self, fig)
       self.setParent(parent)
       self.plot()

   def plot(self):
       x = np.array([50, 30, 40])
       labels = ["Apples", "Bananas", "Melons"]
       self.axes.pie(x, labels=labels)
       self.draw()


class Dialog(QtWidgets.QDialog, Ui_Dialog):

   def __init__(self, parent=None):
       super().__init__(parent)
       self.setupUi(self)
       self.canvas = Canvas(self, width=8, height=4)
       self.canvas.move(0, 0)



if __name__ == "__main__":
   app = QtWidgets.QApplication(sys.argv)
   w = Dialog()
   w.show()
   sys.exit(app.exec_())

PS: it's a messy code so requires cleaning up, but works as you wanted, as to my understanding of the problem

Upvotes: 1

Related Questions