英文:
ttk button background not changing color after theme has been set - python tkinter
问题
我正在使用ttk创建一个tkinter GUI,已经下载并导入了Azure主题到我的项目中。然后,我尝试使用样式将一个按钮的颜色更改为红色,但尽管没有出现错误,但这并没有起作用。该按钮仍然保持该主题的默认颜色(它可以响应样式中可以指定的其他选项,如填充,所以我不认为是样式本身的问题)。
这是我的问题示例:
import tkinter as tk
from tkinter import ttk
def myfunc():
print("Button Clicked")
root = tk.Tk()
root.geometry("200x200")
root.tk.call("source", "C:/Users/samue/OneDrive/Desktop/Website folder/Azure-ttk-theme-main/azure.tcl")
root.tk.call("set_theme", "dark")
buttonstyle = ttk.Style()
buttonstyle.configure("custom.TButton", padding=0, background="red")
mybutton = ttk.Button(root, text="Click Me", command=myfunc, style="custom.TButton")
mybutton.pack()
root.mainloop()
抱歉,这不是完全可复制的,您需要下载Azure主题并修改路径以运行它。Azure可以在这里找到:https://github.com/rdbende/Azure-ttk-theme
英文:
I am creating a tkinter GUI using ttk, and I have downloaded and imported the Azure theme for my project. I then tried to change the color of one of the buttons to red using a style but this didn't work despite no errors coming up. The button just remains the default for that theme (it does respond to other options I can specify in the style like padding, so I don't think it's an issue with the style itself.
Here is an example of my problem:
import tkinter as tk
from tkinter import ttk
def myfunc():
print("Button Clicked")
root = tk.Tk()
root.geometry("200x200")
root.tk.call("source", "C:/Users/samue/OneDrive/Desktop/Website folder/Azure-ttk-theme-main/azure.tcl")
root.tk.call("set_theme", "dark")
buttonstyle = ttk.Style()
buttonstyle.configure("custom.TButton", padding=0, background="red")
mybutton = ttk.Button(root, text="Click Me", command=myfunc, style="custom.TButton")
mybutton.pack()
root.mainloop()
Sorry this is not wholly reproducible, you will have to download the azure theme to run it (and modify the path). Azure can be found here: https://github.com/rdbende/Azure-ttk-theme
答案1
得分: 1
from tkinter import ttk
from tkinter import Tk
root = Tk()
style = ttk.Style()
button_1 = ttk.Button(root, text='click me')
style.theme_use('alt')
style.configure('TButton', font=('Helvetica', 14), background='blue', foreground='white')
style.map('TButton', background=[('active', 'green')])
button_1.pack()
root.mainloop()
英文:
Snippet:
from tkinter import ttk
from tkinter import Tk
root = Tk()
style = ttk.Style()
button_1 = ttk.Button(root, text='click me')
style.theme_use('alt')
style.configure('TButton', font=('Helvetica', 14), background='blue', foreground='white')
style.map('TButton', background=[('active', 'green')])
button_1.pack()
root.mainloop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论