How To Add Buttons In The Tkinter

Introduction

 
In this article, I will explain how to add buttons in the tkinter in Python.
 

Definition

 
The Button keyword is used to add buttons in a Python application. The buttons can display text or photos that convey the cause of the buttons. You can attach a function to a button. When you click the button, it will go to the function.
 
Syntax
  1. b = Button (a, option)   

Parameters

 
a − This represents the parent window of the computer.
 
option − The list of most used options for this widget.
 
Option & Description
  • active background - The active background is used to change Background color when the button is under the cursor.
  • active foreground - The active foreground is used to change Foreground color when the button is under the cursor.
  • bd - The bd is used to change the Border width in pixels. Default is 2.
  • bg - The bg is used to change Normal background color background.
  • command - The command is used to call a function or method when the button is clicked 
program 
  1. from tkinter import *    
  2. import tkinter.messagebox    
  3. a= Tk()    
  4.   
  5. def hello():    
  6.    tkinter.messagebox.showinfo( "Hello c#corner")    
  7.     
  8. b =Button(a, text ="Hello",activebackground="black" , activeforeground="white",bg="green",bd=10,command = hello)    
  9. b.pack()   
  10. a.mainloop()     
Output
 
How To Add Buttons In The Tkinter
  • fg - The fg is used to change the normal foreground (text) color.
  • font - The font is used to change the text font to be used for the button's label.
  • height - The height is used to change the height of the button in text lines (ftext buttons) or pixels (images).
  • highlight color - The highlight color is used to change the color of the focus highlight when the widget has focus.
  • image - Image keyword is used to be displayed on the button.
Program
  1. from tkinter import *    
  2. import tkinter.messagebox    
  3. a= Tk()    
  4.     
  5. def hello():    
  6.    tkinter.messagebox.showinfo( "Hello c#corner")    
  7.     
  8. b =Button(a, text ="Hello",fg="yellow",bg="green",font="Castellar",height=10,width=10,relief="solid",command = hello)    
  9. b.pack()    
  10. a.mainloop()     
Output
 
How To Add Buttons In The Tkinter
  • justify: The justify is used to change the arrangement order LEFT,CENTER,RIGHT.
  • padx: The padx is used to change the additional padding left and right of the text.
  • pady: The pady is used to changing the additional padding above and below the text.
  • relief: Relief is used to specify the type of the border. The values are SUNKEN, RAISED, GROOVE, and RIDGE.
  • state: The state is used to set this option to DISABLED to gray out the button and make it unresponsive.
  • underline: The underline is used to change the corresponding text character and will be underlined.
  • width: The width is used to change the width of the button in letters.
Program
  1. from tkinter import *    
  2. import tkinter.messagebox    
  3. a= Tk()    
  4.     
  5. def hello():    
  6.    tkinter.messagebox.showinfo( "Hello c#corner")    
  7.     
  8. b =Button(a, text ="Hello",state=DISABLED,justify=RIGHT,padx=10,pady=10,relief="solid",command = hello)    
  9. b.pack()    
  10. a.mainloop()    
Output
How To Add Buttons In The Tkinter
 

Conclusion

 
In this article, we have seen how to add buttons in the tkinter in python. I hope this article was useful to you. Thanks for reading!


Similar Articles