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.