我正在构建一个GUI以模仿我为学习PyQt4编写的Employee程序。目前,我已经设置了GUI以要求一种编程语言。用户可以在这里从语言列表中进行选择。我喜欢以下哪种功能:
def new_developer(self):
name, ok = QtGui.QInputDialog.getText(self, "Add Developer", "Enter developers name:")
salary, ok = QtGui.QInputDialog.getInt(self, "Add Developer", "Enter developers salary:")
items = ("C", "C++", "Java", "Python")
programing_language, ok = QtGui.QInputDialog.getItem(self, "Add Developer",
"list of languages", items, 0, False)
ID = new_ID()
emp1 = Developer(ID, salary, name, programing_language)
我以前使用的另一种方法是简单地让用户输入代表员工编程语言的字符串。
我的问题是当前方法不允许用户选择多种编程语言。我希望有人可以通过更改代码的方法来帮助我,以便用户可以选择多种编程语言。如果需要,请参考以下程序的全部内容:
import sys
import random
from PyQt4 import QtGui
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
employee_dictonary(self)
def info(self):
if isinstance(self, Developer):
return "Employee ID:{} \nSalary:{} \nDevelopers name:{}"\
" \nKnown programing languages:{}".format(self.id,
self.salary,
self.name,
self.programming_language)
else:
return "Employee ID:{} \nEmployees salary:{} "\
"\nEmployees name:".format(self.id, self.salary, self.name)
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 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(QtGui.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_Employee = QtGui.QAction("&Add Employee", self)
create_Employee.triggered.connect(self.new_employee)
create_Developer = QtGui.QAction("&Add Developer", self)
create_Developer.triggered.connect(self.new_developer)
find_employee_information = QtGui.QAction("&Find Employee Information", self)
find_employee_information.triggered.connect(self.display_employee)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileInformationMenu = mainMenu.addMenu('&Employee Information')
fileMenu.addAction(create_Employee)
fileMenu.addAction(create_Developer)
fileInformationMenu.addAction(find_employee_information)
self.home()
def home(self):
self.show()
def new_developer(self):
name, ok = QtGui.QInputDialog.getText(self, "Add Developer", "Enter developers name:")
salary, ok = QtGui.QInputDialog.getInt(self, "Add Developer", "Enter developers salary:")
items = ("C", "C++", "Java", "Python")
programing_language, ok = QtGui.QInputDialog.getItem(self, "Add Developer",
"list of languages", items, 0, False)
ID = new_ID()
emp1 = Developer(ID, salary, name, programing_language)
def new_employee(self):
name, ok = QtGui.QInputDialog.getText(self, "Add Employee", "Enter employees name:")
salary, ok = QtGui.QInputDialog.getInt(self, "Add Employee", "Enter employees salary:")
ID = new_ID()
emp1 = Employee(ID, salary, name)
def display_employee(self):
ID, ok = QtGui.QInputDialog.getInt(self, "Employee Information", "Enter employees ID number:")
employee = employee_dict[ID]
QtGui.QMessageBox.information(self, "New Employee", employee.info())
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()
答案 0 :(得分:2)
如果您要一直调用相同的对话框直到用户取消,并保留所有选择的列表,则只需循环即可。因此,代替此:
programing_language, ok = QtGui.QInputDialog.getItem(self, "Add Developer",
"list of languages", items, 0, False)
…执行此操作:
progamming_languages = []
while True:
programing_language, ok = QtGui.QInputDialog.getItem(self, "Add Developer",
"list of languages", items, 0, False)
if not ok:
break
programming_languages.append(progamming_language)
我们正在使用它,实际上您应该检查ok
而不是在所有其他情况下都忽略它。毕竟,如果用户单击Cancel
而不是在输入姓名时输入姓名,您是否真的要继续提问并最终以空名创建新员工?
此外,如果您没有在屏幕边缘附近开始悬挂的缩进,则代码的可读性将大大提高。例如,代替此:
programing_language, ok = QtGui.QInputDialog.getItem(self, "Add Developer",
"list of languages", items, 0, False)
…执行此操作:
programing_language, ok = QtGui.QInputDialog.getItem(
self, "Add Developer", "list of languages", items, 0, False)
…甚至是这样:
programing_language, ok = QtGui.QInputDialog.getItem(
self, "Add Developer", "list of languages", items, 0, False
)
或者更好的是,获得black
或yapf
之类的自动格式化程序,并将其设置为在保存时自动格式化代码,这样您就不必考虑这样的事情