Hi,
I was trying to use Checkbutton widget from Tkinter and I searched here in the forum for example code how to apply it, and I couldnt find any. So I have to test it myself using some documentation of Tkinter.
Since I couldnt find any relevant code in this forum, I want to share what I learned how to use Checkbutton.
# I put the result in a list
import tkinter as tk
root = tk.Tk()
def choose():
# for value (1&2) just put in what you need for it
# for example, I just put a string '101' and '102' in value1 & 2
# the control variable cb(n)_v would hold 1 if its checked and 0 if not
result = []
if cb1_v == 1:
result.append(value1)
if cb2_v == 1:
result.append(value2)
return result
# each checkbutton have to use different control variable
# otherwise when you checked one, all other checkbutton will be checked too
cb1_v = tk.IntVar()
cb2_v = tk.IntVar()
cb1 = tk.Checkbutton(root, text='cb1', variable=cb1_v)
cb2 = tk.Checkbutton(root, text='cb2', variable=cb2_v)
btn1 = tk.Button(root, text='Choose',command=choose)
cb1.grid(row=0,column=0)
cb2.grid(row=1, column=0)
btn1.grid(row=2, column=0)
root.mainloop()
I hope this would help others!
If anyone else with more Tkinter experience know some other tips, tricks or techniques, I would really appreciate if you share the knowledge since I'm also new with Python and Tkinter. Thanks.