Python TKinter Tutorial How To Create a Button in TKinter

Last updated Dec 20, 2021


The Tkinter button widgets allow us to add buttons to our Python scripts. Tkinter is a graphical user interface (GUI) framework for Python. When a user clicks on a button on a webpage, it displays text or graphics. A function or method is usually attached to a button for it to conduct an on-click action.

 

This article will explore how to add a Tkinter button to your website.

 

Creating a Tkinter Button

To create a Tkinter button, simply follow the steps below:

  • Step 1: Include the Tkinter module in your Python program.

from tkinter import *  

  • Step 2: Now, create and specify the main window's dimensions.

root= TK ()  root.geometry('200x200')

  • Step 3: Next, build a button.

btn = Button (root, text = 'Click here !', bd = '5', command = root.destroy)

  • Step 4: Position your button

btn.pack(side = 'top')

 

An Example of a Tkinter Button

The following is a "sign in" button with dimensions of 300 by 300 and placed on top of the widget.

# Import the entire contents of the TKinter module.

from tkinter import *

# Set up a TKinter window

root = Tk()                            

#display a window with a size of 100x100 pixels.

root.geometry('200x200')

# Design a Button

btn = Button(root, text = 'Click me !', bd = '5', command = root.destroy)

# Adjust the button's position at the top of the window.

btn.pack(side = 'top')

root.mainloop()

 

An Example of a Tkinter Button

The following is a "sign in" button with dimensions of 300 by 300 and placed on top of the widget.

# Import the entire contents of the TKinter module.

from tkinter import *

# Set up a TKinter window

root = Tk()                            

#display a window with a size of 100x100 pixels.

root.geometry('200x200')

# Design a Button

btn = Button(root, text = 'Click me !', bd = '5', command = root.destroy)

# Adjust the button's position at the top of the window.

btn.pack(side = 'top')

root.mainloop()

 

 If you run the code above, the following will be displayed on your screen

Python Tkinter Button

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

21 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions