I am not able to disable single radio buttons in my tkinter gui . I can only disable the last created set of buttons, the reason is because the matrix i use to store the handle to the buttons is not stored or returned correctly. It only returns handles to the last created buttons.
This is the code to test:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun May 9 10:02:18 2021
@author: stefaniecg
"""
import tkinter as tk
gui = tk.Tk()
gui.title('title of the app')
gui.geometry("400x200") # (X * Y)
# constants
inc_crit = (('item_0',0,'desc0'),('item_1',1,'desc1'),('item_2',2,'desc2'),('item_3',3,'desc3'))
inc_opt = (('true',0,0),('false',1,1),('maybe',2,2))
# display radio buttons
inc_v = [None] * len(inc_crit)
inc_txt = [None] * len(inc_crit)
rad_btn = [[None]*len(inc_opt)] * len(inc_crit)
for i,[name,row,desc] in enumerate(inc_crit):
inc_v[i] = tk.IntVar(); inc_v[i].set(2)
for j,[optn,val,col] in enumerate(inc_opt):
rad_btn[i][j] = tk.Radiobutton(gui,variable=inc_v[i],value=val)
rad_btn[i][j].grid(row=row,column=col)
inc_txt[i] = tk.Text(gui,width=30,height=1,font=(None,13))
inc_txt[i].grid(row=row,column=3)
inc_txt[i].insert(1.0,desc)
# disable radio buttons
rad_btn[1][2].configure(state='disabled')
rad_btn[2][0].configure(state='disabled')
rad_btn[3][2].configure(state='disabled')
# launch gui
gui.mainloop()
This is the return of the handles of the radio buttons. This is the problem: they all return handle to radiobutton10,11,12 .
In [10]: rad_btn
Out[10]:
[[<tkinter.Radiobutton object .!radiobutton10>,
<tkinter.Radiobutton object .!radiobutton11>,
<tkinter.Radiobutton object .!radiobutton12>],
[<tkinter.Radiobutton object .!radiobutton10>,
<tkinter.Radiobutton object .!radiobutton11>,
<tkinter.Radiobutton object .!radiobutton12>],
[<tkinter.Radiobutton object .!radiobutton10>,
<tkinter.Radiobutton object .!radiobutton11>,
<tkinter.Radiobutton object .!radiobutton12>],
[<tkinter.Radiobutton object .!radiobutton10>,
<tkinter.Radiobutton object .!radiobutton11>,
<tkinter.Radiobutton object .!radiobutton12>]]
so, with every buttons i try to disable: (code below) always the last set of created radio buttons is disabled: ie radio buttons 11,12,13, because of the handles.
# disable radio buttons
rad_btn[1][2].configure(state='disabled')
rad_btn[2][0].configure(state='disabled')
rad_btn[3][2].configure(state='disabled')
Please help:)
It is because the line:
rad_btn = [[None]*len(inc_opt)] * len(inc_crit)
will create a list of lists with all the rows referencing same instance of list ( [None]*len(inc_opt)
).
You need to change it to:
rad_btn = [[None]*len(inc_opt) for _ in inc_crit]
It will create len(inc_crit)
instances of individual list [None]*len(inc_opt)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.