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
program
  1. from tkinter import *
  2. import tkinter.messagebox
  3. a= Tk()
  4. def hello():
  5. tkinter.messagebox.showinfo( "Hello c#corner")
  6. b =Button(a, text ="Hello",activebackground="black" , activeforeground="white",bg="green",bd=10,command = hello)
  7. b.pack()
  8. 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. def hello():
  5. tkinter.messagebox.showinfo( "Hello c#corner")
  6. b =Button(a, text ="Hello",fg="yellow",bg="green",font="Castellar",height=10,width=10,relief="solid",command = hello)
  7. b.pack()
  8. a.mainloop()
Output
How To Add Buttons In The Tkinter
Program
  1. from tkinter import *
  2. import tkinter.messagebox
  3. a= Tk()
  4. def hello():
  5. tkinter.messagebox.showinfo( "Hello c#corner")
  6. b =Button(a, text ="Hello",state=DISABLED,justify=RIGHT,padx=10,pady=10,relief="solid",command = hello)
  7. b.pack()
  8. 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!