QT4 控件
我们有各种控件,我们可以使用PyQt4访问。包含:
- 文本框
- 组合框
- 日历
对于更多控件,我们建议使用下一个教程中介绍的 GUI 创建工具。
文本框控件
几乎每个应用程序都有输入字段。在 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_())
下拉列表 Combobox
下拉列表可用于从列表中选择项目。
#! /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_())
日历控件
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_())
结果: