PyQt5 ImportError

时间:2018-07-19 20:00:30

标签: python-3.x pyqt5

我尝试使用在网上找到的脚本将在PyQt4中编写的GUI转换为PyQt5。转换代码似乎效果很好,这里是转换:

已转换的PyQt5 GUI:

import sys
import random
import datetime
from PyQt5 import QtGui, QtWidgets

employee_dict = {}
all_randomly_generated_intigers = {}
x = 0


class Employee:

  def __init__(self, id, salary, name):
    self.id = id
    self.salary = salary
    self.name = name
    self.start_time = datetime.datetime.now()
    employee_dictonary(self)

  def info(self):

    if isinstance(self, Supervisor):

      return "Supervisors name: {} "\
             "\n Salary: {} "\
             "\n ID's of employees under supervision: {} "\
             "\n Start date: {}".format(self.name,
                                        self.salary,
                                        self.id,
                                        self.supervisees,
                                        self.start_time)

    if isinstance(self, Developer):
      return "Developers name: {} "\
             "\nSalary: {}"\
             "\nKnown programing languages: {} "\
             "\nStart date: {}".format(self.name,
                                       self.salary,
                                       self.programming_language,
                                       self.start_time)
    else:
      return "Employees name: {} "\
             "\nSalary: {} "\
             "\nStarted on: {}".format(self.name, self.salary, self.start_time)


class Supervisor(Employee):
  raise_amount = int(1.03)

  def __init__(self, id, salary, name, supervisees=None):
    super().__init__(id, salary, name)

    self.supervisees = [] if supervisees is None else supervisees

  """
  Add an Employee object to the supervisors
  employee superviesees list.
  """

  def add_supervisees(self, employee):
    if employee not in self.supervisees:
      self.supervisees.append(employee)

  """
  Remove an employee from the supervisors
  employee supervisees list.
  """

  def remove_supervisees(self, employee):
    self.supervisees.remove(employee)

  def print_supervisees(self):
    print("The ID of the supervisses belonging to this supervisor are:")
    return ', '.join(str(employee.id) for employee in self.supervisees)


class Developer(Employee):
  def __init__(self, id, salary, name, programming_language):
    super().__init__(id, salary, name)
    self.programming_language = programming_language


def employee_dictonary(self):
  employee_dict[self.id] = self


def check_dictonary(ID):
  if ID not in employee_dict:
    print("This ID is invalid.")


def new_ID():
  y = random.randint(0, 5)
  global x
  while x in all_randomly_generated_intigers:
    x += 1
  all_randomly_generated_intigers[x] = y
  return x


class Window(QtWidgets.QMainWindow, Employee):

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

    create_supervisor = QtWidgets.QAction("&Add Supervisor", self)
    create_supervisor.triggered.connect(self.new_supervisor)
    create_developer = QtWidgets.QAction("&Add Developer", self)
    create_developer.triggered.connect(self.new_developer)
    create_employee = QtWidgets.QAction("&Add Employee", self)
    create_employee.triggered.connect(self.new_employee)

    find_employee_information = QtWidgets.QAction("&Employee Information", self)
    find_employee_information.triggered.connect(self.display_employee)

    supervisor_first_action = QtWidgets.QAction("Add supervisee", self)
    supervisor_first_action.triggered.connect(self.add_supervisee_action)
    supervisor_seccond_action = QtWidgets.QAction("Remove supervisee", self)
    supervisor_seccond_action.triggered.connect(self.remove_supervisee_action)
    supervisor_third_action = QtWidgets.QAction("Print subervisees", self)
    supervisor_third_action.triggered.connect(self.print_supervisee_action)

    developer_first_action = QtWidgets.QAction("Developer Actions", self)
    developer_first_action.triggered.connect(self.developer_actions)
    developer_seccond_action = QtWidgets.QAction("Seccond Action", self)
    developer_seccond_action.triggered.connect(self.developer_actions)

    employee_first_action = QtWidgets.QAction("Employee Actions", self)
    employee_first_action.triggered.connect(self.employee_actions)
    employee_seccond_action = QtWidgets.QAction("Seccond Action", self)
    employee_seccond_action.triggered.connect(self.employee_actions)

    mainMenu = self.menuBar()

    file_menu = mainMenu.addMenu('&File')
    file_menu.addAction(create_supervisor)
    file_menu.addAction(create_developer)
    file_menu.addAction(create_employee)

    file_information_menu = mainMenu.addMenu('&Employee Information')
    file_information_menu.addAction(find_employee_information)

    actions_menu = mainMenu.addMenu('&Employee Actions')

    supervisor_sub_menu = actions_menu.addMenu('Supervisor Actions')
    supervisor_sub_menu.addAction(supervisor_first_action)
    supervisor_sub_menu.addAction(supervisor_seccond_action)
    supervisor_sub_menu.addAction(supervisor_third_action)

    developer_sub_menu = actions_menu.addMenu('Developer Actions')
    developer_sub_menu.addAction(developer_first_action)
    developer_sub_menu.addAction(developer_seccond_action)

    employee_sub_menu = actions_menu.addMenu('Employee Actions')
    employee_sub_menu.addAction(employee_first_action)
    employee_sub_menu.addAction(employee_seccond_action)

    self.home()

  def home(self):
    self.show()

  def new_supervisor(self):
    name, ok = QtWidgets.QInputDialog.getText(
      self, "Add Supervisor", "Enter supervisors name:")
    if not ok:
      raise Exception
    salary, ok = QtWidgets.QInputDialog.getInt(
      self, "Add Supervisor", "Enter supervisor salary:")
    if not ok:
      raise Exception
    ID = new_ID()
    QtWidgets.QMessageBox.information(
      self, "New supervisor ID:", str(ID))
    emp1 = Supervisor(ID, salary, name)

  def new_developer(self):
    name, ok = QtWidgets.QInputDialog.getText(
      self, "Add Developer", "Enter developers name:")
    if not ok:
      raise Exception
    salary, ok = QtWidgets.QInputDialog.getInt(
      self, "Add Developer", "Enter developers salary:")
    if not ok:
      raise Exception
    items = ("C", "C++", "Java", "Python")
    programing_languages = []
    while True:
      programing_language, ok = QtWidgets.QInputDialog.getItem(
        self, "Add Developer", "list of languages", items, 0, False)
      if not ok:
        break
      programing_languages.append(programing_language)
    ID = new_ID()
    QtWidgets.QMessageBox.information(
      self, "New employee ID:", str(ID))
    emp1 = Developer(ID, salary, name, ','.join(programing_languages))

  def new_employee(self):
    name, ok = QtWidgets.QInputDialog.getText(
      self, "Add Employee", "Enter employees name:")
    if not ok:
      raise Exception
    salary, ok = QtWidgets.QInputDialog.getInt(
      self, "Add Employee", "Enter employees salary:")
    if not ok:
      raise Exception
    ID = new_ID()
    QtWidgets.QMessageBox.information(
      self, "New employee ID:", str(ID))
    emp1 = Employee(ID, salary, name)

  def display_employee(self):
    ID, ok = QtWidgets.QInputDialog.getInt(
      self, "Employee Information", "Enter employees ID number:")
    if not ok:
      raise Exception
    check_dictonary(ID)
    employee = employee_dict[ID]
    QtWidgets.QMessageBox.information(
      self, "New Employee", employee.info())

  def add_supervisee_action(self):
    ID, ok = QtWidgets.QInputDialog.getInt(
      self, "Add supervisee", "Enter supervisors ID:")
    if not ok:
      raise Exception
    check_dictonary(ID)
    employee = employee_dict[ID]
    if isinstance(employee, Supervisor):
      newID, ok = QtWidgets.QInputDialog.getInt(
        self, "Add supervisee", "Enter employees ID:")
      if not ok:
        raise Exception
      check_dictonary(newID)
      newemployee = employee_dict[newID]
      employee.add_supervisees(newemployee)
    else:
      print("Not a valid supervisor.")

  def remove_supervisee_action(self):
    ID, ok = QtWidgets.QInputDialog.getInt(
      self, "Remove supervisee", "Enter supervisors ID:")
    if not ok:
      raise Exception
    check_dictonary(ID)
    employee = employee_dict[ID]
    if isinstance(employee, Supervisor):
      newID, ok = QtWidgets.QInputDialog.getInt(
        self, "Remove supervisee", "Enter supervisees ID:")
      if not ok:
        raise Exception
      check_dictonary(newID)
      newemployee = employee_dict[newID]
      employee.remove_supervisees(newemployee)
    else:
      print("Not a valid supervisor.")

  def print_supervisee_action(self):
    ID, ok = QtWidgets.QInputDialog.getInt(
      self, "Print Supervisor Information", "Enter supervisors ID:")
    if not ok:
      raise Exception
    check_dictonary(ID)
    employee = employee_dict[ID]
    if isinstance(employee, Supervisor):
      QtWidgets.QMessageBox.information(
        self, "New Employee", employee.print_supervisees())
    else:
      print("Not a valid supervisor.")

  def developer_actions(self):
    print("Made it.")

  def employee_actions(self):
    print("Made it.")


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


run()

我的PyQt4 GUI按照我想要的方式工作,但是当我尝试运行PyQt5版本时,出现以下错误:

ImportError: /home/server.com/rob/.local/lib/python3.5/site-packages/PyQt5/QtGui.so: undefined symbol: PySlice_AdjustIndices

这是什么错误?起初,我认为这是某种形式的python版本冲突。目前,我正在使用py3.5,但是由于我一直在寻找解决方案,因此我注意到SO上有很多人在运行py3.7以下版本的PyQt5。

1 个答案:

答案 0 :(得分:0)

我更改了一行,因为收到错误:

TypeError: init ()缺少3个必需的位置参数:“ id”,“ salary”和“ name”

...
class Employee:
      #def __init__(self, id, salary, name):
      def __init__(self, id=None, salary=None, name=None):
...

一切正常。检查。

enter image description here