Skip to content
Related Articles

Related Articles

How to create a pop up message when a button is pressed in Python – Tkinter?
  • Last Updated : 26 Dec, 2020

In this article, we will see how to create a button and how to show a popup message when a button is pressed in Python. Tkinter is a standard Python Package for creating GUI applications. Tkinter may be a set of wrappers that implement the Tk widgets as Python classes. Python, when combined with Tkinter, provides a quick and straightforward way to create GUI applications.

Steps to create a Simple GUI in Python using Tkinter:

  1. Import Tkinter package and all of its modules.
  2. Create a GUI application root window (root = Tk()) and widgets will be inside the main window.
  3. Use mainloop() to call the endless loop of the window. If you forget to call this nothing will appear to the user. The window will await any user interaction till we close it.

Syntax : W = Button(root, options)   

main: Represents the root window.

options:  Its used for widgets. It can be used as key-value pairs separated by commas.



Example 1: 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import everything from tkinter module
from tkinter import *
  
# import messagebox from tkinter module
import tkinter.messagebox
  
# create a tkinter root window
root = tkinter.Tk()
  
# root window title and dimension
root.title("When you press a button the message will pop up")
root.geometry('500x300')
  
# Create a messagebox showinfo
  
def onClick():
    tkinter.messagebox.showinfo("Welcome to GFG.""Hi I'm your message")
  
# Create a Button
button = Button(root, text="Click Me", command=onClick, height=5, width=10)
  
# Set the position of button on the top of window.
button.pack(side='bottom')
root.mainloop()

chevron_right


Output:

Output After clicking “Click me”

Example 2:

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

# import everything from tkinter module
from tkinter import *
  
# import messagebox from tkinter module
import tkinter.messagebox
  
# create a tkinter root window
root = tkinter.Tk()
  
# root window title and dimension
root.title("When you press a any button the message will pop up")
root.geometry('500x300')
  
# Create a messagebox showinfo
  
def East():
    tkinter.messagebox.showinfo("Welcome to GFG", "East Button clicked")
  
def West():
    tkinter.messagebox.showinfo("Welcome to GFG", "West Button clicked")
  
def North():
    tkinter.messagebox.showinfo("Welcome to GFG", "North Button clicked")
  
def South():
    tkinter.messagebox.showinfo("Welcome to GFG", "South Button clicked")
  
# Create a Buttons
  
Button1 = Button(root, text="West", command=West, pady=10)
Button2 = Button(root, text="East", command=East, pady=10)
Button3 = Button(root, text="North", command=North, pady=10)
Button4 = Button(root, text="South", command=South, pady=10)
  
# Set the position of buttons
Button1.pack(side=LEFT)
Button2.pack(side=RIGHT)
Button3.pack(side=TOP)
Button4.pack(side=BOTTOM)
  
root.mainloop()

chevron_right


Output 2:

Output After clicking “North”

Output After clicking “East”


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :