PyQT 图形化编程学习笔记
安装Pyside2
执行
pip install pyside2 -i https://pypi.douban.com/simple/
python安装的第三方库都会放在python安装目录下的Lib/site-packages中。
QT Designer
右侧的边栏展示的是 一种继承关系。此时,点中PlainTextEdit,红框中显示在其上的条目都是一层一层的父类。
Question List
2023/12/16
一:
Q1. if not line.strip() 什么鬼作用
Q2. 13行的鬼东西啥意思,去除空的字符?
Q3. 15行,python这是什么操作?这也行?
- if not ==> 判断后面内容是否为none。在python中 None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组() 都相当于False。如果是none则执行if not 判断体中的内容
- 列表推导式,如果列表中的值不是空,则取出列表中的值赋给第一个p。
eg:
lis = [i for i in range(100) if i % 2 == 0]
print(lis)
range()用法: range(start, stop[, step])
range(10) # 从 0 开始到 9
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(1, 11) # 从 1 开始到 10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
range(0, 30, 5) # 步长为 5
[0, 5, 10, 15, 20, 25]
range(0, 10, 3) # 步长为 3
[0, 3, 6, 9]
range(0, -10, -1) # 负数
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
range(0)
[]
range(1, 0)
[]
二:
from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QPlainTextEdit,QMessageBox
class Stats():
def __init__(self):
self.window = QMainWindow()
self.window.resize(500, 400)
self.window.move(300, 300)
self.window.setWindowTitle('薪资统计')
self.textEdit = QPlainTextEdit(self.window)
self.textEdit.setPlaceholderText("请输入薪资表")
self.textEdit.move(10, 25)
self.textEdit.resize(300, 350)
self.button = QPushButton('统计', self.window)
self.button.move(380, 80)
self.button.clicked.connect(self.handleCalc)
def handleCalc(self):
info = self.textEdit.toPlainText()
# 薪资20000 以上 和 以下 的人员名单
salary_above_20k = ''
salary_below_20k = ''
for line in info.splitlines():
if not line.strip():
continue
parts = line.split(' ')
# 去掉列表中的空字符串内容
parts = [p for p in parts if p]
name,salary,age = parts
if int(salary) >= 20000:
salary_above_20k += name + '\n'
else:
salary_below_20k += name + '\n'
QMessageBox.about(self.window,
'统计结果',
f'''薪资20000 以上的有:\n{salary_above_20k}
\n薪资20000 以下的有:\n{salary_below_20k}'''
)
app = QApplication([])
stats = Stats()
stats.window.show()
app.exec_()
- init(self):相当于构造函数?
- init(self)里面的变量都加了self是什么意思?