PySide2 Pop-up Window -- QInputDialog QMessageBox QFileDialog

Foreword

Original article:
https://blog.csdn.net/weixin_50113231/article/details/124102931

1. QInputDialog

Official Document: https://doc.qt.io/qt-5.15/QInputDialog.html

1.1 Input Integer

The function of the next four numbers is initial value, minimum value, maximum, stride.

1
value, ok = QInputDialog.getInt(self, "Title", "Prompt information", 37, -10000, 10000, 2)

1.2 Input Decimal

The function of the next four numbers is initial value, minimum value, maximum, number of decimal places.

1
value, ok = QInputDialog.getDouble(self, "Title", "Prompt information", 37.56, -10000, 10000, 2)

1.3 Input Text

The third parameter indicates the display type: QLineEdit.Normal, QLineEdit. Password, QLineEdit. NoEcho

1
value, ok = QInputDialog.getText(self, "Title", "Prompt information", QLineEdit.Normal, "This is the default value")

1.4 Input Multiple Text

1
value, ok = QInputDialog.getMultiLineText(self, "Title", "Prompt information", "The default \n My address is \n Guangzhou Panyu, Guangdong, China")

1.5 Input Options

1
2
3
# `1` Selects the selected items by default. True/False Indicates whether the list box can be edited.
items = ["Spring", "Summer", "Fall", "Winter"]
value, ok = QInputDialog.getItem(self, "Title", "Prompt information", items, 1, True)

1.6 Sample

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
import sys
from PySide2.QtWidgets import *


class MyWindow(QWidget):

def __init__(self):
super().__init__()

self.setWindowTitle('PySide2 pop-up window')
self.resize(400, 300)
h1 = QHBoxLayout(self)
btn = QPushButton('pop-up window')
btn.clicked.connect(self.do_btn)
h1.addWidget(btn)

def do_btn(self, event):
value, ok = QInputDialog.getInt(self, "Title", "Prompt information", 37, -10000, 10000, 2)


if __name__ == "__main__":
app = QApplication()
win = MyWindow()
win.show()
sys.exit(app.exec_())

2. QMessageBox

Official Document: https://doc.qt.io/qt-5.15/QMessageBox.html

2.1 Information QMessageBox

Respectively after two buttons (separated with |, a total of 7 types of button, after see sample) and the default button (omit the default for the first button)

1
reply = QMessageBox.information(self, "Title", "This is a message.", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)

2.2 Question QMessageBox

1
reply = QMessageBox.question(self, "Title", "Is this a question ?", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)

2.3 Warning QMessageBox

1
reply = QMessageBox.warning(self, "Title", "This is a warning.", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)

2.4 Critical QMessageBox

1
reply = QMessageBox.critical(self, 'Title', 'Critical error dialog box message body.', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)

2.5 About QMessageBox

There is no need to insert the button type.

1
reply = QMessageBox.about(self, "Title", "This is a description of the software.")

2.6 Sample

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
50
51
52
53
54
55
56
57
58
59
60
import sys
from PySide2.QtWidgets import *


class MyWindow(QWidget):

def __init__(self):
super().__init__()

self.setWindowTitle('PySide2 Pop-up window')
self.resize(400, 300)
# self is added as a global layout, not as a local layout
h1 = QVBoxLayout(self)

btn1 = QPushButton('Information')
btn1.clicked.connect(self.handle_information_button)

btn2 = QPushButton('Question')
btn2.clicked.connect(self.handle_question_button)

btn3 = QPushButton('Warning')
btn3.clicked.connect(self.handle_warning_button)

btn4 = QPushButton('Critical')
btn4.clicked.connect(self.handle_critical_button)

btn5 = QPushButton('About')
btn5.clicked.connect(self.handle_about_button)

h1.addWidget(btn1)
h1.addWidget(btn2)
h1.addWidget(btn3)
h1.addWidget(btn4)
h1.addWidget(btn5)

def handle_information_button(self):
reply = QMessageBox.information(self, "Title", "This is a message.", QMessageBox.Yes | QMessageBox.No,
QMessageBox.Yes)

def handle_question_button(self):
reply = QMessageBox.question(self, "Title", "Is this a question ?", QMessageBox.Yes | QMessageBox.No,
QMessageBox.Yes)

def handle_warning_button(self):
reply = QMessageBox.warning(self, "Title", "This is a warning.", QMessageBox.Yes | QMessageBox.No,
QMessageBox.Yes)

def handle_critical_button(self):
reply = QMessageBox.critical(self, 'Title', 'Critical error dialog box message body.',
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)

def handle_about_button(self):
reply = QMessageBox.about(self, "Title", "This is a description of the software.")


if __name__ == "__main__":
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())

2.7 Button Type

(1) Standard button type

Constant Description
QMessageBox.Ok An “OK” button defined with the AcceptRole.
QMessageBox.Cancel A “Cancel” button defined with the RejectRole.
QMessageBox.Yes A “Yes” button defined with the YesRole.
QMessageBox.No A “No” button defined with the NoRole.
QMessageBox.Abort An “Abort” button defined with the RejectRole.
QMessageBox.Retry A “Retry” button defined with the AcceptRole.
QMessageBox.Ignore An “Ignore” button defined with the AcceptRole.

You can see the details:
https://doc.qt.io/qt-5.15/qmessagebox.html#default-and-escape-keys

(2) Customize the button type

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
import sys
from PySide2.QtWidgets import *


class MyWindow(QWidget):

def __init__(self):
super().__init__()

self.setWindowTitle('PyQt5 弹出窗口')
self.resize(400, 300)
# 加了self为全局布局,没加为局部布局
h1 = QHBoxLayout(self)
btn = QPushButton('弹出窗口')
btn.clicked.connect(self.do_btn)
h1.addWidget(btn)

def do_btn(self):
# 创建Question消息框
self.reply = QMessageBox(QMessageBox.Question, "消息框标题", "这是一条消息。")
# 添加自定义按钮
self.reply.addButton('确定', QMessageBox.YesRole)
self.reply.addButton('取消', QMessageBox.NoRole)
# 设置消息框中内容前面的图标
# self.reply.setIcon(1)
self.reply.show()


if __name__ == "__main__":
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())

Watch out!! Reply needs to be an object property of the window (self.reply), otherwise the window will flash by!!

3. QFileDialog

Official Document: https://doc.qt.io/qt-5.15/QFileDialog.html

3.1 Directory

1
directory = QFileDialog.getExistingDirectory(self, "Select a directory", "C:/")  # The initial path

3.2 Singe File

1
2
# Set file extension filtering. Use a double semicolon interval
file_, filetype = QFileDialog.getOpenFileName(self, "Select the file", "C:/", "All Files (*);;Text Files (*.txt)")

3.3 Multiple Files

1
files, ok = QFileDialog.getOpenFileNames(self, "Multiple files selection", "C:/", "All Files (*);;Text Files (*.txt)")

Ctrl button to select multiple files.

3.4 Save

1
file_, ok = QFileDialog.getSaveFileName(self, "File saving", "C:/", "All Files (*);;Text Files (*.txt)")

3.5 Save as

1
file_, ok = QFileDialog.getSaveFileName(self, "Save the file as", "C:/", "All Files (*);;Text Files (*.txt)")

No different from 4.

3.6 Sample

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
import sys
from PySide2.QtWidgets import *


class MyWindow(QWidget):

def __init__(self):
super().__init__()

self.setWindowTitle('PySide2 pop-up window')
self.resize(400, 300)
h1 = QHBoxLayout(self)
btn = QPushButton('pop-up window')
btn.clicked.connect(self.do_btn)
h1.addWidget(btn)

def do_btn(self, event):
dir_ = QFileDialog.getExistingDirectory(self, "Select a directory", "C:/") # The initial path


if __name__ == "__main__":
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())

PySide2 Pop-up Window -- QInputDialog QMessageBox QFileDialog
https://www.hardyhu.cn/2022/08/09/PySide2-Pop-up-Window-QInputDialog-QMessageBox-QFileDialog/
Author
John Doe
Posted on
August 9, 2022
Licensed under