bt1=tk.Button(my_w,text='I am a Button',bg='lightgreen',command=lambda:my_upd())
In above code while creating the above button, we have used some attributes and assigned values to it.
text
: String to be written over the Buttonbg
: background colour of the Buttoncommand
: On Click event handling when the button is clicked['activebackground', 'activeforeground', 'anchor', 'background',
'bd', 'bg', 'bitmap', 'borderwidth', 'command', 'compound', 'cursor', 'default',
'disabledforeground', 'fg', 'font', 'foreground', 'height', 'highlightbackground',
'highlightcolor', 'highlightthickness', 'image', 'justify', 'overrelief',
'padx', 'pady', 'relief', 'repeatdelay', 'repeatinterval', 'state',
'takefocus', 'text', 'textvariable', 'underline', 'width', 'wraplength']
button.configure(text="Click me!")
We can also set multiple attributes at once by using attribute names and values to the `configure()` method. For example, the following code sets the `text`, `background`, and `foreground` attributes of a `Button` widget:
button.configure(text="Click me!", background="red", foreground="white")
Or we can create a dictionary of our attribute with values and use the same.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x150")
set1={'fg':'red','font':['Arial',18,'bold'],
'text':'King & Queen','bg':'lightgreen'} #set of attributes with values
button = tk.Button(my_w, text="Hello, world!")
button.grid(row=0,column=1,padx=30,pady=25)
button.config(set1) # Update the attributes
my_w.mainloop()
print(button.config().keys())
The output is here .
dict_keys(['activebackground', 'activeforeground', 'anchor',
'background', 'bd', 'bg', 'bitmap', 'borderwidth', 'command', 'compound',
'cursor', 'default', 'disabledforeground', 'fg', 'font', 'foreground',
'height', 'highlightbackground', 'highlightcolor', 'highlightthickness',
'image', 'justify', 'overrelief', 'padx', 'pady', 'relief', 'repeatdelay',
'repeatinterval', 'state', 'takefocus', 'text', 'textvariable', 'underline',
'width', 'wraplength'])
print(button1['width']) # value of the option = 20
using cget()
print(button.cget('bg')) # Use the key or the attribute to get value
for options in button1.config():
print(options + ": " + str(btutton1[options]))
Output ( more are there ... )
activebackground: SystemButtonFace
activeforeground: SystemButtonText
anchor: center
background: green
bd: 2
bg: green
----
----
Listing of all attributes of parent window, here my_w is the root or master window.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x150")
for options in my_w.config():
print(options + ": " + str(my_w[options]))
my_w.mainloop()
if "bitmap" in l2.config():
#if "bitmap" in sb1.config():
print("Attribute is available")
else:
print("Attribute is Not available")
state
attribute of a button we can Enable ( normal ) or Disable the button. state
attribute can take three values: active, disabled, or normal
import tkinter as tk
my_w=tk.Tk()
my_w.geometry('350x150')
my_w.title('www.plus2net.com')
def my_upd(): # triggered once the button is clicked
if bt2['state']=='disabled':
bt2['state']='normal' # Enable the button
bt1['text']='Disable' # update the text
else:
bt2['state']='disabled' # Disable the button
bt1['text']='Enable' # Update the text
bt1=tk.Button(my_w,text='Enable',font=22,bg='lightyellow',
command=lambda:my_upd())
bt1.grid(row=0,column=0,padx=50,pady=20)
bt2=tk.Button(my_w,text='Submit',font=22,state='disabled',
activebackground='lightgreen')
bt2.grid(row=0,column=1,padx=20,pady=20)
my_w.mainloop()
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("450x150")
students={1:'King',2:'Queen',3:'Jack',4:'Ron',
5:'Alex',6:'Ravi',7:'Mike',8:'Teek',9:'Pink',10:'Geek'}
set1={'fg':'red','bg':'lightgreen'} #set of attributes with values
col=0 # starting value of column
for j in students:
e = tk.Button(my_w, text=students[j])
e.grid(row=1,column=col,padx=4,pady=20)
if(j % 3==0): # divisible by 3
e.config(set1) # update the attributes
col=col+1 # increase the column value
my_w.mainloop()
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("400x200") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
def my_upd():
#str1=bt1.cget('text') # reading text
str1=bt1['text'] # reading text
#print(str1)
if str1=='I am a button':
#bt1['text']='Welcome' # setting value
bt1.config(text='Welcome') # setting value
bt2.config(state='disable',bg='blue')
else:
#bt1['text']='I am a button'
bt1.config(text='I am a button')
bt2.config(state='normal',bg='lightblue')
bt1=tk.Button(my_w,text='I am a button',background='lightgreen',
command=lambda:my_upd(),font=18,fg='red',width=15)
bt1.grid(row=0,column=0,padx=25,pady=50)
bt2=tk.Button(my_w,text='plus2net',background='lightblue',font=18,width=15)
bt2.grid(row=0,column=1,padx=25,pady=50)
my_w.mainloop() # Keep the window open
t1 = tk.Button(my_w, width=10,height=1,overrelief='groove')
Food Menu system using Checkbutton indicatoron attribute
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("250x150") # width and height of window
def config_all(widgets, **options):
for widget in widgets:
widget.config(**options)
# Create multiple widgets
button1 = tk.Button(my_w, text="First one")
button1.grid(row=0,column=0,padx=5,pady=25)
button2 = tk.Button(my_w, text="I am 2nd")
button2.grid(row=0,column=1,padx=5,pady=25)
label3 = tk.Button(my_w, text="I am 3rd")
label3.grid(row=0,column=2,padx=5,pady=25)
# Configure multiple widgets simultaneously
config_all([button1, button2, label3], fg="blue", bg="yellow",font=22)
my_w.mainloop()
.config()
or .configure()
methods.button = tk.Button(root, text="Click Me", bg="blue", fg="white")
.winfo_x()
and .winfo_y()
are attributes related to a widget’s position, while options are only concerned with appearance and behavior.Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.