如何在QMainWindow中显示对象信息

时间:2018-07-11 22:23:52

标签: python python-3.x pyqt4

我正在尝试使用PyQt4创建一个GUI,该GUI允许我创建一个新的Employee并调用Employee的info()方法。这是Employee类:

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)

这是集成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_())

此代码用于在运行窗口中显示info()中的信息。但是,我想做的是在QMainWindow中显示信息,但是我不确定该怎么做。

顺便说一句,我不知道我要怎么做才是正确的约定,所以如果我错了,请随时向我展示正确的方法。

2 个答案:

答案 0 :(得分:1)

有多种显示结果的方法,例如,我们可以使用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()

答案 1 :(得分:0)

QMainWindow在窗口底部具有内置的QStatusBar,可用于发送短消息。

如果您要走这条路线,则可能会更改Employee.info以返回字符串,例如

return "Employee ID: " + self.id + ",  Full name: " + self.full_name + ",  Salary: " + self.salary

并在状态栏中显示消息

self.statusbar.showMessage(emp.id)