Skip to content

Files

Latest commit

f9ba77b · Nov 22, 2019

History

History
136 lines (91 loc) · 2.5 KB

218.md

File metadata and controls

136 lines (91 loc) · 2.5 KB

QT4 小部件

原文: https://pythonspot.com/qt4-widgets/

我们有许多可通过 PyQT 访问的小部件。 其中包括:

  • 文本框
  • 组合框
  • 日历

对于更多小部件,我们建议使用下一个教程中介绍的 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_())

qt textbox

qt 文本框

组合框

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

#! /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

qt 组合框

日历小部件

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

qt 日历

下载 PyQT 代码(批量收集)