本章咱們繼續介紹PyQt5控件。此次的有QPixmap
,QLineEdit
,QSplitter
,和QComboBox
。python
QPixmap
是處理圖片的組件。本例中,咱們使用QPixmap
在窗口裏顯示一張圖片。app
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial 歡迎加QQ羣`923 414 804`與我一塊兒學習 In this example, we dispay an image on the window. Author: Jan Bodnar Website: zetcode.com Last edited: August 2017 """ from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QLabel, QApplication) from PyQt5.QtGui import QPixmap import sys class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): hbox = QHBoxLayout(self) pixmap = QPixmap("redrock.png") lbl = QLabel(self) lbl.setPixmap(pixmap) hbox.addWidget(lbl) self.setLayout(hbox) self.move(300, 200) self.setWindowTitle('Red Rock') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
pixmap = QPixmap("redrock.png")
建立一個QPixmap
對象,接收一個文件做爲參數。框架
lbl = QLabel(self) lbl.setPixmap(pixmap)
把QPixmap
實例放到QLabel
組件裏。佈局
程序展現:學習
QLineEdit
組件提供了編輯文本的功能,自帶了撤銷、重作、剪切、粘貼、拖拽等功能。ui
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This example shows text which is entered in a QLineEdit in a QLabel widget. Author: Jan Bodnar Website: zetcode.com Last edited: August 2017 """ 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_())
例子中展現了一個編輯組件和一個標籤,咱們在輸入框裏鍵入的文本,會當即在標籤裏顯示出來。this
qle = QLineEdit(self)
建立一個QLineEdit
對象。spa
qle.textChanged[str].connect(self.onChanged)
若是輸入框的值有變化,就調用onChanged()
方法。code
def onChanged(self, text): self.lbl.setText(text) self.lbl.adjustSize()
在onChanged()
方法內部,咱們把文本框裏的值賦值給了標籤組件,而後調用adjustSize()
方法讓標籤自適應文本內容。對象
程序展現:
QSplitter
組件能讓用戶經過拖拽分割線的方式改變子窗口大小的組件。本例中咱們展現用兩個分割線隔開的三個QFrame
組件。
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This example shows how to use QSplitter widget. Author: Jan Bodnar Website: zetcode.com Last edited: August 2017 """ from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame, QSplitter, QStyleFactory, QApplication) from PyQt5.QtCore import Qt import sys 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
組件能讓用戶在多個選擇項中選擇一個。
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial This example shows how to use a QComboBox widget. Author: Jan Bodnar Website: zetcode.com Last edited: August 2017 """ from PyQt5.QtWidgets import (QWidget, QLabel, QComboBox, QApplication) import sys class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.lbl = QLabel("Ubuntu", self) combo = QComboBox(self) 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()
在方法內部,設置標籤內容爲選定的字符串,而後設置自適應文本大小。
程序展現: