Python Forum
[Tkinter] Default Values for radiobuttons
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Default Values for radiobuttons
#1
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:

1
2
3
4
5
6
7
# 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?
Reply
#2
Please use tags when posting code.
This works for me.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
Thank you for your suggestion and I apologise for not using the tag option. Will do so in the future.

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Settting Default Values for grid gw1500se 6 5,100 Nov-11-2021, 06:44 PM
Last Post: gw1500se
Question Use radiobuttons to determine a total charge SalsaBeanDip 2 2,526 Nov-13-2020, 04:14 AM
Last Post: SalsaBeanDip
  [Tkinter] Connect Toplevel Radiobuttons to root Label/Entry widgets iconit 2 3,438 Apr-28-2020, 06:50 AM
Last Post: iconit
  2 sets of radiobuttons Chuck_Norwich 4 3,782 Dec-02-2019, 07:31 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020