Search code examples
pythonpython-3.xpyqt4

How to display Object information in QMainWindow


I am attempting to create a GUI with PyQt4 which allows me to create a new Employee and call the Employee's info() method. Here is the Employee class:

class Employee:

  def __init__(self, full_name, id, salary):
    self.full_name = full_name
    self.id = id
    self.salary = salary

  @property
  def info(self):
    return print("Employee ID:", self.id, "\nFull name:", self.full_name, "\nSalary:", self.salary)

Here is the code that integrates PyQt4:

import sys
from PyQt4 import QtGui
from PyQt4.QtGui import QInputDialog


class Employee:

  def __init__(self, full_name, id, salary):
    self.full_name = full_name
    self.id = id
    self.salary = salary

  @property
  def info(self):
    return print("Employee ID:", self.id, "\nFull name:", self.full_name, "\nSalary:", self.salary)


class Window(QtGui.QMainWindow, Employee):

  def __init__(self):
    super(Window, self).__init__()  #Returns the parrent object or the QMainWindow object
    self.setGeometry(50, 50, 500, 300)
    self.setWindowTitle("Employee builder")

    extractAction = QtGui.QAction("&Add Employee", self)
    extractAction.triggered.connect(self.create_employee)

    mainMenu = self.menuBar()
    fileMenu = mainMenu.addMenu('&File')
    fileMenu.addAction(extractAction)

    self.home()

  def home(self):
    self.show()

  def create_employee(self):
    text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',
                                          'Enter employees name:')

    ID, ok = QInputDialog.getInt(self, "integer input dualog", "Enter employees id number:")

    pay, ok = QInputDialog.getInt(self, "integer input dualog", "Enter employees salary:")

    emp1 = Employee(text, ID, pay)
    emp1.info


def run():
  app = QtGui.QApplication(sys.argv)
  GUI = Window()
  sys.exit(app.exec_())

This code works to display the information from info() in the run window. However, what I would like to do is display the information in the QMainWindow but I am not sure how to do that.

On a side note, I don't know that the way I am going about this is the correct convention so feel free to show me the correct way if I am wrong.


Solution

  • There are many ways to show a result, for example we can use a QMessageBox.

    import sys
    from PyQt4 import QtGui
    
    
    class Employee:
        def __init__(self, full_name, _id, salary):
            self.full_name = full_name
            self.id = _id
            self.salary = salary
    
        @property
        def info(self):
            return "Employee ID: {}\nFull name:{}\nSalary:{}".format(self.id, self.full_name, self.salary)
    
    
    class Window(QtGui.QMainWindow, Employee):
        def __init__(self):
            super(Window, self).__init__()  #Returns the parrent object or the QMainWindow object
            self.setGeometry(50, 50, 500, 300)
            self.setWindowTitle("Employee builder")
    
            extractAction = QtGui.QAction("&Add Employee", self)
            extractAction.triggered.connect(self.create_employee)
    
            mainMenu = self.menuBar()
            fileMenu = mainMenu.addMenu('&File')
            fileMenu.addAction(extractAction)
    
        def create_employee(self):
            text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog','Enter employees name:')
            ID, ok = QtGui.QInputDialog.getInt(self, "integer input dualog", "Enter employees id number:")
            pay, ok = QtGui.QInputDialog.getInt(self, "integer input dualog", "Enter employees salary:")
            emp1 = Employee(text, ID, pay)
            QtGui.QMessageBox.information(self, "New Employee", emp1.info)
    
    
    def run():
        app = QtGui.QApplication(sys.argv)
        w = Window()
        w.show()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        run()