Home > OS >  setdefault() or self.configure()?
setdefault() or self.configure()?

Time:10-09

I have come across these two ways to create buttons with a default setting in the settings of the button to be created. My question is what advantages or disadvantages does one have over the other? Of course, leaving aside the way the library is imported

First form:

import tkinter as tk


class CalcButton(tk.Button):
    def __init__(self, parent, *args, **kwargs):
        kwargs.setdefault("font", ("Roboto Cn", 14))
        kwargs.setdefault("width", 3)
        kwargs.setdefault("bd", 1)
        kwargs.setdefault("relief", "ridge")
        super().__init__(parent, *args, **kwargs)


root = tk.Tk()
root.title("Calculadora Basica")
mi_frame = tk.Frame(root)
mi_frame.pack()

Second way:

from tkinter import *


class CalcButton(Button):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.config(font=("Roboto Cn", 14))
        self.config(width=3)
        self.config(bd=1)
        self.config(relief="ridge")


root = Tk()
root.title("basic calculator")
my_frame = Frame(root)
my_frame.pack()

CodePudding user response:

The two solutions you provided are not identical in behavior. The first one will establish defaults but let the caller override the defaults, the second one will unconditionally override what was passed in.

So, the advantage of one over the other depends on whether you want to accept the options specified by the user or you want to override them.

  • Related