簡體   English   中英

如何更改 tkinter 應用程序的整體主題?

[英]How do I change the overall theme of a tkinter application?

我想將我的 tkinter 應用程序的主題更改為 clam。

代碼是什么,我把它放在哪里? 我試過了:

from tkinter import *
from tkinter.ttk import *
s=ttk.Style()
s.theme_use('clam')

要更改主題,請使用主題名稱作為參數調用.theme_use()

來自https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-theme-layer.html

許多與主題相關的操作要求您擁有ttk.Style()類的實例(在 Python 類的意義上)。 例如,要獲取安裝中可用主題的列表:

 >>> import ttk # import tkinter.ttk as ttk for Python 3 >>> s=ttk.Style() >>> s.theme_names() ('clam', 'alt', 'default', 'classic')

.theme_names()方法返回一個包含可用樣式名稱的元組。 ' classic ' 主題為您提供原始的、前 ttk 的外觀。

要確定您默認獲得哪個主題,請使用不帶參數的.theme_use()方法。 要更改當前主題,請使用所需的主題名稱作為參數調用相同的方法:

 >>> s.theme_use() 'default' >>> s.theme_use('alt') >>> s.theme_use() 'alt'
>>> from tkinter import ttk

>>> s=ttk.Style()

>>> s.theme_names() """======== if you are under win 8.1 you must see ..
 ('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative') you can use for example 'clam' ===== """

>>> s.theme_use('clam')

這篇文章已經過時了,以下是您只需一行代碼即可在 Python3 中輕松設置主題的方法:

將此添加到“Tk()”行下方。 例如:

window = Tk() # <--- Main window line

ttk.Style().theme_use('default') # <--- Change default to whichever theme you want to use.

其中 'default' 是默認主題的名稱。 將“默認”更改為您喜歡的任何可用主題。

這是一個帶有屏幕截圖的很好的主題列表:

<--截至 2020年的當前主題-->

https://ttkthemes.readthedocs.io/en/latest/themes.html

上面列表中的某些主題未包含在主 tkinter 下載中。

如果是這種情況,您可以通過運行以下命令輕松安裝默認 ttk 安裝中未包含的主題文件:

python3 -m pip install git+https://github.com/RedFantom/ttkthemes

希望這對你有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM