欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用Qt5實(shí)現(xiàn)水平導(dǎo)航欄的示例代碼

 更新時間:2023年03月06日 09:46:29   作者:3Blue1Red  
本文主要介紹了Python使用Qt5實(shí)現(xiàn)水平導(dǎo)航欄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

在 Qt5 中可以使用 QWidget 包含兩個水平布局,通過點(diǎn)擊水平布局里的按鈕,實(shí)現(xiàn)下標(biāo)滑動與頁面的切換。

請?zhí)砑訄D片描述

可以按照以下步驟來實(shí)現(xiàn)上面圖片中的功能:

導(dǎo)入必要的 Qt 包:

from PyQt5.QtCore import QPoint, QPropertyAnimation
from PyQt5.QtWidgets import QVBoxLayout, QHBoxLayout, QPushButton, QLabel

創(chuàng)建 ui_init 函數(shù),接收 self,和外部傳入的列表:

def ui_init(self, datas):

創(chuàng)建一個 list 來保存水平布局里的按鈕:

self.button_list = []

創(chuàng)建兩個水平布局 QHBoxLayout,一個放置 QPushButton, 一個放置 QLabel,并設(shè)置水平布局的內(nèi)容邊距以及組件邊距:

container = QHBoxLayout()
container.setSpacing(0)
container.setContentsMargins(10, 0, 10, 0)
container2 = QHBoxLayout()
container2.setContentsMargins(10, 0, 10, 0)

使用 for 循環(huán)來遍歷傳遞進(jìn)來的字符串列表,并且根據(jù)其長度創(chuàng)建 QPushButton,設(shè)置 QPushButton 的顯示樣式,最后將 QPushButton 添加進(jìn) QHBoxLayout,并且保存在 self.button_list 當(dāng)中:

for number, data in enumerate(datas):
    btn = QPushButton(data)
    btn.setStyleSheet(
        "QPushButton { border: none; "
        "height: 25px; }"
    )
    container.addWidget(btn)
    self.button_list.append(btn)

創(chuàng)建一個 QLabel 設(shè)置其大小和顏色,其中 Label 的寬度為測量 QHBoxLayout 的寬度 / QPushButton 的個數(shù):

self.label = QLabel()
self.label.setContentsMargins(0, 0, 0, 0)
self.label.setFixedWidth(int(710 / len(self.button_list)))
self.label.setFixedHeight(2)
self.label.setStyleSheet("QLabel { background-color: rgb(10, 96, 255); }")

創(chuàng)建一個QPropertyAnimation對象,設(shè)置動畫的持續(xù)時間為100毫秒,用于實(shí)現(xiàn)動畫效果:

self.animation = QPropertyAnimation(self.label, b'pos')
self.animation.setDuration(100)

將 QLabel 添加進(jìn)水平布局中,并且在其右邊設(shè)置一個伸縮量確保 QLabel 初始化時在第一個按鈕下方:

container2.addWidget(self.label)
# 設(shè)置伸縮量使label處于左邊
container2.addStretch(1)
self.addLayout(container)
self.addLayout(container2)

使用 for 循環(huán)遍歷 self.button_list,讓列表中的 QPushButton 連接槽函數(shù):

for btn in self.button_list:
    btn.clicked.connect(lambda state, b=btn: self.buttonClicked(b))

創(chuàng)建一個槽函數(shù):

def buttonClicked(self, button):

在槽函數(shù)中,獲取按鈕和標(biāo)簽的當(dāng)前位置:

pos = button.pos()
current_pos = self.label.pos()

設(shè)置動畫的起始值和結(jié)束值:

start_value = QPoint(current_pos.x(), current_pos.y())
end_value = QPoint(pos.x(), pos.y() + button.height())
self.animation.setStartValue(start_value)
self.animation.setEndValue(end_value)

啟用動畫:

self.animation.start()

以下為完整代碼示例:

from PyQt5.QtCore import QPoint, QPropertyAnimation
from PyQt5.QtWidgets import QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QApplication


class TopTitle(QVBoxLayout):
    def __init__(self, datas):
        super().__init__()
        self.ui_init(datas)

    def ui_init(self, datas):
        self.button_list = []

        container = QHBoxLayout()
        container.setSpacing(0)
        container.setContentsMargins(10, 0, 10, 0)
        container2 = QHBoxLayout()
        container2.setContentsMargins(10, 0, 10, 0)

        for number, data in enumerate(datas):
            # 此時num是元素的序號,data為元素
            # 水平布局包含多個垂直布局
            btn = QPushButton(data)
            btn.setStyleSheet(
                "QPushButton { border: none; "
                "height: 25px; }"
            )
            container.addWidget(btn)
            self.button_list.append(btn)

        self.label = QLabel()
        self.label.setContentsMargins(0, 0, 0, 0)
        self.label.setFixedWidth(int(710 / len(self.button_list))) # 710為手動測量QHBoxLayout的寬度
        self.label.setFixedHeight(2)
        self.label.setStyleSheet("QLabel { background-color: rgb(10, 96, 255); }")

        # 創(chuàng)建一個QPropertyAnimation對象,用于實(shí)現(xiàn)動畫效果
        self.animation = QPropertyAnimation(self.label, b'pos')
        self.animation.setDuration(100)  # 設(shè)置動畫的持續(xù)時間為100毫秒

        container2.addWidget(self.label)
        # 設(shè)置伸縮量使label處于左邊
        container2.addStretch(1)
        self.addLayout(container)
        self.addLayout(container2)

        for btn in self.button_list:
            btn.clicked.connect(lambda state, b=btn: self.buttonClicked(b))
            # 這里,我們?yōu)?lambda 表達(dá)式添加了額外的參數(shù) state,以兼容 QPushButton.clicked 信號的多重重載。
            # 同時,使用默認(rèn)參數(shù) b=btn 來確保 button 參數(shù)在 lambda 表達(dá)式中被正確地傳遞。

    def buttonClicked(self, button):
        # 獲取按鈕的位置
        pos = button.pos()

        # 獲取標(biāo)簽的當(dāng)前位置
        current_pos = self.label.pos()

        # 設(shè)置動畫的起始值和結(jié)束值
        start_value = QPoint(current_pos.x(), current_pos.y())
        end_value = QPoint(pos.x(), pos.y() + button.height())

        # 設(shè)置動畫的起始值和結(jié)束值
        self.animation.setStartValue(start_value)
        self.animation.setEndValue(end_value)

        # 啟動動畫
        self.animation.start()

if __name__ == '__main__':
    app = QApplication([])
    
    title = TopTitle(["7", "8", "9", "+", "7", "8", "9", "+"])
    
    w = QWidget()
    w.resize(710, 200)

	w.setLayout(title)
    w.show()
    
    app.exec_()

最后要實(shí)現(xiàn)頁面切換的方法,就是在按鍵的槽函數(shù)中,設(shè)置 StackedLayout 的 index 就可以實(shí)現(xiàn)了,當(dāng)然也可以在創(chuàng)建對象后,在其他 QWidget 中獲取 TopTitle.button_list 中的 QPushButton,來自定義按鈕的鏈接。這樣一個自適應(yīng)標(biāo)簽欄數(shù)量的水平導(dǎo)航欄就實(shí)現(xiàn)了。

到此這篇關(guān)于Python使用Qt5實(shí)現(xiàn)水平導(dǎo)航欄的示例代碼的文章就介紹到這了,更多相關(guān)Qt5 水平導(dǎo)航欄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • pandas修改DataFrame列名的實(shí)現(xiàn)方法

    pandas修改DataFrame列名的實(shí)現(xiàn)方法

    這篇文章主要介紹了pandas修改DataFrame列名的實(shí)現(xiàn)方法, 文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • python中使用print輸出中文的方法

    python中使用print輸出中文的方法

    今天小編就為大家分享一篇python中使用print輸出中文的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python編程之黑板上排列組合,你舍得解開嗎

    Python編程之黑板上排列組合,你舍得解開嗎

    這篇文章主要介紹了Python排列組合算法的相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • 三分鐘教會你用Python+OpenCV批量裁剪xml格式標(biāo)注的圖片

    三分鐘教會你用Python+OpenCV批量裁剪xml格式標(biāo)注的圖片

    最近學(xué)習(xí)網(wǎng)絡(luò)在線課程的過程中,為了方便課后復(fù)習(xí),使用手機(jī)截取了大量的圖片,下面這篇文章主要給大家介紹了如何通過三分鐘教會你用Python+OpenCV批量裁剪xml格式標(biāo)注圖片的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • 一篇文章帶你了解python元組基礎(chǔ)

    一篇文章帶你了解python元組基礎(chǔ)

    今天小編就為大家分享一篇關(guān)于Python中的元組介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2021-08-08
  • 如何將寫好的pygame游戲打包成exe文件

    如何將寫好的pygame游戲打包成exe文件

    這篇文章主要介紹了如何將寫好的pygame游戲打包成exe文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 通過python讀取txt文件和繪制柱形圖的實(shí)現(xiàn)代碼

    通過python讀取txt文件和繪制柱形圖的實(shí)現(xiàn)代碼

    這篇文章主要介紹了通過python讀取txt文件和繪制柱形圖的實(shí)現(xiàn)代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • 查看已安裝tensorflow版本的方法示例

    查看已安裝tensorflow版本的方法示例

    這篇文章主要介紹了查看已安裝tensorflow版本的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • PyTorch?Tensor創(chuàng)建實(shí)現(xiàn)

    PyTorch?Tensor創(chuàng)建實(shí)現(xiàn)

    本文主要介紹了PyTorch?Tensor創(chuàng)建實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • python去除字符串中空格的6種常用方法

    python去除字符串中空格的6種常用方法

    最近業(yè)務(wù)需要對Pyhon中的一些字符串內(nèi)容去除空格,方便后續(xù)處理,下面這篇文章主要給大家介紹了關(guān)于python去除字符串中空格的6種常用方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05

最新評論