[Python] Default selection of radio button in tkinter

Discussão em 'Python' iniciado por Stack, Julho 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have the code below. The intended behaviour is to set the default option in a set of radio buttons. I've used very similar code elsewhere and its worked but in this case the last 3 options are all showing as selected. I'm new to tkinter and can't for the life of me see where I've gone wrong. Any help much appreciated.

    charttypevar=StringVar()

    chart_type_1 = ttk.Radiobutton(chart_type_frame, text="3d with catagories",variable=charttypevar,value='3d with catagories')#
    chart_type_2 = ttk.Radiobutton(chart_type_frame, text="3d no catagories",variable=charttypevar,value='3d no catagories')#
    chart_type_3 = ttk.Radiobutton(chart_type_frame, text="3d relief plot", variable=charttypevar, value='3d relief plot') #
    chart_type_4=ttk.Radiobutton(chart_type_frame,text="Not yet defined",variable=charttypevar,value="Not yet defined")
    charttypevar.set('Not yet defined') # intended to be set as default

    chart_type_1.grid(column=0,row=1,sticky="W")
    chart_type_2.grid(column=0,row=2,sticky="W")
    chart_type_3.grid(column=0,row=3,sticky="W")
    chart_type_4.grid(column=0,row=4,sticky="W")

    Continue reading...

Compartilhe esta Página