demo.py 8.7 KB
Newer Older
之一Yo's avatar
之一Yo 已提交
1 2 3 4
# coding:utf-8
import sys

from PyQt5.QtCore import Qt, QSize
之一Yo's avatar
之一Yo 已提交
5
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QAction, QGridLayout
6 7
from qfluentwidgets import (Action, DropDownPushButton, DropDownToolButton, PushButton, PrimaryPushButton,
                            HyperlinkButton, setTheme, Theme, ToolButton, ToggleButton, RoundMenu,
之一Yo's avatar
之一Yo 已提交
8
                            SplitPushButton, SplitToolButton, PrimaryToolButton, PrimarySplitPushButton,
9 10 11 12
                            PrimarySplitToolButton, PrimaryDropDownPushButton, PrimaryDropDownToolButton,
                            TogglePushButton, ToggleToolButton, TransparentPushButton, TransparentToolButton,
                            TransparentToggleToolButton, TransparentTogglePushButton, TransparentDropDownToolButton,
                            TransparentDropDownPushButton)
之一Yo's avatar
之一Yo 已提交
13 14 15
from qfluentwidgets import FluentIcon as FIF


之一Yo's avatar
之一Yo 已提交
16
class ToolButtonDemo(QWidget):
之一Yo's avatar
之一Yo 已提交
17 18 19 20

    def __init__(self):
        super().__init__()
        # setTheme(Theme.DARK)
之一Yo's avatar
之一Yo 已提交
21 22 23 24 25
        self.setStyleSheet("ToolButtonDemo{background: white}")

        self.menu = RoundMenu(parent=self)
        self.menu.addAction(QAction(FIF.SEND_FILL.icon(), 'Send'))
        self.menu.addAction(QAction(FIF.SAVE.icon(), 'Save'))
之一Yo's avatar
之一Yo 已提交
26

27
        # tool button
之一Yo's avatar
之一Yo 已提交
28 29 30 31 32 33
        self.toolButton = ToolButton(FIF.SETTING, self)

        # change the size of tool button
        # self.toolButton.resize(50, 50)
        # self.toolButton.setIconSize(QSize(30, 30))

之一Yo's avatar
之一Yo 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        # drop down tool button
        self.dropDownToolButton = DropDownToolButton(FIF.MAIL, self)
        self.dropDownToolButton.setMenu(self.menu)

        # split tool button
        self.splitToolButton = SplitToolButton(FIF.GITHUB, self)
        self.splitToolButton.setFlyout(self.menu)

        # primary color tool button
        self.primaryToolButton = PrimaryToolButton(FIF.SETTING, self)

        # primary color drop down tool button
        self.primaryDropDownToolButton = PrimaryDropDownToolButton(FIF.MAIL, self)
        self.primaryDropDownToolButton.setMenu(self.menu)

        # primary color split tool button
        self.primarySplitToolButton = PrimarySplitToolButton(FIF.GITHUB, self)
        self.primarySplitToolButton.setFlyout(self.menu)

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        # toggle tool button
        self.toggleToolButton = ToggleToolButton(FIF.SETTING, self)
        self.toggleToolButton.toggled.connect(lambda: print('Toggled'))
        self.toggleToolButton.toggle()

        # transparent toggle tool button
        self.transparentToggleToolButton = TransparentToggleToolButton(FIF.GITHUB, self)

        # transparent tool button
        self.tranparentToolButton = TransparentToolButton(FIF.MAIL, self)

        # transparent drop down tool button
        self.transparentDropDownToolButton = TransparentDropDownToolButton(FIF.MAIL, self)
        self.transparentDropDownToolButton.setMenu(self.menu)

之一Yo's avatar
之一Yo 已提交
68 69 70 71 72 73 74 75
        # add buttons to layout
        self.gridLayout = QGridLayout(self)
        self.gridLayout.addWidget(self.toolButton, 0, 0)
        self.gridLayout.addWidget(self.dropDownToolButton, 0, 1)
        self.gridLayout.addWidget(self.splitToolButton, 0, 2)
        self.gridLayout.addWidget(self.primaryToolButton, 1, 0)
        self.gridLayout.addWidget(self.primaryDropDownToolButton, 1, 1)
        self.gridLayout.addWidget(self.primarySplitToolButton, 1, 2)
76 77 78 79
        self.gridLayout.addWidget(self.toggleToolButton, 2, 0)
        self.gridLayout.addWidget(self.transparentToggleToolButton, 2, 1)
        self.gridLayout.addWidget(self.tranparentToolButton, 3, 0)
        self.gridLayout.addWidget(self.transparentDropDownToolButton, 3, 1)
之一Yo's avatar
之一Yo 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

        self.resize(300, 300)


class PushButtonDemo(QWidget):

    def __init__(self):
        super().__init__()
        # setTheme(Theme.DARK)
        self.setStyleSheet('PushButtonDemo{background:white}')

        self.menu = RoundMenu(parent=self)
        self.menu.addAction(Action(FIF.BASKETBALL, 'Basketball'))
        self.menu.addAction(Action(FIF.ALBUM, 'Sing'))
        self.menu.addAction(Action(FIF.MUSIC, 'Music'))

96
        # push button
之一Yo's avatar
之一Yo 已提交
97 98 99
        self.pushButton1 = PushButton('Standard push button')
        self.pushButton2 = PushButton('Standard push button with icon', self, FIF.FOLDER)

之一Yo's avatar
之一Yo 已提交
100
        # primary color push button
之一Yo's avatar
之一Yo 已提交
101 102 103
        self.primaryButton1 = PrimaryPushButton('Accent style button', self)
        self.primaryButton2 = PrimaryPushButton('Accent style button with icon', self, FIF.UPDATE)

104 105 106 107
        # transparent push button
        self.transparentPushButton1 = TransparentPushButton('Transparent push button', self)
        self.transparentPushButton2 = TransparentPushButton('Transparent push button', self, FIF.BOOK_SHELF)

108
        # toggle button
109 110 111 112 113 114
        self.toggleButton1 = TogglePushButton('Toggle push button', self)
        self.toggleButton2 = TogglePushButton('Toggle push button', self, FIF.SEND)

        # transparent toggle push button
        self.transparentTogglePushButton1 = TransparentTogglePushButton('Transparent toggle button', self)
        self.transparentTogglePushButton2 = TransparentTogglePushButton('Transparent toggle button', self, FIF.BOOK_SHELF)
之一Yo's avatar
之一Yo 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127

        # drop down push button
        self.dropDownPushButton1 = DropDownPushButton('Email', self)
        self.dropDownPushButton2 = DropDownPushButton('Email', self, FIF.MAIL)
        self.dropDownPushButton1.setMenu(self.menu)
        self.dropDownPushButton2.setMenu(self.menu)

        # primary color drop down push button
        self.primaryDropDownPushButton1 = PrimaryDropDownPushButton('Email', self)
        self.primaryDropDownPushButton2 = PrimaryDropDownPushButton('Email', self, FIF.MAIL)
        self.primaryDropDownPushButton1.setMenu(self.menu)
        self.primaryDropDownPushButton2.setMenu(self.menu)

128 129 130 131 132 133
        # primary color drop down push button
        self.transparentDropDownPushButton1 = TransparentDropDownPushButton('Email', self)
        self.transparentDropDownPushButton2 = TransparentDropDownPushButton('Email', self, FIF.MAIL)
        self.transparentDropDownPushButton1.setMenu(self.menu)
        self.transparentDropDownPushButton2.setMenu(self.menu)

之一Yo's avatar
之一Yo 已提交
134 135 136 137 138 139 140 141 142 143 144
        # split push button
        self.splitPushButton1 = SplitPushButton('Split push button', self)
        self.splitPushButton2 = SplitPushButton('Split push button', self, FIF.GITHUB)
        self.splitPushButton1.setFlyout(self.menu)
        self.splitPushButton2.setFlyout(self.menu)

        # primary split push button
        self.primarySplitPushButton1 = PrimarySplitPushButton('Split push button', self)
        self.primarySplitPushButton2 = PrimarySplitPushButton('Split push button', self, FIF.GITHUB)
        self.primarySplitPushButton1.setFlyout(self.menu)
        self.primarySplitPushButton2.setFlyout(self.menu)
145 146

        # hyperlink button
之一Yo's avatar
之一Yo 已提交
147 148 149 150 151 152
        self.hyperlinkButton = HyperlinkButton(
            url='https://github.com/zhiyiYo/PyQt-Fluent-Widgets',
            text='Hyper link button',
            parent=self
        )

之一Yo's avatar
之一Yo 已提交
153 154 155 156 157
        self.gridLayout = QGridLayout(self)
        self.gridLayout.addWidget(self.pushButton1, 0, 0)
        self.gridLayout.addWidget(self.pushButton2, 0, 1)
        self.gridLayout.addWidget(self.primaryButton1, 1, 0)
        self.gridLayout.addWidget(self.primaryButton2, 1, 1)
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        self.gridLayout.addWidget(self.transparentPushButton1, 2, 0)
        self.gridLayout.addWidget(self.transparentPushButton2, 2, 1)

        self.gridLayout.addWidget(self.toggleButton1, 3, 0)
        self.gridLayout.addWidget(self.toggleButton2, 3, 1)
        self.gridLayout.addWidget(self.transparentTogglePushButton1, 4, 0)
        self.gridLayout.addWidget(self.transparentTogglePushButton2, 4, 1)

        self.gridLayout.addWidget(self.splitPushButton1, 5, 0)
        self.gridLayout.addWidget(self.splitPushButton2, 5, 1)
        self.gridLayout.addWidget(self.primarySplitPushButton1, 6, 0)
        self.gridLayout.addWidget(self.primarySplitPushButton2, 6, 1)

        self.gridLayout.addWidget(self.dropDownPushButton1, 7, 0, Qt.AlignLeft)
        self.gridLayout.addWidget(self.dropDownPushButton2, 7, 1, Qt.AlignLeft)
        self.gridLayout.addWidget(self.primaryDropDownPushButton1, 8, 0, Qt.AlignLeft)
        self.gridLayout.addWidget(self.primaryDropDownPushButton2, 8, 1, Qt.AlignLeft)
        self.gridLayout.addWidget(self.transparentDropDownPushButton1, 9, 0, Qt.AlignLeft)
        self.gridLayout.addWidget(self.transparentDropDownPushButton2, 9, 1, Qt.AlignLeft)

        self.gridLayout.addWidget(self.hyperlinkButton, 10, 0)

        self.resize(600, 700)
之一Yo's avatar
之一Yo 已提交
181 182 183 184 185 186 187 188 189 190 191



if __name__ == '__main__':
    # enable dpi scale
    QApplication.setHighDpiScaleFactorRoundingPolicy(
        Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)

    app = QApplication(sys.argv)
之一Yo's avatar
之一Yo 已提交
192 193 194 195 196
    w1 = ToolButtonDemo()
    w1.show()

    w2 = PushButtonDemo()
    w2.show()
之一Yo's avatar
之一Yo 已提交
197
    app.exec_()