|
PyQt4 Widget II
這里我們將繼續介紹 PyQt4 組件。我們將涉及 QtGui.QPixmap , QtGui.QLineEdit , QtGui.QSplitter 和 QtGui.QComboBox 。
QtGui.QPixmap
QtGui.QPixmap 是可以處理圖片的組件之一。它對顯示圖片進行了優化。在我們的例子中,我們會用 QtGui.QPixmap 來顯示圖片。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we dispay an image
on the window.
author: Jan Bodnar
website: zetcode.com
last edited: September 2011
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
pixmap = QtGui.QPixmap("redrock.png")
lbl = QtGui.QLabel(self)
lbl.setPixmap(pixmap)
hbox.addWidget(lbl)
self.setLayout(hbox)
self.move(300, 200)
self.setWindowTitle('Red Rock')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在這個例子里,我們顯示了一幅圖片。
pixmap = QtGui.QPixmap("redrock.png")
我們創建了一個 QtGui.QPixmap 對象。它接受文件名作為參數。
lbl = QtGui.QLabel(self)
lbl.setPixmap(pixmap)
我們把 pixmap 放到了 QtGui.QLabel 中。
QtGui.QLineEdit
QtGui.QLineEdit 是一個允許輸入和編輯一行純文本。這個組件中可以撤銷/恢復,剪切/粘貼以及拖拉。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
This example shows text which
is entered in a QtGui.QLineEdit
in a QtGui.QLabel widget.
author: Jan Bodnar
website: zetcode.com
last edited: August 2011
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.lbl = QtGui.QLabel(self)
qle = QtGui.QLineEdit(self)
qle.move(60, 100)
self.lbl.move(60, 40)
qle.textChanged[str].connect(self.onChanged)
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('QtGui.QLineEdit')
self.show()
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
這個例子顯示一個行編輯區和一個標簽。我們輸入的就會立即在標簽中顯示。
qle = QtGui.QLineEdit(self)
創建了 QtGui.QLineEdit 組件。
qle.textChanged[str].connect(self.onChanged)
如果文本區的文本改變了,我們就調用 onChanged() 方法。
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
在 onChanged() 方法中,我們把輸入的文本放到了標簽中。通過調用 adjustSize() 方法,我們把標簽設置到文本的長度。
QtGui.QSplitter
QtGui.QSplitter 可以讓用戶控制子組件的大小,通過拖動子組件的大小。在我們的例子中,我們的三個 QtGui.QFrame 將由兩個 splitter 分割。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
This example shows
how to use QtGui.QSplitter widget.
author: Jan Bodnar
website: zetcode.com
last edited: September 2011
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
topleft = QtGui.QFrame(self)
topleft.setFrameShape(QtGui.QFrame.StyledPanel)
topright = QtGui.QFrame(self)
topright.setFrameShape(QtGui.QFrame.StyledPanel)
bottom = QtGui.QFrame(self)
bottom.setFrameShape(QtGui.QFrame.StyledPanel)
splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)
splitter2 = QtGui.QSplitter(QtCore.Qt.Vertical)
splitter2.addWidget(splitter1)
splitter2.addWidget(bottom)
hbox.addWidget(splitter2)
self.setLayout(hbox)
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QtGui.QSplitter')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在這個例子中,有三個框架組件,兩個分割條。
topleft = QtGui.QFrame(self)
topleft.setFrameShape(QtGui.QFrame.StyledPanel)
我們使用了有樣式的框架,主要用于看到邊框。
splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)
我們創建了一個 QtGui.QSplitter 組件,并添加到兩個框架。
splitter2 = QtGui.QSplitter(QtCore.Qt.Vertical)
splitter2.addWidget(splitter1)
我們也可以把一個 splitter 添加到 splitter 中。
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
我們使用一個簡潔的樣式。在有些樣式中,框架是不可見的。
QtGui.QComboBox
QtGui.QComboBox 允許用戶從一組選項中選取一個。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
This example shows
how to use QtGui.QComboBox widget.
author: Jan Bodnar
website: zetcode.com
last edited: September 2011
"""
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.lbl = QtGui.QLabel("Ubuntu", self)
combo = QtGui.QComboBox(self)
combo.addItem("Ubuntu")
combo.addItem("Mandriva")
combo.addItem("Fedora")
combo.addItem("Red Hat")
combo.addItem("Gentoo")
combo.move(50, 50)
self.lbl.move(50, 150)
combo.activated[str].connect(self.onActivated)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QtGui.QComboBox')
self.show()
def onActivated(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
這個例子中有一個 QtGui.QComboBox 和 QtGui.QLable 。這里有五個選項。它們是 Linux 的發行版。標簽中就會顯示選中的項目。
combo = QtGui.QComboBox(self)
combo.addItem("Ubuntu")
combo.addItem("Mandriva")
combo.addItem("Fedora")
combo.addItem("Red Hat")
combo.addItem("Gentoo")
我們創建一個 QtGui.QComboBox 組件并增加了五個選項。
combo.activated[str].connect(self.onActivated)
當選擇一個選項后,我們調用了 onActivated() 方法。
def onActivated(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
在這個方法中,我們把選中的選項的文本放到標簽中。我們還調整標簽的大小。
在本部分,我們涉及了另外四個 PyQt4 組件。
|