Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4ed7989

Browse files
committed
PyQt5 Dialogs
1 parent 58ef27f commit 4ed7989

File tree

6 files changed

+207
-0
lines changed

6 files changed

+207
-0
lines changed

‎pyqt5/tutorials/dialogs/basic_app.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
3+
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
4+
5+
6+
class MainWindow(QMainWindow):
7+
def __init__(self):
8+
super().__init__()
9+
10+
self.setWindowTitle("My App")
11+
12+
button = QPushButton("Press me for a dialog!")
13+
button.clicked.connect(self.button_clicked)
14+
self.setCentralWidget(button)
15+
16+
def button_clicked(self, s):
17+
print("click", s)
18+
19+
20+
app = QApplication(sys.argv)
21+
window = MainWindow()
22+
window.show()
23+
app.exec()

‎pyqt5/tutorials/dialogs/critical_box.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
3+
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QPushButton
4+
5+
6+
class MainWindow(QMainWindow):
7+
def __init__(self):
8+
super().__init__()
9+
10+
self.setWindowTitle("My App")
11+
12+
button = QPushButton("Press me for a dialog!")
13+
button.clicked.connect(self.button_clicked)
14+
self.setCentralWidget(button)
15+
16+
def button_clicked(self, s):
17+
button = QMessageBox.critical(
18+
self,
19+
"Oh dear!",
20+
"Something went very wrong.",
21+
buttons=QMessageBox.Discard | QMessageBox.NoToAll | QMessageBox.Ignore,
22+
defaultButton=QMessageBox.Discard,
23+
)
24+
25+
if button == QMessageBox.Discard:
26+
print("Discard!")
27+
elif button == QMessageBox.NoToAll:
28+
print("No to all!")
29+
else:
30+
print("Ignore!")
31+
32+
33+
app = QApplication(sys.argv)
34+
window = MainWindow()
35+
window.show()
36+
app.exec()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import sys
2+
3+
from PyQt5.QtWidgets import (
4+
QApplication,
5+
QDialog,
6+
QDialogButtonBox,
7+
QLabel,
8+
QMainWindow,
9+
QPushButton,
10+
QVBoxLayout,
11+
)
12+
13+
14+
class MainWindow(QMainWindow):
15+
def __init__(self):
16+
super().__init__()
17+
18+
self.setWindowTitle("My App")
19+
20+
button = QPushButton("Press me for a dialog!")
21+
button.clicked.connect(self.button_clicked)
22+
self.setCentralWidget(button)
23+
24+
def button_clicked(self, s):
25+
print("click", s)
26+
27+
dlg = CustomDialog(self)
28+
if dlg.exec():
29+
print("Success!")
30+
else:
31+
print("Cancel!")
32+
33+
34+
class CustomDialog(QDialog):
35+
def __init__(self, parent=None):
36+
super().__init__(parent)
37+
38+
self.setWindowTitle("HELLO!")
39+
40+
QBtn = QDialogButtonBox.Ok | QDialogButtonBox.Cancel
41+
42+
self.buttonBox = QDialogButtonBox(QBtn)
43+
self.buttonBox.accepted.connect(self.accept)
44+
self.buttonBox.rejected.connect(self.reject)
45+
46+
layout = QVBoxLayout()
47+
message = QLabel("Something happened, is that OK?")
48+
layout.addWidget(message)
49+
layout.addWidget(self.buttonBox)
50+
self.setLayout(layout)
51+
52+
53+
app = QApplication(sys.argv)
54+
window = MainWindow()
55+
window.show()
56+
app.exec()

‎pyqt5/tutorials/dialogs/message_box.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
3+
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QPushButton
4+
5+
6+
class MainWindow(QMainWindow):
7+
def __init__(self):
8+
super().__init__()
9+
10+
self.setWindowTitle("My App")
11+
12+
button = QPushButton("Press me for a dialog!")
13+
button.clicked.connect(self.button_clicked)
14+
self.setCentralWidget(button)
15+
16+
def button_clicked(self, s):
17+
dlg = QMessageBox(self)
18+
dlg.setWindowTitle("I have a question!")
19+
dlg.setText("This is a question dialog")
20+
dlg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
21+
dlg.setIcon(QMessageBox.Icon.Question)
22+
button = dlg.exec()
23+
24+
if button == QMessageBox.Yes:
25+
print("Yes!")
26+
else:
27+
print("No!")
28+
29+
30+
app = QApplication(sys.argv)
31+
window = MainWindow()
32+
window.show()
33+
app.exec()

‎pyqt5/tutorials/dialogs/qdialog.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sys
2+
3+
from PyQt6.QtWidgets import QApplication, QDialog, QMainWindow, QPushButton
4+
5+
6+
class MainWindow(QMainWindow):
7+
def __init__(self):
8+
super().__init__()
9+
10+
self.setWindowTitle("My App")
11+
12+
button = QPushButton("Press me for a dialog!")
13+
button.clicked.connect(self.button_clicked)
14+
self.setCentralWidget(button)
15+
16+
def button_clicked(self, s):
17+
print("click", s)
18+
19+
dlg = QDialog(self)
20+
dlg.setWindowTitle("HELLO!")
21+
dlg.exec()
22+
23+
24+
app = QApplication(sys.argv)
25+
window = MainWindow()
26+
window.show()
27+
app.exec()

‎pyqt5/tutorials/dialogs/question_box.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
3+
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QPushButton
4+
5+
6+
class MainWindow(QMainWindow):
7+
def __init__(self):
8+
super().__init__()
9+
10+
self.setWindowTitle("My App")
11+
12+
button = QPushButton("Press me for a dialog!")
13+
button.clicked.connect(self.button_clicked)
14+
self.setCentralWidget(button)
15+
16+
def button_clicked(self, s):
17+
button = QMessageBox.question(
18+
self,
19+
"Question dialog",
20+
"The longer message",
21+
)
22+
23+
if button == QMessageBox.StandardButton.Yes:
24+
print("Yes!")
25+
else:
26+
print("No!")
27+
28+
29+
app = QApplication(sys.argv)
30+
window = MainWindow()
31+
window.show()
32+
app.exec()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /