I have some ttk buttons but they're inside a function and whenever I try to style it it doesn't work but when I create a new file and use same lines but without putting it inside functions it works just fine. Here's the snippet of code.
def function():
if Something == Another thing:
r = Tk() # Opens new window
r.title('Lorem ipsum')
s = ttk.Style()
s.configure('TButton', font=('Helvetica', 18))
Button = ttk.Button(r, text = "lorem ipsum dolor sit amet",command = lorem ipsum,style="TButton")
Label = ttk.Label(r, text = "Get total Stores Values and quantities")
Label.place(relx = 0.2, rely= 0.4,anchor=CENTER)
Button.place(relx = 0.5, rely= 0.4 ,width= 500 ,height = 50 ,anchor=CENTER)
Thanks and hope it's clear enough.
Since there are multiple Tk()
instances, you need to specify which instance the style belongs to:
from tkinter import *
from tkinter import ttk
def function():
if True:
r = Tk() # Opens new window
r.geometry('600x400')
r.title('Lorem ipsum')
s = ttk.Style(r) # should specify which Tk instance
s.configure('TButton', font=('Helvetica', 18))
Button = ttk.Button(r, text="lorem ipsum dolor sit amet", style="TButton")
Label = ttk.Label(r, text="Get total Stores Values and quantities")
Label.place(relx=0.5, rely=0.4, anchor=CENTER)
Button.place(relx=0.5, rely=0.6, width=500, height=50, anchor=CENTER)
root = Tk()
function()
root.mainloop()
Note that you used TButton
as the style name which affects all the ttk.Button()
(actually you can remove style="TButton"
). Better to use other name like Custom.TButton
if you want to apply the style to particular widgets only.
Avoid using multiple Tk()
instances. Use Toplevel()
if you can.