对话框

对话框是一个现代GUI应用不可或缺的一部分。对话是两个人之间的交流,对话框就是人与电脑之间的对话。对话框用来输入数据,修改数据,修改应用设置等等。

输入文字

QInputDialog提供了一个简单方便的对话框,可以输入字符串,数字或列表。

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. In this example, we receive data from
  6. a QInputDialog dialog.
  7. Aauthor: Jan Bodnar
  8. Website: zetcode.com
  9. Last edited: August 2017
  10. """
  11. from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
  12. QInputDialog, QApplication)
  13. import sys
  14. class Example(QWidget):
  15. def __init__(self):
  16. super().__init__()
  17. self.initUI()
  18. def initUI(self):
  19. self.btn = QPushButton('Dialog', self)
  20. self.btn.move(20, 20)
  21. self.btn.clicked.connect(self.showDialog)
  22. self.le = QLineEdit(self)
  23. self.le.move(130, 22)
  24. self.setGeometry(300, 300, 290, 150)
  25. self.setWindowTitle('Input dialog')
  26. self.show()
  27. def showDialog(self):
  28. text, ok = QInputDialog.getText(self, 'Input Dialog',
  29. 'Enter your name:')
  30. if ok:
  31. self.le.setText(str(text))
  32. if __name__ == '__main__':
  33. app = QApplication(sys.argv)
  34. ex = Example()
  35. sys.exit(app.exec_())

这个示例有一个按钮和一个输入框,点击按钮显示对话框,输入的文本会显示在输入框里。

  1. text, ok = QInputDialog.getText(self, 'Input Dialog',
  2. 'Enter your name:')

这是显示一个输入框的代码。第一个参数是输入框的标题,第二个参数是输入框的占位符。对话框返回输入内容和一个布尔值,如果点击的是OK按钮,布尔值就返回True。

  1. if ok:
  2. self.le.setText(str(text))

把得到的字符串放到输入框里。

程序展示:

input dialog

选取颜色

QColorDialog提供颜色的选择。

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. In this example, we select a color value
  6. from the QColorDialog and change the background
  7. color of a QFrame widget.
  8. Author: Jan Bodnar
  9. Website: zetcode.com
  10. Last edited: August 2017
  11. """
  12. from PyQt5.QtWidgets import (QWidget, QPushButton, QFrame,
  13. QColorDialog, QApplication)
  14. from PyQt5.QtGui import QColor
  15. import sys
  16. class Example(QWidget):
  17. def __init__(self):
  18. super().__init__()
  19. self.initUI()
  20. def initUI(self):
  21. col = QColor(0, 0, 0)
  22. self.btn = QPushButton('Dialog', self)
  23. self.btn.move(20, 20)
  24. self.btn.clicked.connect(self.showDialog)
  25. self.frm = QFrame(self)
  26. self.frm.setStyleSheet("QWidget { background-color: %s }"
  27. % col.name())
  28. self.frm.setGeometry(130, 22, 100, 100)
  29. self.setGeometry(300, 300, 250, 180)
  30. self.setWindowTitle('Color dialog')
  31. self.show()
  32. def showDialog(self):
  33. col = QColorDialog.getColor()
  34. if col.isValid():
  35. self.frm.setStyleSheet("QWidget { background-color: %s }"
  36. % col.name())
  37. if __name__ == '__main__':
  38. app = QApplication(sys.argv)
  39. ex = Example()
  40. sys.exit(app.exec_())

例子里有一个按钮和一个QFrame,默认的背景颜色为黑色,我们可以使用QColorDialog改变背景颜色。

  1. col = QColor(0, 0, 0)

初始化QtGui.QFrame的背景颜色。

  1. col = QColorDialog.getColor()

弹出一个QColorDialog对话框。

  1. if col.isValid():
  2. self.frm.setStyleSheet("QWidget { background-color: %s }"
  3. % col.name())

我们可以预览颜色,如果点击取消按钮,没有颜色值返回,如果颜色是我们想要的,就从取色框里选择这个颜色。

程序展示:

color dialog

选择字体

QFontDialog能做字体的选择。

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. In this example, we select a font name
  6. and change the font of a label.
  7. Author: Jan Bodnar
  8. Website: zetcode.com
  9. Last edited: August 2017
  10. """
  11. from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton,
  12. QSizePolicy, QLabel, QFontDialog, QApplication)
  13. import sys
  14. class Example(QWidget):
  15. def __init__(self):
  16. super().__init__()
  17. self.initUI()
  18. def initUI(self):
  19. vbox = QVBoxLayout()
  20. btn = QPushButton('Dialog', self)
  21. btn.setSizePolicy(QSizePolicy.Fixed,
  22. QSizePolicy.Fixed)
  23. btn.move(20, 20)
  24. vbox.addWidget(btn)
  25. btn.clicked.connect(self.showDialog)
  26. self.lbl = QLabel('Knowledge only matters', self)
  27. self.lbl.move(130, 20)
  28. vbox.addWidget(self.lbl)
  29. self.setLayout(vbox)
  30. self.setGeometry(300, 300, 250, 180)
  31. self.setWindowTitle('Font dialog')
  32. self.show()
  33. def showDialog(self):
  34. font, ok = QFontDialog.getFont()
  35. if ok:
  36. self.lbl.setFont(font)
  37. if __name__ == '__main__':
  38. app = QApplication(sys.argv)
  39. ex = Example()
  40. sys.exit(app.exec_())

我们创建了一个有一个按钮和一个标签的QFontDialog的对话框,我们可以使用这个功能修改字体样式。

  1. font, ok = QFontDialog.getFont()

弹出一个字体选择对话框。getFont()方法返回一个字体名称和状态信息。状态信息有OK和其他两种。

  1. if ok:
  2. self.label.setFont(font)

如果点击OK,标签的字体就会随之更改。

程序展示:

font dialog

选择文件

QFileDialog给用户提供文件或者文件夹选择的功能。能打开和保存文件。

  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ZetCode PyQt5 tutorial
  5. In this example, we select a file with a
  6. QFileDialog and display its contents
  7. in a QTextEdit.
  8. Author: Jan Bodnar
  9. Website: zetcode.com
  10. Last edited: August 2017
  11. """
  12. from PyQt5.QtWidgets import (QMainWindow, QTextEdit,
  13. QAction, QFileDialog, QApplication)
  14. from PyQt5.QtGui import QIcon
  15. import sys
  16. class Example(QMainWindow):
  17. def __init__(self):
  18. super().__init__()
  19. self.initUI()
  20. def initUI(self):
  21. self.textEdit = QTextEdit()
  22. self.setCentralWidget(self.textEdit)
  23. self.statusBar()
  24. openFile = QAction(QIcon('open.png'), 'Open', self)
  25. openFile.setShortcut('Ctrl+O')
  26. openFile.setStatusTip('Open new File')
  27. openFile.triggered.connect(self.showDialog)
  28. menubar = self.menuBar()
  29. fileMenu = menubar.addMenu('&File')
  30. fileMenu.addAction(openFile)
  31. self.setGeometry(300, 300, 350, 300)
  32. self.setWindowTitle('File dialog')
  33. self.show()
  34. def showDialog(self):
  35. fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
  36. if fname[0]:
  37. f = open(fname[0], 'r')
  38. with f:
  39. data = f.read()
  40. self.textEdit.setText(data)
  41. if __name__ == '__main__':
  42. app = QApplication(sys.argv)
  43. ex = Example()
  44. sys.exit(app.exec_())

本例中有一个菜单栏,一个置中的文本编辑框,一个状态栏。点击菜单栏选项会弹出一个QtGui.QFileDialog对话框,在这个对话框里,你能选择文件,然后文件的内容就会显示在文本编辑框里。

  1. class Example(QMainWindow):
  2. def __init__(self):
  3. super().__init__()
  4. self.initUI()

这里设置了一个文本编辑框,文本编辑框是基于QMainWindow组件的。

  1. fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')

弹出QFileDialog窗口。getOpenFileName()方法的第一个参数是说明文字,第二个参数是默认打开的文件夹路径。默认情况下显示所有类型的文件。

  1. if fname[0]:
  2. f = open(fname[0], 'r')
  3. with f:
  4. data = f.read()
  5. self.textEdit.setText(data)

读取选中的文件,并显示在文本编辑框内(但是打开HTML文件时,是渲染后的结果,汗)。

程序展示:

file Dialog