开发者

Only allow user to select one Checkbutton at a time?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜