So,the problem is that I have dozens of buttons with same configuration and i want to shorten it so that I dont need to write same config to each button.
Here is example of code:
self.Button1 = tk.Button(self.ButtonsFrame1)
self.Button1.place(relx=0.664, rely=0.275, height=40, width=94)
self.Button1.configure(activebackground="beige",
activeforeground="#000000",
background="#8c9eef",
compound='left',
foreground="#000000",
highlightbackground="#d9d9d9",
highlightcolor="black",
pady="0",
text='''button1''')
self.Button2 = tk.Button(self.ButtonsFrame2)
self.Button2.place(relx=0.824, rely=0.275, height=40, width=94)
self.Button2.configure(activebackground="beige",
activeforeground="#000000",
background="#8c9eef",
compound='left',
foreground="#000000",
highlightbackground="#d9d9d9",
highlightcolor="black",
pady="0",
text='''button2''')
As you can see many elements of configure() are the same, and I know it can be shorter but I don't have any idea on how to do that. Can you help me?
CodePudding user response:
You can define a Style
and then apply it to your buttons.
Here's some examples from the documentation using Tk themed widgets:
from tkinter import ttk
# change default style for every button
ttk.Style().configure("TButton", padding=6, relief="flat", background="#ccc")
btn = ttk.Button(text="Sample")
Second example:
# override the basic Tk widgets
from tkinter import *
from tkinter.ttk import *
style = Style()
style.configure("my.TButton", activebackground="beige",
activeforeground="#000000",
background="#8c9eef",
compound='left',
foreground="#000000",
highlightbackground="#d9d9d9",
highlightcolor="black",
pady="0")
btn1 = Button(style="my.TButton", text="button1")
btn2 = Button(style="my.TButton", text="button2")