Python Tkinter MessageBox-Example

by Apr 21, 2020Python Tkinter

INTRODUCTION

In this article, we will learn “How to interface the radio button and messages with Tkinter”. I recommend you to go through this first similarly if you are new to Tkinter go through this.

Here we will design a GUI with a 4 radio button and display the messages for each option. So I have decided the look of the GUI as given below.

In this, we will have 4 radio buttons. and we will get the answer from the user and a button to trigger the action. We can do this with simple knowledge in Tkinter. We will learn it in this article. Here is the code for the following.

from tkinter import *
import tkinter.messagebox as tmsg

root = Tk()
root.geometry("455x233")
root.title("Radiobutton ")

def order():


    if(var.get() == 1):
        {
            tmsg.showinfo("answer", "correct!!!")
        }
        t=StringVar()
    else:
     {

        tmsg.showerror("answer", "wrong the correct answer is option1")

    }

    value = tmsg.askquestion("Was your experience Good?", "You used this gui.. Was your experience Good?")
    if value == "yes":
        msg = "Great. comment us on IoT4Beginners please"
    else:
        msg = "Tell us what went wrong. We will call you soon"
    tmsg.showinfo("Experience", msg)

var = IntVar()
var.set("Radio")
Label(root, text = "Which of the following is correct?",font="lucida 19 bold",justify=LEFT, padx=14).pack()
radio = Radiobutton(root, text="Variable name can start with an underscore.", padx=14, variable=var, value=1).pack(anchor="w")
radio = Radiobutton(root, text="Variable name can start with a digit.", padx=14, variable=var, value=2).pack(anchor="w")
radio = Radiobutton(root, text="Keywords cannot be used as a variable name.", padx=14, variable=var, value=3).pack(anchor="w")
radio = Radiobutton(root, text="Variable name can have symbols like: @, #, $ etc.", padx=14, variable=var, value=4).pack(anchor="w")

Button(text="submit", command=order).pack()
root.mainloop()


STEPS WITH EXPLANATION: 

1. Importing the packages


from tkinter import *
import tkinter.messagebox as tmsg

Here, we’ve imported two packages, for Tkinter and message box for display message for each option choices.

2. Creating a window for the GUI  

root = Tk()
root.geometry("455x233")
root.title("Radiobutton ")

3.Adding widgets to the GUI  

var = IntVar()
var.set("Radio")
Label(root, text = "Which of the following is correct?",font="lucida 19 bold",justify=LEFT, padx=14).pack()
radio = Radiobutton(root, text="Variable name can start with an underscore.", padx=14, variable=var, value=1).pack(anchor="w")
radio = Radiobutton(root, text="Variable name can start with a digit.", padx=14, variable=var, value=2).pack(anchor="w")
radio = Radiobutton(root, text="Keywords cannot be used as a variable name.", padx=14, variable=var, value=3).pack(anchor="w")
radio = Radiobutton(root, text="Variable name can have symbols like: @, #, $ etc.", padx=14, variable=var, value=4).pack(anchor="w")

Button(text="submit", command=order).pack()

A radio button is a graphical user interface element of Tkinter, which allows the user to choose (exactly) one of a predefined set of options. Similarly, Radio buttons can contain text or images. The button can only display text in a single font. A Python function or method can be associated with a radio button. This function or method will be called if you press this radio button.

4. Defining a function (order) to display the message

def order():
if(var.get() == 1):
{
tmsg.showinfo("answer", "correct!!!")
}
t=StringVar()
else:
{

tmsg.showerror("answer", "wrong the correct answer is option1")

}

value = tmsg.askquestion("Was your experience Good?", "You used this gui.. Was your experience Good?")
if value == "yes":
msg = "Great. comment us on IoT4Beginners please"
else:
msg = "Tell us what went wrong. We will call you soon"
tmsg.showinfo("Experience", msg)
  • First we will get an option value(i.e option 1 means var=1,similarly option 2 means var =2 and so on )
  • according to the question the first option is the correct answer.
  • So if the user selects option 1 then we need to display correct answer and wrong for the rest of the choose   

5.Types of messages

Tkinter provides a message box class which that can be used to show a variety of messages so that the user can respond according to those messages. Messages like confirmation message, error message, warning, message, etc.

SHOWINFO
tmsg.showinfo("answer", "correct!!!")# Show an info message.
OUTPUT:
SHOWERROR

tmsg.showerror("answer", "wrong the correct answer is option1")# Show an error message.

OUTPUT:

ASKQUESTION
value = tmsg.askquestion("Was your experience Good?", "You used this gui.. Was your experience Good?")
if value == "yes":
    msg = "Great. comment us on IoT4Beginners please"
else:
    msg = "Tell us what went wrong. We will call you soon"
tmsg.showinfo("Experience", msg)
OUTPUT:

If we choose yes the output is as shown below

Likewise if we choose no then the output is as shown below

The output of the following code.

6.OUTPUT

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

Data And Classes in Python Tkinter

Data And Classes in Python Tkinter

In this article, we are going to learn about data and classes in python Tkinter. We will save our GUIdata into Tkinter variables after that we will also start using OOP to extend the existing Tkinter classes to extend the built-in functionality of Tkinter....

asksaveasfile() function in Python-Tkinter

asksaveasfile() function in Python-Tkinter

In this tutorial, we will be learning how to use the asksaveasfile() in Tkinter and save the file in the system. The asksaveasfile() function comes under the class filedialog. The file dialog module provides functions for creating file/directory selection...

Graphical User Interface with tkinter python

Graphical User Interface with tkinter python

In this article, we will tell about the GUI(graphical user interface) in python using. We should also know about how to import the library and using some widgets in it. Let’s have a quick look at the below contents. Contents IntroductionWhat is Tkinter?Packaging...

Plotting graphs with Matplotlib (Python)

Plotting graphs with Matplotlib (Python)

In this tutorial, you'll learn about matplotlib, the plotting library of Python. You'll also get to learn how do we use it for plotting different types of graphs in Python. What is Matplotlib? Matplotlib is an amazing visualization library in Python for 2D plots of...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....