|
PyQt4 Widget II
這里我們將繼續(xù)介紹 PyQt4 組件。我們將涉及 QtGui.QPixmap , QtGui.QLineEdit , QtGui.QSplitter 和 QtGui.QComboBox 。
QtGui.QPixmap
QtGui.QPixmap 是可以處理圖片的組件之一。它對(duì)顯示圖片進(jìn)行了優(yōu)化。在我們的例子中,我們會(huì)用 QtGui.QPixmap 來(lái)顯示圖片。
#!/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()
在這個(gè)例子里,我們顯示了一幅圖片。
pixmap = QtGui.QPixmap("redrock.png")
我們創(chuàng)建了一個(gè) QtGui.QPixmap 對(duì)象。它接受文件名作為參數(shù)。
lbl = QtGui.QLabel(self)
lbl.setPixmap(pixmap)
我們把 pixmap 放到了 QtGui.QLabel 中。
QtGui.QLineEdit
QtGui.QLineEdit 是一個(gè)允許輸入和編輯一行純文本。這個(gè)組件中可以撤銷/恢復(fù),剪切/粘貼以及拖拉。
#!/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()
這個(gè)例子顯示一個(gè)行編輯區(qū)和一個(gè)標(biāo)簽。我們輸入的就會(huì)立即在標(biāo)簽中顯示。
qle = QtGui.QLineEdit(self)
創(chuàng)建了 QtGui.QLineEdit 組件。
qle.textChanged[str].connect(self.onChanged)
如果文本區(qū)的文本改變了,我們就調(diào)用 onChanged() 方法。
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
在 onChanged() 方法中,我們把輸入的文本放到了標(biāo)簽中。通過調(diào)用 adjustSize() 方法,我們把標(biāo)簽設(shè)置到文本的長(zhǎng)度。
QtGui.QSplitter
QtGui.QSplitter 可以讓用戶控制子組件的大小,通過拖動(dòng)子組件的大小。在我們的例子中,我們的三個(gè) QtGui.QFrame 將由兩個(gè) 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()
在這個(gè)例子中,有三個(gè)框架組件,兩個(gè)分割條。
topleft = QtGui.QFrame(self)
topleft.setFrameShape(QtGui.QFrame.StyledPanel)
我們使用了有樣式的框架,主要用于看到邊框。
splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)
我們創(chuàng)建了一個(gè) QtGui.QSplitter 組件,并添加到兩個(gè)框架。
splitter2 = QtGui.QSplitter(QtCore.Qt.Vertical)
splitter2.addWidget(splitter1)
我們也可以把一個(gè) splitter 添加到 splitter 中。
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
我們使用一個(gè)簡(jiǎn)潔的樣式。在有些樣式中,框架是不可見的。
QtGui.QComboBox
QtGui.QComboBox 允許用戶從一組選項(xiàng)中選取一個(gè)。
#!/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()
這個(gè)例子中有一個(gè) QtGui.QComboBox 和 QtGui.QLable 。這里有五個(gè)選項(xiàng)。它們是 Linux 的發(fā)行版。標(biāo)簽中就會(huì)顯示選中的項(xiàng)目。
combo = QtGui.QComboBox(self)
combo.addItem("Ubuntu")
combo.addItem("Mandriva")
combo.addItem("Fedora")
combo.addItem("Red Hat")
combo.addItem("Gentoo")
我們創(chuàng)建一個(gè) QtGui.QComboBox 組件并增加了五個(gè)選項(xiàng)。
combo.activated[str].connect(self.onActivated)
當(dāng)選擇一個(gè)選項(xiàng)后,我們調(diào)用了 onActivated() 方法。
def onActivated(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
在這個(gè)方法中,我們把選中的選項(xiàng)的文本放到標(biāo)簽中。我們還調(diào)整標(biāo)簽的大小。
在本部分,我們涉及了另外四個(gè) PyQt4 組件。
|