Python - How do I add a theme from ttkthemes package to a guizero application?

给你一囗甜甜゛ 提交于 2021-01-28 08:12:44

问题


I'm working on multiple guizero projects and I'm trying to add a theme from the Python package ttkthemes (arc to be exact). I have tried to add the theme to the app widget with the following code:

from guizero import App, Text, PushButton
from ttkthemes import ThemedStyle
import tkinter.ttk as ttk

app = App(title="App")

style = ThemedStyle(app)
style.set_theme("arc")

text = Text(app, text="Text")
button = PushButton(app, text="Button")

app.display()

And it doesn't show the theme

This is what is supposed to look like before the theme

And this is what it looks like with a different theme plastik.

I think I am doing something wrong. So how do I properly add a theme to a guizero app. Thanks.


回答1:


You are not doing anything wrong. The reason why the theme does not change your guizero app is that guizero widgets are based on the basic tkinter widgets, while the theme only applies to ttk widgets.

If you want to use ttk themes, you will need to drop guizero and use ttk widgets:

from ttkthemes import ThemedStyle
import tkinter as tk
from tkinter import ttk

app = tk.Tk()
app.title('App')

style = ThemedStyle(app)
style.set_theme("arc")

tktext = tk.Label(app, text=" tk Label")
tktext.pack()
tkbutton = tk.Button(app, text="tk Button")
tkbutton.pack()

text = ttk.Label(app, text=" ttk Label")
text.pack()
button = ttk.Button(app, text="ttk Button")
button.pack()

app.geometry('200x200')

app.mainloop()

Result: with theme 'arc'

and with theme 'plastik':



来源:https://stackoverflow.com/questions/51697858/python-how-do-i-add-a-theme-from-ttkthemes-package-to-a-guizero-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!