PySide2 First simple case

Foreword

Original article:
https://www.byhy.net/tut/py/gui/qt_02/

1. Start with an example

Now let’s develop a program that lets the user enter a text containing:
Employee name, salary, and age.

The format is as follow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
薛蟠     4560 25
薛蝌 4460 25
薛宝钗 35776 23
薛宝琴 14346 18
王夫人 43360 45
王熙凤 24460 25
王子腾 55660 45
王仁 15034 65
尤二姐 5324 24
贾芹 5663 25
贾兰 13443 35
贾芸 4522 25
尤三姐 5905 22
贾珍 54603 35

The program can print out the list of people with salaries above and below 20,000.

Of course, we can do the same as before, and develop a command line program, and let the user type on the character terminal.

But it would be even cooler if we could develop a graphical interface program like the following.

In fact, with PySide2, this short program is all you need to develop the interface above

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QPlainTextEdit

app = QApplication([])

window = QMainWindow()
window.resize(500, 400)
window.move(300, 310)
window.setWindowTitle('薪资统计')

textEdit = QPlainTextEdit(window)
textEdit.setPlaceholderText("请输入薪资表")
textEdit.move(10, 25)
textEdit.resize(300, 350)

button = QPushButton('统计', window)
button.move(380, 80)

window.show()

app.exec_()

2. Interfae action processing (Signal and Slot)

When the user clicks the statistics button, the user input string content is obtained from the interface contorl QPlainTextEdit for processing.

But how to you notify the program when the user clicks the statistics button? Only if the program is notify of the click can it do anything about it.

In fact, QT use Signal and Slot to achieve this association.

We can define a function as follows:

1
2
def handleCalc():
print('统计按钮被点击了')

Then, specify that if a button is clicked, you need handleCalc to handle it, like this:

1
button.clicked.connect(handleCalc)

After our modification, the code is as follow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QPlainTextEdit, QMessageBox


def handleCalc():
info = 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(window,
'统计结果',
f'''薪资20000 以上的有:\n{salary_above_20k}
\n薪资20000 以下的有:\n{salary_below_20k}'''
)


app = QApplication([])

window = QMainWindow()
window.resize(500, 400)
window.move(300, 300)
window.setWindowTitle('薪资统计')

textEdit = QPlainTextEdit(window)
textEdit.setPlaceholderText("请输入薪资表")
textEdit.move(10, 25)
textEdit.resize(300, 350)

button = QPushButton('统计', window)
button.move(380, 80)
button.clicked.connect(handleCalc)

window.show()

app.exec_()

When you run it, you’ll find the following results:

3. Encapsulate into a class

The code above treats all the variable names corresponding to the control as global variable.

If you were to design a slightly more complex program, you would have too many variable names for the controls.

It’s also bad for modularity.

Therefore, we should usually warp a windo and its contained controls, the corresponding code all into the class, as shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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_()

PySide2 First simple case
https://www.hardyhu.cn/2022/08/05/PySide2-First-simple-case/
Author
John Doe
Posted on
August 5, 2022
Licensed under