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_())
結果: