1 面向过程版

  1. from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QPlainTextEdit, QMessageBox
  2. def HandleCalc():
  3. info = textEdit.toPlainText()
  4. # 薪资20000 以上 和 以下 的人员名单
  5. salary_above_20k = ''
  6. salary_below_20k = ''
  7. for line in info.splitlines():
  8. # 去除首尾空白符后字符为空, 则跳过
  9. if not line.strip():
  10. continue
  11. parts = line.split(' ')
  12. # 去掉列表中的空字符串内容
  13. parts = [p for p in parts if p]
  14. name, salary, age = parts
  15. if int(salary) >= 20000:
  16. salary_above_20k += name + '\n'
  17. else:
  18. salary_below_20k += name + '\n'
  19. QMessageBox.about(window,
  20. '统计结果',
  21. f'''薪资20000 以上的有:\n{salary_above_20k}
  22. \n薪资20000 以下的有:\n{salary_below_20k}'''
  23. )
  24. app = QApplication()
  25. window = QMainWindow()
  26. window.resize(500, 400)
  27. window.move(300, 310)
  28. window.setWindowTitle('薪资统计')
  29. textEdit = QPlainTextEdit(window)
  30. textEdit.setPlaceholderText("请输入薪资表")
  31. textEdit.move(10,25)
  32. textEdit.resize(300,350)
  33. button = QPushButton('统计', window)
  34. button.move(380,80)
  35. button.clicked.connect(HandleCalc)
  36. window.show()
  37. app.exec_()

image.png

2 面向对象版

  1. from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QPlainTextEdit,QMessageBox
  2. class Stats():
  3. def __init__(self):
  4. self.window = QMainWindow()
  5. self.window.resize(500, 400)
  6. self.window.move(300, 300)
  7. self.window.setWindowTitle('薪资统计')
  8. self.textEdit = QPlainTextEdit(self.window)
  9. self.textEdit.setPlaceholderText("请输入薪资表")
  10. self.textEdit.move(10, 25)
  11. self.textEdit.resize(300, 350)
  12. self.button = QPushButton('统计', self.window)
  13. self.button.move(380, 80)
  14. self.button.clicked.connect(self.handleCalc)
  15. def handleCalc(self):
  16. info = self.textEdit.toPlainText()
  17. # 薪资20000 以上 和 以下 的人员名单
  18. salary_above_20k = ''
  19. salary_below_20k = ''
  20. for line in info.splitlines():
  21. if not line.strip():
  22. continue
  23. parts = line.split(' ')
  24. # 去掉列表中的空字符串内容
  25. parts = [p for p in parts if p]
  26. name,salary,age = parts
  27. if int(salary) >= 20000:
  28. salary_above_20k += name + '\n'
  29. else:
  30. salary_below_20k += name + '\n'
  31. QMessageBox.about(self.window,
  32. '统计结果',
  33. f'''薪资20000 以上的有:\n{salary_above_20k}
  34. \n薪资20000 以下的有:\n{salary_below_20k}'''
  35. )
  36. app = QApplication()
  37. stats = Stats()
  38. stats.window.show()
  39. app.exec_()

3 使用QtDesigner创建UI界面

image.png

  1. # 调用代码-动态加载UI界面
  2. from PySide2.QtWidgets import QApplication, QMessageBox
  3. from PySide2.QtUiTools import QUiLoader
  4. class Stats():
  5. def __init__(self):
  6. # 从 UI 定义中动态 创建一个相应的窗口对象
  7. # 注意:里面的控件对象也成为窗口对象的属性了
  8. self.ui = QUiLoader().load("statsUI.ui")
  9. self.ui.btn_1.clicked.connect(self.handleCalc)
  10. def handleCalc(self):
  11. info = self.ui.pedt_1.toPlainText()
  12. salary_above_20k = ''
  13. salary_below_20k = ''
  14. for line in info.splitlines():
  15. if not line.strip():
  16. continue
  17. parts = line.split(' ')
  18. parts = [p for p in parts if p]
  19. name,salary,age = parts
  20. if int(salary) >= 20000:
  21. salary_above_20k += name + '\n'
  22. else:
  23. salary_below_20k += name + '\n'
  24. QMessageBox.about(self.ui,
  25. '统计结果',
  26. f'''薪资20000 以上的有:\n{salary_above_20k}
  27. \n薪资20000 以下的有:\n{salary_below_20k}'''
  28. )
  29. app = QApplication()
  30. stats = Stats()
  31. stats.ui.show()
  32. app.exec_()