控件2
本章我們繼續介紹PyQt5控件。這次的有QPixmap,QLineEdit,QSplitter,和QComboBox。
圖片
QPixmap是處理圖片的組件。本例中,我們使用QPixmap在窗口裏顯示一張圖片。
import sys
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QLabel, QApplication)
from PyQt5.QtGui import QPixmap
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
pixmap = QPixmap('cuiniao.jpg')
lbl = QLabel(self)
lbl.setPixmap(pixmap)
hbox.addWidget(lbl)
self.setLayout(hbox)
self.move(300, 200)
self.setWindowTitle("翠鳥")
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
pixmap = QPixmap("cuiniao.jpg")
創建一個QPixmap對象,接收一個文件作爲參數。
lbl = QLabel(self) lbl.setPixmap(pixmap)
把QPixmap實例放到QLabel組件裏。
程序展示:
行編輯
QLineEdit組件提供了編輯文本的功能,自帶了撤銷、重做、剪切、粘貼、拖拽等功能。
import sys
from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.lbl = QLabel(self)
qle = 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('QLineEdit')
self.show()
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
例子中展示了一個編輯組件和一個標籤,我們在輸入框裏鍵入的文本,會立即在標籤裏顯示出來。
qle = QLineEdit(self)
創建一個QLineEdit對象。
qle.textChanged[str].connect(self.onChanged)
如果輸入框的值有變化,就調用onChanged()方法。
def onChanged(self, text): self.lbl.setText(text) self.lbl.adjustSize()
在onChanged()方法內部,我們把文本框裏的值賦值給了標籤組件,然後調用adjustSize()方法讓標籤自適應文本內容。
程序展示:
QSplitter
QSplitter組件能讓用戶通過拖拽分割線的方式改變子窗口大小的組件。本例中我們展示用兩個分割線隔開的三個QFrame組件。
import sys
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QFrame, QSplitter, QStyleFactory, QApplication
from PyQt5.QtCore import Qt
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
topleft = QFrame(self)
topleft.setFrameShape(QFrame.StyledPanel)
topright = QFrame(self)
topright.setFrameShape(QFrame.StyledPanel)
bottom = QFrame(self)
bottom.setFrameShape(QFrame.StyledPanel)
splitter1 = QSplitter(Qt.Horizontal)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)
splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(splitter1)
splitter2.addWidget(bottom)
hbox.addWidget(splitter2)
self.setLayout(hbox)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QSplitter')
self.show()
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
三個窗口和兩個分割線的佈局創建完成了,但是要注意,有些主題下,分割線的顯示效果不太好。
topleft = QFrame(self) topleft.setFrameShape(QFrame.StyledPanel)
爲了更清楚的看到分割線,我們使用了設置好的子窗口樣式。
splitter1 = QSplitter(Qt.Horizontal) splitter1.addWidget(topleft) splitter1.addWidget(topright)
創建一個QSplitter組件,並在裏面添加了兩個框架。
splitter2 = QSplitter(Qt.Vertical) splitter2.addWidget(splitter1)
也可以在分割線裏面再進行分割。
程序展示:
下拉選框
QComboBox組件能讓用戶在多個選擇項中選擇一個。
import sys
from PyQt5.QtWidgets import (QWidget, QLabel, QComboBox, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.lbl = QLabel('Window', self)
combo = QComboBox(self)
combo.addItem("Window")
combo.addItem("Ubuntu")
combo.addItem("Mandriva")
combo.addItem("Fedora")
combo.addItem("Arch")
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('QComboBox')
self.show()
def onActivated(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
本例包含了一個QComboBox和一個QLabel。下拉選擇框有五個選項,都是Linux的發行版名稱,標籤內容爲選定的發行版名稱。
combo = QComboBox(self) combo.addItem("Ubuntu") combo.addItem("Mandriva") combo.addItem("Fedora") combo.addItem("Arch") combo.addItem("Gentoo")
創建一個QComboBox組件和五個選項。
combo.activated[str].connect(self.onActivated)
在選中的條目上調用onActivated()方法。
def onActivated(self, text): self.lbl.setText(text) self.lbl.adjustSize()
在方法內部,設置標籤內容爲選定的字符串,然後設置自適應文本大小。
程序展示:
參考:
https://maicss.gitbooks.io/pyqt5/
http://zetcode.com/gui/pyqt5/
http://code.py40.com/pyqt5/