开发者

Only allow user to select one Checkbutton at a time?

开发者 https://www.devze.com 2023-01-23 15:38 出处:网络
Is there a way to only开发者_运维技巧 allow a user to check one Checkbutton widget at a time out of a set of Checkbutton widgets? I can imagine some brute-force solutions, but I\'m looking for somethi

Is there a way to only开发者_运维技巧 allow a user to check one Checkbutton widget at a time out of a set of Checkbutton widgets? I can imagine some brute-force solutions, but I'm looking for something elegant.


You can tie all the checkbutton to single variable with different onvalue.

import tkinter

root = tk.Tk()            #Creating the root window
var = tk.IntVar()         #Creating a variable which will track the selected checkbutton
cb = []                   #Empty list which is going to hold all the checkbutton
for i in range(5):
    cb.append(tk.Checkbutton(root, onvalue = i, variable = var))  
                          #Creating and adding checkbutton to list
    cb[i].pack()          #packing the checkbutton

root.mainloop()           #running the main loop

I have created them in a loop for demonstration purpose. Even when you create them sequentially you can use the same variable name with different onvalue.

0

精彩评论

暂无评论...
验证码 换一张
取 消