Site icon DataFlair

How to use RadioButton in GUI Application in Python

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Program 1

from tkinter import *
class MyRadio:
    def __init__(self,myroot):
        self.mf=Frame(myroot,width=500,height=500,bg='yellow')
        self.mf.pack()
        self.mf.propagate(0)
        self.v=IntVar()
        self.rdm=Radiobutton(self.mf,text="Male",variable=self.v,value=1,font=('Arial,bold,10'),command=self.rdClick)
        self.rdf=Radiobutton(self.mf,text="FeMale",variable=self.v,value=2,font=('Arial,bold,10'),command=self.rdClick)
        self.lb1=Label(self.mf,text="-",font=('Arial,bold,10'))
Self.rdm.place
        Self.rdf.place
        self.lb1.place
def rdClick(self):
       #print(self.v.get())
        if(self.v.get()==1):
            self.lb1["text"]="Male Selected"
            self.mf["bg"]='cyan'
        if(self.v.get()==2):
            self.lb1["text"]="FeMale Selected"
            self.mf["bg"] = 'green'

myroot=Tk()
myroot.geometry('500x500')
myroot.maxsize(500,500)
myroot.minsize(500,500)
myroot.title('Radio button Demo')
myroot.wm_iconbitmap('2.ico')

M1=MyRadio(myroot)
myroot.mainloop()

 

Exit mobile version