In this article, we will look into the Tkinter Messagebox and Radiobutton widgets of Tkinter.
The tkinter package (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. It improves the user interface of Python application as well as provides a good number of widgets that can be used from the inbuilt package.
What is Tkinter messagebox?
A messagebox is an essential component of the application to communicate to the user the status of his/her code. Basically, it lets the coder know the mistakes or successes achieved during the coding.
It provides this functionality with the help of many inbuilt functions. Some of these functions are showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, and askretryignore.
Basics of the Tkinter Messagebox
Firstly, we will import the tkinter module and also the messagebox function in specific.
We will then specify the window(frame) and name it as
root using Tk().
from tkinter import * from tkinter import messagebox root =Tk()
We will then specify the dimensions of the frame by instantiating the
geometry() on the
We will then define a button named
b using the Tkinter Button() widget that displays the message “Click here” and on click on this button triggers the
call_me() function. We will then use the
pack() to place the widget on the geometry of the frame.
def call_me(): messagebox.showinfo("Success","Welcome to our tutorial") b = Button(root,text="Click here",command=call_me) b.pack()
The complete code is as shown below:
from tkinter import * from tkinter import messagebox def call_me(): messagebox.showinfo("Success","Welcome to our tutorial") root =Tk() b = Button(root,text="Click here",command=call_me) b.pack() root.geometry("400x400+120+120") root.mainloop()
The output is as shown. So once you click on the “Click here” button, the alert box named “Success” is displayed with the message “Welcome to our tutorial”. The box vanishes on the click of the “Ok” button.
The radio button is a tkinter widget that is used for the selection of choices among a few given multiple choices. Remember seeing this in the Multiple Choice Questions GUIs?
Why should you use a radiobutton?
- They’re faster for the user since they can make faster choices with one click rather than two. A typical click takes 1.2 to 2.4 seconds.
- They provide better self-documentation of the control without having to click anything.
- For touch interfaces, it’s easier to tap a radio button than to navigate a drop-down list.
- You can provide all the choices together (in terms of accessibility)
Creating Simple Radio button
To create a radio button, we will use the tkinter module of python. Let us take a look at the coding for the same.
from tkinter import * root =Tk() r1=Radiobutton(root, text="Yes", value=1) r2=Radiobutton(root, text="No", value=2) r1.pack() r2.pack() root.geometry("300x300+120+120") root.mainloop()
In the above code snippet, we first import the
tkinter module and initialise a tkinter frame using
Tk(). Within this frame
root we create two radiobuttons using the
Radiobutton() function which takes in parameters such as the name of the frame, text and value. We then use the
pack() function to set this widget into rows and columns.
The output of the above code snippet is as shown below:
That brings us to the end of our tutorial on the Tkinter Messagebox and Radiobuttons. The Tkinter module is vast and we want to make it really easy for you to understand. So keep a watch on the Tkinter tutorials page to learn more!