Python Forum

Full Version: Default Values for radiobuttons
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am learning to use tkinter and I have a program which includes Radiobuttons and Entry. These work well if I press buttons and insert text into the appropriate widgets each time I start the program.

I would like to alter the program to use default values at startup so that then all I have to do is press continue if I want to stay with the defaults. From documentation I understood that this can be done using .invoke() for the radiobuttons but when I run this i get the following error at this strip of code:

   # radio button
    button_var = IntVar()
    radio_1 = Radiobutton(
        text="Windows", variable=button_var, value=1).place(x=15, y=338)
    radio_2 = Radiobutton(text="Linux", variable=button_var,
                          value=2).place(x=115, y=338)
    radio_2.invoke()
Error:
>>> AttributeError: 'NoneType' object has no attribute 'invoke'
My intention if for the program to default to the "Linux" option.

Can anyone help please?
Please use tags when posting code.
This works for me.

move the .place to radio_1.place() and radio_2.place()

import tkinter as tk

root = tk.Tk()

var = tk.IntVar()
r1 = tk.Radiobutton(root, text='Option 1', variable=var, value=1)
r1.pack(anchor='w')

r2 = tk.Radiobutton(root, text='Option 2', variable=var, value=2)
r2.pack(anchor='w')
r2.invoke()

r3 = tk.Radiobutton(root, text='Option 3', variable=var, value=3)
r3.pack(anchor='w')



root.mainloop()
Thank you for your suggestion and I apologise for not using the tag option. Will do so in the future.

Moving the
.place()
to its own line as you suggest worked perfectly