当前位置:   article > 正文

38-tkinter-ttkbootstrap-主题_ttkbootstrap主题创建

ttkbootstrap主题创建

主题

ttkbootstrap 附带了许多风格精美的浅色和深色主题,您可以在 安装 ttkbootstrap 后 在终端中键入此命令,在演示中查看这些主题。

python -m ttkbootstrap
  • 1

将看到如下图所示的演示屏幕。 在 Select a theme 下拉框中更改主题选择,演示屏幕将更新为新主题。

image-20221216202525638

浅色主题

image-20221216202710056

image-20221216202737432

image-20221216202757158

image-20221216202822099

image-20221216202853210

image-20221216202914077

image-20221216202937930

image-20221216202952721

image-20221216203201690

image-20221216203220252

深色主题

image-20221216203020266

image-20221216203046809

image-20221216203101161

image-20221216203118372

image-20221216203137252

主题定义

标准主题在 ttkbootstrap.themes.standard 模块中定义。

{
    "cosmo": {
        "type": "light",
        "colors": {
            "primary": "#2780e3",
            "secondary": "#7E8081",
            "success": "#3fb618",
            "info": "#9954bb",
            "warning": "#ff7518",
            "danger": "#ff0039",
            "light": "#F8F9FA",
            "dark": "#373A3C",
            "bg": "#ffffff",
            "fg": "#373a3c",
            "selectbg": "#7e8081",
            "selectfg": "#ffffff",
            "border": "#ced4da",
            "inputfg": "#373a3c",
            "inputbg": "#fdfdfe",
        },
    },
    "flatly": {
        "type": "light",
        "colors": {
            "primary": "#2c3e50",
            "secondary": "#95a5a6",
            "success": "#18bc9c",
            "info": "#3498db",
            "warning": "#f39c12",
            "danger": "#e74c3c",
            "light": "#ECF0F1",
            "dark": "#7B8A8B",
            "bg": "#ffffff",
            "fg": "#212529",
            "selectbg": "#95a5a6",
            "selectfg": "#ffffff",
            "border": "#ced4da",
            "inputfg": "#212529",
            "inputbg": "#ffffff",
        },
    },
    "litera": {
        "type": "light",
        "colors": {
            "primary": "#4582ec",
            "secondary": "#adb5bd",
            "success": "#02b875",
            "info": "#17a2b8",
            "warning": "#f0ad4e",
            "danger": "#d9534f",
            "light": "#F8F9FA",
            "dark": "#343A40",
            "bg": "#ffffff",
            "fg": "#343a40",
            "selectbg": "#adb5bd",
            "selectfg": "#ffffff",
            "border": "#bfbfbf",
            "inputfg": "#343a40",
            "inputbg": "#fff",
        },
    },
    "minty": {
        "type": "light",
        "colors": {
            "primary": "#78c2ad",
            "secondary": "#f3969a",
            "success": "#56cc9d",
            "info": "#6cc3d5",
            "warning": "#ffce67",
            "danger": "#ff7851",
            "light": "#F8F9FA",
            "dark": "#343A40",
            "bg": "#ffffff",
            "fg": "#5a5a5a",
            "selectbg": "#f3969a",
            "selectfg": "#ffffff",
            "border": "#ced4da",
            "inputfg": "#696969",
            "inputbg": "#fff",
        },
    },
    "lumen": {
        "type": "light",
        "colors": {
            "primary": "#158cba",
            "secondary": "#919191",
            "success": "#28b62c",
            "info": "#75caeb",
            "warning": "#ff851b",
            "danger": "#ff4136",
            "light": "#F6F6F6",
            "dark": "#555555",
            "bg": "#ffffff",
            "fg": "#555555",
            "selectbg": "#919191",
            "selectfg": "#ffffff",
            "border": "#ced4da",
            "inputfg": "#555555",
            "inputbg": "#fff",
        },
    },
    "sandstone": {
        "type": "light",
        "colors": {
            "primary": "#325D88",
            "secondary": "#8e8c84",
            "success": "#93c54b",
            "info": "#29abe0",
            "warning": "#f47c3c",
            "danger": "#d9534f",
            "light": "#F8F5F0",
            "dark": "#3E3F3A",
            "bg": "#ffffff",
            "fg": "#3e3f3a",
            "selectbg": "#8e8c84",
            "selectfg": "#ffffff",
            "border": "#ced4da",
            "inputfg": "#6E6D69",
            "inputbg": "#fff",
        },
    },
    "yeti": {
        "type": "light",
        "colors": {
            "primary": "#008cba",
            "secondary": "#707070",
            "success": "#43ac6a",
            "info": "#5bc0de",
            "warning": "#e99002",
            "danger": "#f04124",
            "light": "#EEEEEE",
            "dark": "#222222",
            "bg": "#ffffff",
            "fg": "#222222",
            "selectbg": "#707070",
            "selectfg": "#ffffff",
            "border": "#cccccc",
            "inputfg": "#222222",
            "inputbg": "#fff",
        },
    },
    "pulse": {
        "type": "light",
        "colors": {
            "primary": "#593196",
            "secondary": "#69676E",
            "success": "#13b955",
            "info": "#009cdc",
            "warning": "#efa31d",
            "danger": "#fc3939",
            "light": "#F9F8FC",
            "dark": "#17141F",
            "bg": "#ffffff",
            "fg": "#444444",
            "selectbg": "#69676e",
            "selectfg": "#ffffff",
            "border": "#cbc8d0",
            "inputfg": "#444444",
            "inputbg": "#fdfdfe",
        },
    },
    "united": {
        "type": "light",
        "colors": {
            "primary": "#e95420",
            "secondary": "#aea79f",
            "success": "#38b44a",
            "info": "#17a2b8",
            "warning": "#efb73e",
            "danger": "#df382c",
            "light": "#E9ECEF",
            "dark": "#772953",
            "bg": "#ffffff",
            "fg": "#333333",
            "selectbg": "#aea79f",
            "selectfg": "#ffffff",
            "border": "#ced4da",
            "inputfg": "#333333",
            "inputbg": "#fff",
        },
    },
    "morph": {
        "type": "light",
        "colors": {
            "primary": "#378DFC",
            "secondary": "#aaaaaa",
            "success": "#43cc29",
            "info": "#5B62F4",
            "warning": "#FFC107",
            "danger": "#E52527",
            "light": "#F0F5FA",
            "dark": "#212529",
            "bg": "#D9E3F1",
            "fg": "#7B8AB8",
            "selectbg": "#aaaaaa",
            "selectfg": "#FBFDFF",
            "border": "#B9C7DA",
            "inputfg": "#7F8EBA",
            "inputbg": "#F0F5FA",
        },
    },
    "journal": {
        "type": "light",
        "colors": {
            "primary": "#eb6864",
            "secondary": "#aaaaaa",
            "success": "#22b24c",
            "info": "#336699",
            "warning": "#f5e625",
            "danger": "#f57a00",
            "light": "#F8F9FA",
            "dark": "#222222",
            "bg": "#ffffff",
            "fg": "#222222",
            "selectbg": "#aaaaaa",
            "selectfg": "#ffffff",
            "border": "#ced4da",
            "inputfg": "#565656",
            "inputbg": "#fff",
        },
    },
    "darkly": {
        "type": "dark",
        "colors": {
            "primary": "#375a7f",
            "secondary": "#444444",
            "success": "#00bc8c",
            "info": "#3498db",
            "warning": "#f39c12",
            "danger": "#e74c3c",
            "light": "#ADB5BD",
            "dark": "#303030",
            "bg": "#222222",
            "fg": "#ffffff",
            "selectbg": "#555555",
            "selectfg": "#ffffff",
            "border": "#222222",
            "inputfg": "#ffffff",
            "inputbg": "#2f2f2f",
        },
    },
    "superhero": {
        "type": "dark",
        "colors": {
            "primary": "#4c9be8",
            "secondary": "#4e5d6c",
            "success": "#5cb85c",
            "info": "#5bc0de",
            "warning": "#f0ad4e",
            "danger": "#d9534f",
            "light": "#ABB6C2",
            "dark": "#20374C",
            "bg": "#2b3e50",
            "fg": "#ffffff",
            "selectbg": "#526170",
            "selectfg": "#ffffff",
            "border": "#222222",
            "inputfg": "#ebebeb",
            "inputbg": "#32465a",
        },
    },
    "solar": {
        "type": "dark",
        "colors": {
            "primary": "#bc951a",
            "secondary": "#94a2a4",
            "success": "#44aca4",
            "info": "#3f98d7",
            "warning": "#d05e2f",
            "danger": "#d95092",
            "light": "#A9BDBD",
            "dark": "#073642",
            "bg": "#002B36",
            "fg": "#ffffff",
            "selectbg": "#0b5162",
            "selectfg": "#ffffff",
            "border": "#00252e",
            "inputfg": "#A9BDBD",
            "inputbg": "#073642",
        },
    },
    "cyborg": {
        "type": "dark",
        "colors": {
            "primary": "#2a9fd6",
            "secondary": "#555555",
            "success": "#77b300",
            "info": "#9933cc",
            "warning": "#ff8800",
            "danger": "#cc0000",
            "light": "#ADAFAE",
            "dark": "#222222",
            "bg": "#060606",
            "fg": "#ffffff",
            "selectbg": "#454545",
            "selectfg": "#ffffff",
            "border": "#060606",
            "inputfg": "#ffffff",
            "inputbg": "#191919",
        },
    },
    "vapor": {
        "type": "dark",
        "colors": {
            "primary": "#6e40c0",
            "secondary": "#ea38b8",
            "success": "#3af180",
            "info": "#1da2f2",
            "warning": "#ffbd05",
            "danger": "#e34b54",
            "light": "#44d7e8",
            "dark": "#170229",
            "bg": "#190831",
            "fg": "#32fbe2",
            "selectbg": "#461a8a",
            "selectfg": "#ffffff",
            "border": "#060606",
            "inputfg": "#bfb6cd",
            "inputbg": "#30115e",
        },
    },
    "simplex": {
        "type": "light",
        "colors": {
            "primary": "#d8220e",
            "secondary": "#858e96",
            "success": "#469307",
            "info": "#0099ce",
            "warning": "#d88220",
            "danger": "#9a479e",
            "light": "#f2f2f2",
            "dark": "#3b3d3f",
            "bg": "#fcfcfc",
            "fg": "#3b3d3f",
            "selectbg": "#a9afb6",
            "selectfg": "#ffffff",
            "border": "#858e96",
            "inputfg": "#3b3d3f",
            "inputbg": "#fcfcfc",
        },
    },
    "cerculean": {
        "type": "light",
        "colors": {
            "primary": "#4bb1ea",
            "secondary": "#a9b4be",
            "success": "#84b251",
            "info": "#225384",
            "warning": "#e16e25",
            "danger": "#cf3c40",
            "light": "#eceef1",
            "dark": "#33383e",
            "bg": "#ffffff",
            "fg": "#2ea4e7",
            "selectbg": "#adb5bd",
            "selectfg": "#ffffff",
            "border": "#a9b4be",
            "inputfg": "#495057",
            "inputbg": "#ffffff",
        },
    },
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363

设置全局主题切换

通过 ttk.Style().theme_use(theme_value)进行设置

ttk.Style().theme_names() 以列表的形式返回多个主题名

import ttkbootstrap as ttk
from ttkbootstrap.constants import *

root = ttk.Window()
style = ttk.Style()
theme_names = style.theme_names()  # 以列表的形式返回多个主题名
theme_selection = ttk.Frame(root, padding=(10, 10, 10, 0))

theme_selection.pack(fill=X, expand=YES)

lbl = ttk.Label(theme_selection, text="选择主题:")
theme_cbo = ttk.Combobox(
    master=theme_selection,
    text=style.theme.name,
    values=theme_names,
)
theme_cbo.pack(padx=10, side=RIGHT)
theme_cbo.current(theme_names.index(style.theme.name))
lbl.pack(side=RIGHT)


def change_theme(event):
    theme_cbo_value = theme_cbo.get()
    style.theme_use(theme_cbo_value)
    theme_selected.configure(text=theme_cbo_value)
    theme_cbo.selection_clear()


theme_cbo.bind('<<ComboboxSelected>>', change_theme)
theme_selected = ttk.Label(
    master=theme_selection,
    text="litera",
    font="-size 24 -weight bold"
)
theme_selected.pack(side=LEFT)
root.mainloop()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

TTK创建器

TTK创建器 与 ttkbootstrap 打包,以便可以修改、保存、导出和导入创建的主题。

要运行该程序,您安装ttkbootstrap后 在控制台中键入以下命令:

python -m ttkcreator
  • 1

image-20221216203528905

创建一个新主题

所有用于更改主题的控件都在左侧控制框架上。

  1. 使用 name 条目命名您的主题
  2. 选择一个基础主题;这将设置 初始 颜色
  3. 使用两个选项之一为每个颜色 关键字 选择一种颜色
推荐阅读
相关标签