237.md 1.4 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4 5 6 7 8
# wxPython 输入对话框

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

输入对话框可让您的用户给您反馈或输入。 它们偶尔会出现在桌面应用程序中。

wxPython 支持输入对话框,它们包含在框架中。 典型的 wxPython 对话框如下所示:

W
wizardforcel 已提交
9 10 11
![wx input](img/38adcfbb184729e5d1d74b09ba622436.jpg)

input dialog made with wxPython
W
wizardforcel 已提交
12 13 14 15 16

## wxPython 输入对话框

The example code below creates an input dialog with wxPython:

W
wizardforcel 已提交
17
```py
W
wizardforcel 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#!/usr/bin/python

import wx

def onButton(event):
    print "Button pressed."

app = wx.App()

frame = wx.Frame(None, -1, 'win.py')
frame.SetDimensions(0,0,200,50)

# Create text input
dlg = wx.TextEntryDialog(frame, 'Enter some text','Text Entry')
dlg.SetValue("Default")
if dlg.ShowModal() == wx.ID_OK:
    print('You entered: %s\n' % dlg.GetValue())
dlg.Destroy()

```

可以使用以下函数将 [wxPython](https://pythonspot.com/wx/) 文本框添加到窗口:

W
wizardforcel 已提交
41
```py
W
wizardforcel 已提交
42 43 44 45 46 47 48 49
wx.TextEntryDialog(frame, 'Enter some text','Text Entry')

```

其中第一个参数是框架,第二个参数是标签,最后一个参数是窗口标题。

下面的函数显示对话框,并等待用户按下按钮之一:

W
wizardforcel 已提交
50
```py
W
wizardforcel 已提交
51 52 53 54 55 56
dlg.ShowModal()

```

您可以通过选择以下按钮之一来按下按钮:

W
wizardforcel 已提交
57
```py
W
wizardforcel 已提交
58 59 60 61 62 63 64 65
wx.OK
wx.CANCEL

```

(结果是其中之一)

给出输入后,可以使用 dlg.GetValue()函数获取输入文本。