Skip to content

Commit dd686ff

Browse files
committed
PySide6 Dialogs
1 parent 5eddd62 commit dd686ff

File tree

7 files changed

+212
-0
lines changed

7 files changed

+212
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
3+
from PySide6.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()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
3+
from PySide6.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 PySide6.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()
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()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
3+
from PySide6.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 simple dialog")
20+
button = dlg.exec()
21+
22+
if button == QMessageBox.Yes:
23+
print("Yes!")
24+
else:
25+
print("No!")
26+
27+
28+
app = QApplication(sys.argv)
29+
window = MainWindow()
30+
window.show()
31+
app.exec()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def button_clicked(self, s):
2+
3+
button = QMessageBox.question(self, "Question dialog", "The longer message")
4+
5+
if button == QMessageBox.Yes:
6+
print("Yes!")
7+
else:
8+
print("No!")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sys
2+
3+
from PySide6.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()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
3+
from PySide6.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+
if button == QMessageBox.Yes:
23+
print("Yes!")
24+
else:
25+
print("No!")
26+
27+
28+
app = QApplication(sys.argv)
29+
window = MainWindow()
30+
window.show()
31+
app.exec()

0 commit comments

Comments
 (0)