Python 3 Radio Buttons GUI Program

By | March 24, 2020

Topic: Python 3  Radio Buttons GUI Program using  tkinter

Python 3 GUI Program tkinter Radio Buttons

from tkinter import *  
  
def selection():  
   choice = "You selected the option " + str(radio.get())
   if   radio.get()== 1:
           choice = choice + " - Python"
   elif  radio.get()== 2:
           choice = choice + " - Java"
   elif  radio.get()== 3:
           choice = choice + " - C++"
   label.config(text = choice)  
  
window = Tk()  
window.geometry("400x200")  
radio = IntVar()  
lbl = Label(text = "Favourite programming language:", bg='lightgreen',
            font=('verdana',12))  
lbl.pack()  
radio1 = Radiobutton(window, text="Python", variable=radio, value=1,  
                  command=selection)  
radio1.pack( anchor = W )  
  
radio2 = Radiobutton(window, text="Java", variable=radio, value=2,  
                  command=selection)  
radio2.pack( anchor = W )  
  
radio3 = Radiobutton(window, text="C++", variable=radio, value=3,  
                  command=selection)  
radio3.pack( anchor = W)  
  
label = Label(window, text='', bg='green', fg='white', font=('verdana',10))  
label.pack()  
window.mainloop()  

How this Python GUI program works?

Python 3 Radio Buttons

  • Import * from tkinter GUI module.
  • Define a selection function
  • The selection functions uses if-elif statement to select a block of statements to execute on the basis of the corresponding radio buttion selection
  • Create main window
  • set size of  main window 400 x 200
  • Set ttle of the main window
  • Create 3 radio buttons.
  • Create and Place a label in main window
  • Call the mainloop() to display the GUI and start the program.
  • The main window of the program will show and wait for any action by the user
  • As soon as the user selects a radio button, the corresponding text will be shown on the label widget.

You will also like:

Loading

3 thoughts on “Python 3 Radio Buttons GUI Program

  1. Pingback: Multiplication Table Python GUI Program | EasyCodeBook.com

  2. Pingback: Find Factorial by Recursive Function Python GUI Program | EasyCodeBook.com

  3. Pingback: Python 3 tkinter Message Widget Program Examples | EasyCodeBook.com

Leave a Reply

Your email address will not be published. Required fields are marked *