溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PyQt5快速入門(三)PyQt5基本窗口組件

發(fā)布時間:2020-07-19 20:05:04 來源:網(wǎng)絡(luò) 閱讀:7378 作者:天山老妖S 欄目:編程語言

PyQt5快速入門(三)PyQt5基本窗口組件

一、QMainWindow

1、窗口類型簡介

QMainWindow、QWidget、QDialog用于創(chuàng)建窗口,可以直接使用,也可以派生使用。
QMainWindow窗口包含菜單欄、工具欄、狀態(tài)欄、標(biāo)題欄等,是最常見的窗口形式。
QDialog是對話框窗口的基類,主要用于執(zhí)行短期任務(wù),或與用戶進(jìn)行交互,可以是模態(tài)或非模態(tài)的。QDialog對話框沒有菜單欄、工具欄、狀態(tài)欄等。
QWidget是Qt圖形組件的基類,可以作為頂層窗口,也可以嵌入到其它組件中。

2、QMainWindow

QMainWindow是頂層窗口,QMainWindow有自己的布局管理器,不能使用setLayout對其進(jìn)行設(shè)置,布局如下:
PyQt5快速入門(三)PyQt5基本窗口組件
Menu Bar是菜單欄,Toolbars是工具欄,Dock Widgets是??看翱冢珻entral Widget是中央窗口,Status Bar是狀態(tài)欄。

3、QMainWindow實例

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QLabel, QDesktopWidget, QToolBar

class 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

1、QWidget簡介

QWidget是所有GUI界面組件的基類,所有窗口和控件都直接或間接繼承自QWidget基類。

2、窗口坐標(biāo)系統(tǒng)

Qt使用統(tǒng)一的坐標(biāo)系統(tǒng)來定位窗口控件的位置和大小,坐標(biāo)系統(tǒng)如下:
PyQt5快速入門(三)PyQt5基本窗口組件
以屏幕的左上角為原點,即(0,0),從左向右為X軸正向,從上向下為Y軸正向,屏幕的坐標(biāo)系統(tǒng)用于定位頂層窗口。窗口內(nèi)部有自己的坐標(biāo)系統(tǒng),窗口坐標(biāo)系統(tǒng)以左上角為原點,即(0,0),從左向右為X軸正向,從上向下為Y軸正向,原點、X軸、Y軸圍成的區(qū)域為客戶區(qū),在客戶區(qū)的周圍是標(biāo)題欄、邊框。
?intx() const;
?inty() const;
?int width() const;
?int height() const;
?x()、y()獲取窗口左上角的坐標(biāo),但width()和height()獲取的是客戶區(qū)的寬和高。
geometry().x()、geometry().y()獲取客戶區(qū)的左上角坐標(biāo),geometry().width()、geometry().height()獲取客戶區(qū)的寬度和高度。
frameGeometry().x()、frameGeometry().y()獲取客戶區(qū)的左上角坐標(biāo),frameGeometry().width()、frameGeometry().height()獲取包含客戶區(qū)、標(biāo)題欄、邊框在內(nèi)的寬度和高度。

3、QWidget實例

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QDesktopWidget, QToolBar

class 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

1、QLabel簡介

QLabel作為一個占位符可以顯示不可編輯的文本、圖片、GIF動畫,QLabel是界面的標(biāo)簽類,繼承自QFrame。

2、QLabel實例

import sys
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPalette

if __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_())

四、文本框控件

1、QLineEdit

QLineEdit是單行文本框控件,可以編輯單行字符串,用于接收用戶輸入。

import sys
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIntValidator

class 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_())

2、QTextEdit

QTextEdit是一個多行文本框編輯控件,可以顯示、編輯多行文本編輯內(nèi)容,當(dāng)文本內(nèi)容超出控件顯示范圍時,可以顯示水平和垂直滾動條,QTextEdit不僅可以顯示文本,還可以顯示HTML文檔。

import sys
from PyQt5.QtWidgets import QApplication, QTextEdit, QWidget, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIntValidator

class 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_())

五、按鈕控件

1、QPushButton

QPushButton繼承自QAbstractButton,形狀為長方形,文本、圖標(biāo)顯示在長方形區(qū)域。

import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget

class 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_())

2、QRadioButton

QRadioButton繼承自QAbstractButton,提供了一組可選的按鈕和標(biāo)簽,用戶可以選擇其中一個選項,標(biāo)簽用于顯示對應(yīng)的文本信息。QRadioButton是一種開關(guān)按鈕,可以切換為on或off,即checked或unchecked。在單選按鈕組里,一次只能選擇一個單選按鈕,如果需要多個獨占的按鈕組合,需要將其放到QGroupBox或QButtonGroup中。

import sys
from PyQt5.QtWidgets import QApplication, QRadioButton, QWidget

class 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_())

3、QCheckBox

QCheckBox繼承自QAbstractButton,提供一組帶文本標(biāo)簽的復(fù)選框,用戶可以選擇多個選項,復(fù)選框可以顯示文本和圖標(biāo)。
除了選中、未選中,QCheckBox有第三種狀態(tài):半選中,表示沒有變化。

import sys
from PyQt5.QtWidgets import QApplication, QCheckBox, QWidget

class 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

QComboBox是下拉列表框。

import sys
from PyQt5.QtWidgets import QApplication, QComboBox, QWidget

class 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

QSpinBox是一個計數(shù)器控件,允許用戶選擇一個整數(shù)值,通過單擊向上、向下按鈕或鍵盤的上下箭頭來增加減少當(dāng)前顯示的值,用戶也可以從編輯框輸入當(dāng)前值。默認(rèn)情況下,QSpinBox的取值范圍為0——99,每次改變的步長值為1。

import sys
from PyQt5.QtWidgets import QApplication, QSpinBox, QWidget

class 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

QSlider控件提供了一個垂直或水平的滑動條,是一個用于控制有界值的控件,允許用戶沿著水平或垂直方向在某一范圍內(nèi)移動滑塊,并將滑塊所在的位置轉(zhuǎn)換成一個合法范圍內(nèi)的整數(shù)值。

import sys
from PyQt5.QtWidgets import QApplication, QSlider, QWidget
from PyQt5.QtCore import Qt

class 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_())

九、對話框控件

1、QDialog

QDialog是對話框類,提供了三種窗口模態(tài),非模態(tài),模態(tài)和應(yīng)用程序模態(tài),使用setWindowModality方法設(shè)置窗口模態(tài)。
(1)非模態(tài)
非模態(tài)可以和應(yīng)用程序的其它窗×××互,使用Qt.NonModal進(jìn)行設(shè)置。
(2)窗口模態(tài)
窗口模態(tài)在未處理完成當(dāng)前對話框時,將阻止和對話框的父窗口進(jìn)行交互,使用Qt.Modal進(jìn)行設(shè)置。
(3)應(yīng)用程序模態(tài)
應(yīng)用程序模態(tài)阻止任何和其它窗口進(jìn)行交互,使用Qt.ApplicationModal。
QDialog及其派生類對話框在ESC按鍵按下時,對話框窗口將會默認(rèn)調(diào)用QDialog.reject方法,關(guān)閉對話框。
setWindowModality()方法可以設(shè)置窗口是否是模態(tài)窗口,Qt::WindowModality默認(rèn)值為Qt::NonModal,如果沒有設(shè)置Qt::WindowModality屬性值,每次用show()方法顯示出的窗口都是非模態(tài)窗口。
使用exec()方法顯示的對話框為模態(tài)對話框,同時會阻塞窗口的響應(yīng)直到用戶關(guān)閉對話框,并且返回DialogCode(包括Accepted和Rejected兩個值)結(jié)果。
如果沒有設(shè)置Qt::WindowModality屬性值,使用exec()方法顯示出的對話框默認(rèn)為應(yīng)用程序級模態(tài)對話框。所有使用exec()方法顯示的對話框在窗口關(guān)閉前會阻塞整個程序所有窗口的響應(yīng)。調(diào)用exec()方法后,對話框會阻塞,直到對話框關(guān)閉才會繼續(xù)執(zhí)行。在關(guān)閉對話框后exec()方法會返回Accepted或者Rejected,一般程序根據(jù)返回不同的結(jié)果進(jìn)行相應(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 sys
from PyQt5.QtWidgets import QApplication, QDialog, QWidget, QPushButton
from PyQt5.QtCore import Qt

class 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_())

2、QMessageBox

QMessageBox是一種通用的彈出式對話框,用于顯示消息,允許用戶通過點擊不同的標(biāo)準(zhǔn)按鈕對消息進(jìn)行反饋,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 sys
from PyQt5.QtWidgets import QApplication, QMessageBox, QWidget, QPushButton

class 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_())

3、QInputDialog

QInputDialog是一個標(biāo)準(zhǔn)對話框控件,由一個文本框和兩個按鈕(OK和Cancel)組成,用戶單擊OK按鈕或按下Enter鍵后,在父窗口可以接收通過QInputDialog輸入的信息。
QInputDialog.getInt從控件中獲取標(biāo)準(zhǔn)整型輸入
QInputDialog.getItem從控件中獲取列表的選項輸入
QInputDialog.getText從控件中獲取標(biāo)準(zhǔn)字符串輸入
QInputDialog.getDouble從控件中獲取標(biāo)準(zhǔn)浮點數(shù)輸入

import sys
from PyQt5.QtWidgets import QApplication, QInputDialog, QWidget, QPushButton

class 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_())

4、QFontDialog

QFontDialog是字體選擇對話框,可以讓用戶選擇所顯示的文本的字號大小、樣式和格式。QFontDialog.getFont可以從字體選擇對話框中獲取文本的顯示字號、樣式和格式。

import sys
from PyQt5.QtWidgets import QApplication, QFontDialog, QWidget, QPushButton
from PyQt5.QtGui import QFont

class 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_())

5、QFileDialog

QFileDialog是用于打開和保存文件的標(biāo)準(zhǔn)對話框,QFileDialog在打開文件時使用文件過濾器,用于顯示指定擴(kuò)展名的文件。

import sys
from PyQt5.QtWidgets import QApplication, QFileDialog, QWidget, QPushButton

class 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_())
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI