QMainWindow、QWidget、QDialog用于創(chuàng)建窗口,可以直接使用,也可以派生使用。
QMainWindow窗口包含菜單欄、工具欄、狀態(tài)欄、標題欄等,是最常見的窗口形式。
QDialog是對話框窗口的基類,主要用于執(zhí)行短期任務(wù),或與用戶進行交互,可以是模態(tài)或非模態(tài)的。QDialog對話框沒有菜單欄、工具欄、狀態(tài)欄等。
QWidget是Qt圖形組件的基類,可以作為頂層窗口,也可以嵌入到其它組件中。
QMainWindow是頂層窗口,QMainWindow有自己的布局管理器,不能使用setLayout對其進行設(shè)置,布局如下:
import sysfrom PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QLabel, QDesktopWidget, QToolBarclass MainWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) # 菜單欄設(shè)置 self.menuBar = self.menuBar() self.menuBar.addAction("File") self.menuBar.addAction("View") # 工具欄設(shè)置 open = QToolBar() open.addAction("OPen") self.addToolBar(open) close = QToolBar() close.addAction("Close") self.addToolBar(close) # 中央組件設(shè)置 self.window = QWidget() self.setCentralWidget(self.window) # 狀態(tài)欄設(shè)置 self.statusBar = self.statusBar() self.statusBar.showMessage("This is an status message.", 5000) label = QLabel("permanent status") self.statusBar.addPermanentWidget(label) self.resize(800, 600) self.setWindowTitle("MainWindow Demo") self.center() def center(self): screen = QDesktopWidget().screenGeometry() size = self.geometry() self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())
QWidget是所有GUI界面組件的基類,所有窗口和控件都直接或間接繼承自QWidget基類。
Qt使用統(tǒng)一的坐標系統(tǒng)來定位窗口控件的位置和大小,坐標系統(tǒng)如下:
import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QLabel, QDesktopWidget, QToolBarclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle("MainWidget") self.resize(800, 600) self.center() def center(self): screen = QDesktopWidget().screenGeometry() size = self.geometry() self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2) def dispalyGeometry(self): x = self.x() y = self.y() print("x: {0}, y: {1}".format(x, y)) x = self.pos().x() y = self.pos().y() print("x: {0}, y: {1}".format(x, y)) x = self.frameGeometry().x() y = self.frameGeometry().y() print("x: {0}, y: {1}".format(x, y)) x = self.geometry().x() y = self.geometry().y() print("x: {0}, y: {1}".format(x, y)) print("geometry: ", self.geometry()) print("frameGemetry: ", self.frameGeometry())if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.show() window.dispalyGeometry() sys.exit(app.exec_())
QLabel作為一個占位符可以顯示不可編輯的文本、圖片、GIF動畫,QLabel是界面的標簽類,繼承自QFrame。
import sysfrom PyQt5.QtWidgets import QApplication, QLabelfrom PyQt5.QtCore import Qtfrom PyQt5.QtGui import QPaletteif __name__ == "__main__": app = QApplication(sys.argv) window = QLabel() window.setWindowTitle("QLabel Demo") window.setText("www.baidu.com") window.setOpenExternalLinks(True) pallette = QPalette() pallette.setColor(QPalette.Window, Qt.blue) window.setPalette(pallette) window.setAlignment(Qt.AlignCenter) window.setAutoFillBackground(True) window.resize(400, 200) window.show() sys.exit(app.exec_())
QLineEdit是單行文本框控件,可以編輯單行字符串,用于接收用戶輸入。
import sysfrom PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QLabelfrom PyQt5.QtCore import Qtfrom PyQt5.QtGui import QIntValidatorclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) label = QLabel("input: ", self) label.move(0, 0) lineEdit = QLineEdit(self) # 設(shè)置驗證器 intValidator = QIntValidator() lineEdit.setValidator(intValidator) lineEdit.setMaxLength(10) lineEdit.move(50, 0) lineEdit.textChanged.connect(self.displayText) def displayText(self, text): print(text)if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
QTextEdit是一個多行文本框編輯控件,可以顯示、編輯多行文本編輯內(nèi)容,當文本內(nèi)容超出控件顯示范圍時,可以顯示水平和垂直滾動條,QTextEdit不僅可以顯示文本,還可以顯示HTML文檔。
import sysfrom PyQt5.QtWidgets import QApplication, QTextEdit, QWidget, QLabelfrom PyQt5.QtCore import Qtfrom PyQt5.QtGui import QIntValidatorclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) label = QLabel("input: ", self) label.move(0, 0) self.textEdit = QTextEdit(self) self.textEdit.move(50, 0) self.textEdit.setPlainText("Hello, PyQt5") self.textEdit.textChanged.connect(self.displayText) def displayText(self): print(self.textEdit.toPlainText())if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
QPushButton繼承自QAbstractButton,形狀為長方形,文本、圖標顯示在長方形區(qū)域。
import sysfrom PyQt5.QtWidgets import QApplication, QPushButton, QWidgetclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button1 = QPushButton("OK", self) button1.clicked.connect(lambda: self.onClicked(button1)) def onClicked(self, button): print("Button {0} is clicked.".format(button.text()))if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
QRadioButton繼承自QAbstractButton,提供了一組可選的按鈕和標簽,用戶可以選擇其中一個選項,標簽用于顯示對應(yīng)的文本信息。QRadioButton是一種開關(guān)按鈕,可以切換為on或off,即checked或unchecked。在單選按鈕組里,一次只能選擇一個單選按鈕,如果需要多個獨占的按鈕組合,需要將其放到QGroupBox或QButtonGroup中。
import sysfrom PyQt5.QtWidgets import QApplication, QRadioButton, QWidgetclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button1 = QRadioButton("OK", self) button1.toggled.connect(lambda: self.onClicked(button1)) def onClicked(self, button): print("Button {0} is clicked.status is {1}".format(button.text(), button.isChecked()))if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
QCheckBox繼承自QAbstractButton,提供一組帶文本標簽的復選框,用戶可以選擇多個選項,復選框可以顯示文本和圖標。
除了選中、未選中,QCheckBox有第三種狀態(tài):半選中,表示沒有變化。
import sysfrom PyQt5.QtWidgets import QApplication, QCheckBox, QWidgetclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button1 = QCheckBox("OK", self) button1.stateChanged.connect(lambda: self.onStateChanged(button1)) def onStateChanged(self, button): print("Button {0} is clicked.status is {1}".format(button.text(), button.isChecked()))if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
QComboBox是下拉列表框。
import sysfrom PyQt5.QtWidgets import QApplication, QComboBox, QWidgetclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.combo = QComboBox(self) self.combo.addItem("Apple") self.combo.addItem("HuaWei") self.combo.addItem("XiaoMi") self.combo.addItem("Oppo") self.combo.currentIndexChanged.connect(self.onCurrentIndex) def onCurrentIndex(self, index): print("current item is {0}".format(self.combo.currentText()))if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
QSpinBox是一個計數(shù)器控件,允許用戶選擇一個整數(shù)值,通過單擊向上、向下按鈕或鍵盤的上下箭頭來增加減少當前顯示的值,用戶也可以從編輯框輸入當前值。默認情況下,QSpinBox的取值范圍為0——99,每次改變的步長值為1。
import sysfrom PyQt5.QtWidgets import QApplication, QSpinBox, QWidgetclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) spinBox = QSpinBox(self) spinBox.valueChanged.connect(self.onValueChanged) def onValueChanged(self, value): print("current value is {0}".format(value))if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
QSlider控件提供了一個垂直或水平的滑動條,是一個用于控制有界值的控件,允許用戶沿著水平或垂直方向在某一范圍內(nèi)移動滑塊,并將滑塊所在的位置轉(zhuǎn)換成一個合法范圍內(nèi)的整數(shù)值。
import sysfrom PyQt5.QtWidgets import QApplication, QSlider, QWidgetfrom PyQt5.QtCore import Qtclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) slider = QSlider(Qt.Horizontal, self) slider.setMaximum(20) slider.setMinimum(10) slider.valueChanged.connect(self.onValueChanged) def onValueChanged(self, value): print("current value is {0}".format(value))if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
QDialog是對話框類,提供了三種窗口模態(tài),非模態(tài),模態(tài)和應(yīng)用程序模態(tài),使用setWindowModality方法設(shè)置窗口模態(tài)。
(1)非模態(tài)
非模態(tài)可以和應(yīng)用程序的其它窗×××互,使用Qt.NonModal進行設(shè)置。
(2)窗口模態(tài)
窗口模態(tài)在未處理完成當前對話框時,將阻止和對話框的父窗口進行交互,使用Qt.Modal進行設(shè)置。
(3)應(yīng)用程序模態(tài)
應(yīng)用程序模態(tài)阻止任何和其它窗口進行交互,使用Qt.ApplicationModal。
QDialog及其派生類對話框在ESC按鍵按下時,對話框窗口將會默認調(diào)用QDialog.reject方法,關(guān)閉對話框。
setWindowModality()方法可以設(shè)置窗口是否是模態(tài)窗口,Qt::WindowModality默認值為Qt::NonModal,如果沒有設(shè)置Qt::WindowModality屬性值,每次用show()方法顯示出的窗口都是非模態(tài)窗口。
使用exec()方法顯示的對話框為模態(tài)對話框,同時會阻塞窗口的響應(yīng)直到用戶關(guān)閉對話框,并且返回DialogCode(包括Accepted和Rejected兩個值)結(jié)果。
如果沒有設(shè)置Qt::WindowModality屬性值,使用exec()方法顯示出的對話框默認為應(yīng)用程序級模態(tài)對話框。所有使用exec()方法顯示的對話框在窗口關(guān)閉前會阻塞整個程序所有窗口的響應(yīng)。調(diào)用exec()方法后,對話框會阻塞,直到對話框關(guān)閉才會繼續(xù)執(zhí)行。在關(guān)閉對話框后exec()方法會返回Accepted或者Rejected,一般程序根據(jù)返回不同的結(jié)果進行相應(yīng)的操作。
模式對話框有自己的事件循環(huán),exec() 方法內(nèi)部先設(shè)置modal屬性為Qt::ApplicationModal,然后調(diào)用 show() 顯示對話框,最后啟用事件循環(huán)來阻止exec() 方法的結(jié)束。直到窗口關(guān)閉,得到返回結(jié)果(DialogCode),退出事件循環(huán),最后exec()方法調(diào)用結(jié)束,exec()方法后的代碼將繼續(xù)執(zhí)行。
import sysfrom PyQt5.QtWidgets import QApplication, QDialog, QWidget, QPushButtonfrom PyQt5.QtCore import Qtclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button = QPushButton("OK", self) self.resize(800, 600) button.clicked.connect(self.onOKClicked) def onOKClicked(self): dialog = QDialog() dialog.setWindowTitle("Dialog Demo") dialog.resize(300, 200) dialog.exec_()if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
QMessageBox是一種通用的彈出式對話框,用于顯示消息,允許用戶通過點擊不同的標準按鈕對消息進行反饋,QMessageBox提供了五種常用消息對話框的顯示方法。warning(self, QWidget, p_str, p_str_1, buttons, QMessageBox_StandardButtons=None, QMessageBox_StandardButton=None, *args, **kwargs)
創(chuàng)建警告消息對話框critical(self, QWidget, p_str, p_str_1, buttons, QMessageBox_StandardButtons=None, QMessageBox_StandardButton=None, *args, **kwargs)
創(chuàng)建關(guān)鍵錯誤消息對話框information(self, QWidget, p_str, p_str_1, buttons, QMessageBox_StandardButtons=None, QMessageBox_StandardButton=None, *args, **kwargs)
創(chuàng)建信息消息對話框question(self, QWidget, p_str, p_str_1, buttons, QMessageBox_StandardButtons=None, QMessageBox_StandardButton=None, *args, **kwargs)
創(chuàng)建詢問消息對話框about(self, QWidget, p_str, p_str_1)
創(chuàng)建關(guān)于信息對話框
import sysfrom PyQt5.QtWidgets import QApplication, QMessageBox, QWidget, QPushButtonclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button = QPushButton("OK", self) self.resize(800, 600) button.clicked.connect(self.onOKClicked) def onOKClicked(self): button = QMessageBox.question(self, "MessageBox Title", "是否確定關(guān)閉?", QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Ok) if button == QMessageBox.Ok: print("select Ok Button")if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
QInputDialog是一個標準對話框控件,由一個文本框和兩個按鈕(OK和Cancel)組成,用戶單擊OK按鈕或按下Enter鍵后,在父窗口可以接收通過QInputDialog輸入的信息。
QInputDialog.getInt從控件中獲取標準整型輸入
QInputDialog.getItem從控件中獲取列表的選項輸入
QInputDialog.getText從控件中獲取標準字符串輸入
QInputDialog.getDouble從控件中獲取標準浮點數(shù)輸入
import sysfrom PyQt5.QtWidgets import QApplication, QInputDialog, QWidget, QPushButtonclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button = QPushButton("OK", self) self.resize(800, 600) button.clicked.connect(self.onOKClicked) def onOKClicked(self): items = ["C++", "Python", "Java", "Go"] item, ok = QInputDialog.getItem(self, "Select an Item", "Programing Language", items, 0, False) if ok and item: print("selected item: ", item) text, ok = QInputDialog.getText(self, "Input an text", "text:") if ok: print("input text: ", text)if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
QFontDialog是字體選擇對話框,可以讓用戶選擇所顯示的文本的字號大小、樣式和格式。QFontDialog.getFont可以從字體選擇對話框中獲取文本的顯示字號、樣式和格式。
import sysfrom PyQt5.QtWidgets import QApplication, QFontDialog, QWidget, QPushButtonfrom PyQt5.QtGui import QFontclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button = QPushButton("OK", self) self.resize(800, 600) button.clicked.connect(self.onOKClicked) def onOKClicked(self): font, ok = QFontDialog.getFont() if ok: print(font.family(), font.pointSize())if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())
QFileDialog是用于打開和保存文件的標準對話框,QFileDialog在打開文件時使用文件過濾器,用于顯示指定擴展名的文件。
import sysfrom PyQt5.QtWidgets import QApplication, QFileDialog, QWidget, QPushButtonclass MainWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) button = QPushButton("OK", self) self.resize(800, 600) button.clicked.connect(self.onOKClicked) def onOKClicked(self): fname, _ = QFileDialog.getOpenFileName(self, "Open file", '/', "Images(*.jpg *.gif)") print(fname)if __name__ == "__main__": app = QApplication(sys.argv) window = MainWidget() window.resize(400, 200) window.show() sys.exit(app.exec_())