from Tkinter import *
from ttk import Style

class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent, background="#333")          
        self.parent = parent        
        self.pack(fill=BOTH, expand=1)

    def centerWindow(self):      
        w = 500
        h = 450

        sw = self.parent.winfo_screenwidth()
        sh = self.parent.winfo_screenheight()

        x = (sw - w)/2
        y = (sh - h)/2
        self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))

    def closeBtn(self):
        '''creating Button to close window''' = Style()"default")
        quitButton = Button(self.parent,text='Close',command=self.parent.destroy),y=50)  

    def initUI(self):

        ''' Operating system selection'''
        os_label = Label(frame1,text="Current OS :",width=10)
        os_label.pack(side=LEFT, padx=5, pady=5)

        '''os =[("Winows",1),
        v = IntVar()
        #v.set(1) Default value i.e. Windows

        '''for txt,val in os:
            radiobtn_os = Radiobutton(frame1,text=txt,variable=v,value=val).pack(side=LEFT, padx=5, pady=5)'''
        radiobtn_os1 = Radiobutton(frame1,text="Winows",variable=v,value=1)
        radiobtn_os1.pack(side=LEFT, padx=5, pady=5)

        radiobtn_os2 = Radiobutton(frame1,text="Linux",variable=v,value=2)
        radiobtn_os2.pack(side=LEFT, padx=5, pady=5)

        radiobtn_os3 = Radiobutton(frame1,text="Mac",variable=v,value=3)
        radiobtn_os3.pack(side=LEFT, padx=5, pady=5)

        '''Drive selection'''

        scan_label = Label(frame2,text="System Scan :",width=11).pack(side=LEFT, padx=5, pady=5)

        sscan =[("Default",1),

        w = IntVar()
        w.set(1) # Default value i.e. Windows

        for txt,val in sscan:
            radiobtn_scan = Radiobutton(frame2,text=txt,variable=w,value=val)
            radiobtn_scan.pack(side=LEFT, padx=5, pady=5)

def main():
    root = Tk()
    app = Example(root)

if __name__ == '__main__':

Line 46:
v = IntVar()

v goes out of scope.

If you bind v to something persistent (self), then it works.

How did you plan to read that variable anyway?

I am very new in this area..So i am not getting what to do.How can i bind it..I am jsut following the syntax only..How to use radio button in tkinter for default selected button?

Edited 1 Month Ago by Pandey_1

Maybe I was not clear enough.

If you give a variable to a control object in tkinter, then the control reads its state from this variable. If you do not keep track of this variable, then you loose the state of the control.

Unfortunately (?) the the control's binding to the variable seems to be weak. So if you loose reference to the control's variable in your code, then the control is loosing it too. So something undefined happens....

So instead of
Line 46:
v = IntVar()

Do this:
and update all occurences.

Edited 1 Month Ago by slate

This question has already been answered. Start a new discussion instead.