Skip to content

Commit bbbcfd7

Browse files
committed
add qdialog
1 parent c538840 commit bbbcfd7

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed

QDialog/dialog_test.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.QtCore import *
4+
from PyQt5.QtGui import *
5+
from PyQt5.QtWidgets import *
6+
7+
8+
class DialogDemo(QMainWindow):
9+
10+
def __init__(self, parent=None):
11+
super(DialogDemo, self).__init__(parent)
12+
self.setWindowTitle("Dialog demo")
13+
self.resize(600, 400)
14+
15+
self.button = QPushButton(self)
16+
self.button.setText("点击弹出对话框")
17+
self.button.move(50, 50)
18+
self.button.clicked.connect(self.showDialog)
19+
20+
def showDialog(self):
21+
dialog = QDialog()
22+
btn = QPushButton("ok", dialog)
23+
btn.move(50, 50)
24+
dialog.setWindowTitle("Dialog")
25+
# Qt.WindowModal
26+
dialog.setWindowModality(Qt.WindowModal)
27+
dialog.exec_()
28+
29+
if __name__ == '__main__':
30+
app = QApplication(sys.argv)
31+
demo = DialogDemo()
32+
demo.show()
33+
sys.exit(app.exec_())

QDialog/qfiledialog_test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
3+
from PyQt5.QtCore import *
4+
from PyQt5.QtGui import *
5+
from PyQt5.QtWidgets import *
6+
7+
8+
class DialogDemo(QMainWindow):
9+
10+
def __init__(self, parent=None):
11+
super(DialogDemo, self).__init__(parent)
12+
self.setWindowTitle("Dialog demo")
13+
self.resize(600, 400)
14+
15+
self.button = QPushButton(self)
16+
self.button.setText("点击弹出文件对话框")
17+
self.button.move(50, 50)
18+
self.button.clicked.connect(self.showDialog)
19+
20+
def showDialog(self):
21+
filename, _ = QFileDialog.getOpenFileName(self, "打开文件", "D:\\", "Image Files (*.jpg *.png)")
22+
if filename:
23+
print(f"file: {filename}")
24+
25+
26+
if __name__ == '__main__':
27+
app = QApplication(sys.argv)
28+
demo = DialogDemo()
29+
demo.show()
30+
sys.exit(app.exec_())

QDialog/qfontdialog_test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
3+
from PyQt5.QtCore import *
4+
from PyQt5.QtGui import *
5+
from PyQt5.QtWidgets import *
6+
7+
8+
class DialogDemo(QMainWindow):
9+
10+
def __init__(self, parent=None):
11+
super(DialogDemo, self).__init__(parent)
12+
self.setWindowTitle("Dialog demo")
13+
self.resize(600, 400)
14+
15+
self.button = QPushButton(self)
16+
self.button.setText("点击弹出字体对话框")
17+
self.button.move(50, 50)
18+
self.button.clicked.connect(self.showDialog)
19+
20+
def showDialog(self):
21+
font, ok = QFontDialog.getFont()
22+
if ok:
23+
print(f'font: {font.pointSize()}')
24+
25+
26+
if __name__ == '__main__':
27+
app = QApplication(sys.argv)
28+
demo = DialogDemo()
29+
demo.show()
30+
sys.exit(app.exec_())

QDialog/qinputdialog_test.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.QtCore import *
4+
from PyQt5.QtGui import *
5+
from PyQt5.QtWidgets import *
6+
7+
8+
class DialogDemo(QMainWindow):
9+
10+
def __init__(self, parent=None):
11+
super(DialogDemo, self).__init__(parent)
12+
self.setWindowTitle("QInputDialog demo")
13+
self.resize(600, 400)
14+
15+
self.button = QPushButton(self)
16+
self.button.setText("点击弹出QInputDialog")
17+
self.button.move(50, 50)
18+
self.button.clicked.connect(self.showDialog)
19+
20+
def showDialog(self):
21+
name, okPressed = QInputDialog.getText(self, "迷途小书童", "请输入你的大名:", QLineEdit.Normal, " ")
22+
if okPressed and name:
23+
print(f'Welcome {name}')
24+
else:
25+
QMessageBox.critical(self, "Error", "请输入大名并点击OK!")
26+
27+
28+
if __name__ == '__main__':
29+
app = QApplication(sys.argv)
30+
demo = DialogDemo()
31+
demo.show()
32+
sys.exit(app.exec_())

QDialog/qmessagebox_test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
3+
from PyQt5.QtCore import *
4+
from PyQt5.QtGui import *
5+
from PyQt5.QtWidgets import *
6+
7+
8+
class DialogDemo(QMainWindow):
9+
10+
def __init__(self, parent=None):
11+
super(DialogDemo, self).__init__(parent)
12+
self.setWindowTitle("QMessageBox demo")
13+
self.resize(600, 400)
14+
15+
self.button = QPushButton(self)
16+
self.button.setText("点击弹出QMessageBox")
17+
self.button.move(50, 50)
18+
self.button.clicked.connect(self.showDialog)
19+
20+
def showDialog(self):
21+
# warning、critical、question
22+
reply = QMessageBox.critical(self, "标题", "消息正文", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
23+
print(reply)
24+
25+
26+
if __name__ == '__main__':
27+
app = QApplication(sys.argv)
28+
demo = DialogDemo()
29+
demo.show()
30+
sys.exit(app.exec_())

0 commit comments

Comments
 (0)