最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

請(qǐng)?zhí)砑訄D片描述

可以按照以下步驟來(lái)實(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)建一個(gè) list 來(lái)保存水平布局里的按鈕:

self.button_list = []

創(chuàng)建兩個(gè)水平布局 QHBoxLayout,一個(gè)放置 QPushButton, 一個(gè)放置 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)來(lái)遍歷傳遞進(jìn)來(lái)的字符串列表,并且根據(jù)其長(zhǎng)度創(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)建一個(gè) QLabel 設(shè)置其大小和顏色,其中 Label 的寬度為測(cè)量 QHBoxLayout 的寬度 / QPushButton 的個(gè)數(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)建一個(gè)QPropertyAnimation對(duì)象,設(shè)置動(dòng)畫(huà)的持續(xù)時(shí)間為100毫秒,用于實(shí)現(xiàn)動(dòng)畫(huà)效果:

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

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

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)建一個(gè)槽函數(shù):

def buttonClicked(self, button):

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

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

設(shè)置動(dòng)畫(huà)的起始值和結(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)

啟用動(dòng)畫(huà):

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):
            # 此時(shí)num是元素的序號(hào),data為元素
            # 水平布局包含多個(gè)垂直布局
            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為手動(dòng)測(cè)量QHBoxLayout的寬度
        self.label.setFixedHeight(2)
        self.label.setStyleSheet("QLabel { background-color: rgb(10, 96, 255); }")

        # 創(chuàng)建一個(gè)QPropertyAnimation對(duì)象,用于實(shí)現(xiàn)動(dòng)畫(huà)效果
        self.animation = QPropertyAnimation(self.label, b'pos')
        self.animation.setDuration(100)  # 設(shè)置動(dòng)畫(huà)的持續(xù)時(shí)間為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 信號(hào)的多重重載。
            # 同時(shí),使用默認(rèn)參數(shù) b=btn 來(lái)確保 button 參數(shù)在 lambda 表達(dá)式中被正確地傳遞。

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

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

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

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

        # 啟動(dòng)動(dòng)畫(huà)
        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)頁(yè)面切換的方法,就是在按鍵的槽函數(shù)中,設(shè)置 StackedLayout 的 index 就可以實(shí)現(xiàn)了,當(dāng)然也可以在創(chuàng)建對(duì)象后,在其他 QWidget 中獲取 TopTitle.button_list 中的 QPushButton,來(lái)自定義按鈕的鏈接。這樣一個(gè)自適應(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python使用Pandas庫(kù)實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)讀寫(xiě)

    Python使用Pandas庫(kù)實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)讀寫(xiě)

    本次分享將介紹如何在Python中使用Pandas庫(kù)實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的讀寫(xiě),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • 簡(jiǎn)單有效上手Python3異步asyncio問(wèn)題

    簡(jiǎn)單有效上手Python3異步asyncio問(wèn)題

    這篇文章主要介紹了簡(jiǎn)單有效上手Python3異步asyncio問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • VSCode配置python.analysis.extraPaths作用詳解

    VSCode配置python.analysis.extraPaths作用詳解

    本文主要介紹了VSCode配置python.analysis.extraPaths作用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-12-12
  • 詳細(xì)探究Python中的字典容器

    詳細(xì)探究Python中的字典容器

    這篇文章主要介紹了Python中的字典容器,本文來(lái)自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下
    2015-04-04
  • 用Python簡(jiǎn)陋模擬n階魔方

    用Python簡(jiǎn)陋模擬n階魔方

    這篇文章主要介紹了用Python簡(jiǎn)陋模擬n階魔方,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴呢有一定的幫助,需要的朋友可以參考下
    2021-04-04
  • python中函數(shù)傳參詳解

    python中函數(shù)傳參詳解

    本文給大家介紹的是Python中函數(shù)傳參的使用方法及示例,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下
    2016-07-07
  • python實(shí)例化對(duì)象的具體方法

    python實(shí)例化對(duì)象的具體方法

    在本篇文章里小編給大家整理的是關(guān)于python實(shí)例化對(duì)象的具體方法,有興趣的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • Python的Random庫(kù)的使用方法詳解

    Python的Random庫(kù)的使用方法詳解

    這篇文章主要介紹了Python的Random庫(kù)的使用方法詳解,random庫(kù)是使用隨機(jī)數(shù)的Python標(biāo)準(zhǔn)庫(kù),python中用于生成偽隨機(jī)數(shù)的函數(shù)庫(kù)是random,需要的朋友可以參考下
    2023-07-07
  • Python中函數(shù)的參數(shù)定義和可變參數(shù)用法實(shí)例分析

    Python中函數(shù)的參數(shù)定義和可變參數(shù)用法實(shí)例分析

    這篇文章主要介紹了Python中函數(shù)的參數(shù)定義和可變參數(shù)用法,以實(shí)例形式較為詳細(xì)的分析了Python中參數(shù)定義與可變參數(shù)的具體使用方法,需要的朋友可以參考下
    2015-06-06
  • 在Python中存儲(chǔ)字符串

    在Python中存儲(chǔ)字符串

    這篇文章主要介紹了在Python中存儲(chǔ)字符串,文章通過(guò)unicode展開(kāi)主題相關(guān)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05

最新評(píng)論

宜兴市| 安丘市| 金平| 阜康市| 德庆县| 清原| 长丰县| 银川市| 岳普湖县| 北流市| 隆子县| 错那县| 昆明市| 石阡县| 甘泉县| 南充市| 河源市| 平阴县| 湛江市| 来安县| 高唐县| 长治县| 长顺县| 叙永县| 三门县| 偏关县| 类乌齐县| 塔城市| 开封市| 柯坪县| 临高县| 安泽县| 资兴市| 秀山| 炎陵县| 深州市| 利川市| 鹤庆县| 依兰县| 类乌齐县| 光山县|