211.md 3.1 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4 5 6
# PyQT5 输入对话框

> 原文: [https://pythonspot.com/pyqt5-input-dialog/](https://pythonspot.com/pyqt5-input-dialog/)

[**PyQt5**](https://pythonspot.com/pyqt5/) 支持多个输入对话框,使用它们可以导入 QInputDialog。

W
wizardforcel 已提交
7
```py
W
wizardforcel 已提交
8 9 10 11 12 13 14 15
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit

```

[**PyQt5**](https://pythonspot.com/pyqt5/) 输入对话框概述:

![pyqt5-input-dialog](img/6872d4e6f12488af0c0f4f9c14deb773.jpg)

W
wizardforcel 已提交
16 17 18
## 获取整数

使用 QInputDialog.getInt()获取整数:
W
wizardforcel 已提交
19

W
wizardforcel 已提交
20
```py
W
wizardforcel 已提交
21 22 23 24 25 26 27 28 29
def getInteger(self):
    i, okPressed = QInputDialog.getInt(self, "Get integer","Percentage:", 28, 0, 100, 1)
    if okPressed:
        print(i)

```

参数顺序如下:自身,窗口标题,标签(在输入框之前),默认值,最小,最大和步长。

W
wizardforcel 已提交
30 31 32
## 获得双倍

通过 QInputDialog.getDouble()获得双倍:
W
wizardforcel 已提交
33

W
wizardforcel 已提交
34
```py
W
wizardforcel 已提交
35 36 37 38 39 40 41 42 43
def getDouble(self):
    d, okPressed = QInputDialog.getDouble(self, "Get double","Value:", 10.05, 0, 100, 10)
    if okPressed:
        print(d)

```

最后一个参数(10)是逗号后面的小数位数。

W
wizardforcel 已提交
44 45 46
## 获取项目/选择

从下拉框中获取一个项目:
W
wizardforcel 已提交
47

W
wizardforcel 已提交
48
```py
W
wizardforcel 已提交
49 50 51 52 53 54 55 56
def getChoice(self):
    items = ("Red","Blue","Green")
    item, okPressed = QInputDialog.getItem(self, "Get item","Color:", items, 0, False)
    if okPressed and item:
        print(item)

```

W
wizardforcel 已提交
57 58 59
## 获取字符串

使用 QInputDialog.getText()获取字符串
W
wizardforcel 已提交
60

W
wizardforcel 已提交
61
```py
W
wizardforcel 已提交
62 63 64 65 66 67 68
def getText(self):
    text, okPressed = QInputDialog.getText(self, "Get text","Your name:", QLineEdit.Normal, "")
    if okPressed and text != '':
        print(text)

```

W
wizardforcel 已提交
69 70 71
## 所有 PyQt5 输入对话框的示例

下面的完整示例:
W
wizardforcel 已提交
72

W
wizardforcel 已提交
73
```py
W
wizardforcel 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit
from PyQt5.QtGui import QIcon

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 input dialogs - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 480
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.getInteger()
        self.getText()
        self.getDouble()
        self.getChoice()

        self.show()

    def getInteger(self):
        i, okPressed = QInputDialog.getInt(self, "Get integer","Percentage:", 28, 0, 100, 1)
        if okPressed:
            print(i)

    def getDouble(self):
        d, okPressed = QInputDialog.getDouble(self, "Get double","Value:", 10.50, 0, 100, 10)
        if okPressed:
            print( d)

    def getChoice(self):
        items = ("Red","Blue","Green")
        item, okPressed = QInputDialog.getItem(self, "Get item","Color:", items, 0, False)
        if ok and item:
            print(item)

    def getText(self):
        text, okPressed = QInputDialog.getText(self, "Get text","Your name:", QLineEdit.Normal, "")
        if okPressed and text != '':
            print(text)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

```

[下载 PyQT5 示例](https://pythonspot.com/download-pyqt5-examples/)