218.md 2.6 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 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
# QT4 小部件

> 原文: [https://pythonspot.com/qt4-widgets/](https://pythonspot.com/qt4-widgets/)

我们有许多可通过 [PyQT](https://pythonspot.com/pyqt4/) 访问的小部件。 其中包括:

*   文本框
*   组合框
*   日历

For more widgets we suggest using the GUI creation tool covered in the next tutorial.

**相关课程:** [使用 PyQt5 创建 GUI 应用](https://gum.co/pysqtsamples)

**文本框小部件** 几乎每个应用程序中都存在输入字段。 在 [PyQT4](https://pythonspot.com/pyqt4/) 中,可以使用 QLineEdit()函数创建输入字段。

```
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
import sys
from PyQt4.QtGui import *

# Create an PyQT4 application object.
a = QApplication(sys.argv)

# The QWidget widget is the base class of all user interface objects in PyQt4.
w = QMainWindow()

# Set window size.
w.resize(320, 100)

# Set window title
w.setWindowTitle("PyQT Python Widget!")

# Create textbox
textbox = QLineEdit(w)
textbox.move(20, 20)
textbox.resize(280,40)

# Show window
w.show()

sys.exit(a.exec_())

```

![qt textbox](img/8e1ee291a6d4a348a3629298a80fefbd.jpg) qt textbox

**组合框** 组合框可用于从列表中选择一个项目。

```
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
import sys
from PyQt4.QtGui import *

# Create an PyQT4 application object.
a = QApplication(sys.argv)

# The QWidget widget is the base class of all user interface objects in PyQt4.
w = QMainWindow()

# Set window size.
w.resize(320, 100)

# Set window title
w.setWindowTitle("PyQT Python Widget!")

# Create combobox
combo = QComboBox(w)
combo.addItem("Python")
combo.addItem("Perl")
combo.addItem("Java")
combo.addItem("C++")
combo.move(20,20)

# Show window
w.show()

sys.exit(a.exec_())

```

![qt combobox](img/cb9724ae989ef2c079731874772a1798.jpg) qt combobox

**日历小部件** PyQT4 库有一个日历小部件,您可以使用 QCalendarWidget()调用来创建它。

```
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
import sys
from PyQt4.QtGui import *

# Create an PyQT4 application object.
a = QApplication(sys.argv)

# The QWidget widget is the base class of all user interface objects in PyQt4.
w = QMainWindow()

# Set window size.
w.resize(320, 240)

# Set window title
w.setWindowTitle("PyQT Python Widget!")

# Create calendar
cal = QCalendarWidget(w)
cal.setGridVisible(True)
cal.move(0, 0)
cal.resize(320,240)

# Show window
w.show()

sys.exit(a.exec_())

```

结果:

![calendar qt](img/ab786df4859e36d6feeabac1f97dc5f2.jpg) calendar qt

[下载 PyQT 代码(批量收集)](https://pythonspot.com/python-qt-examples/)