I have some labels in a frame with checkboxes and I would like to update a counter each time a checkbox is ticked (or unticked). So far I failed to implement it... I would like the output to appear in the text: number of flag1/flag2 selected (/10): with the overal number corresponding to how many flag1/flag2 boxes have been ticked.
I'd like the update to be triggered as soon as a box is checked.
I am adding the code below. Any help would be greatly appreciated! Regards, Pierre.
import tkinter as tk
import webbrowser
class Pierre:
def __init__(self, master, urls, chk_lbl):
self.urls = urls
self.chk_lbl = chk_lbl
self.counter = 0
self.vars = []
i=0
for url in self.urls:
lbl = tk.Label(master, text=url, fg="blue", cursor="hand2")
lbl.grid(row=i, column=1)
lbl.bind("<Button-1>", self.callback)
ic = 2
for lbl in self.chk_lbl:
var = tk.IntVar()
chk = tk.Checkbutton(master, text=lbl, variable=var)
chk.grid(row=i, column=ic)
self.vars.append(var)
ic += 1
i+=1
def state(self):
return map((lambda var: var.get()), self.vars)
def callback(self, event):
webbrowser.open_new(self.url)
class TestClass(tk.Tk):
def __init__(self, **kwargs):
root = tk.Tk.__init__(self, **kwargs)
self.title('Test')
# --- Changes starts from here --- #
def onFrameConfigure(canvas):
canvas.configure(scrollregion=canvas.bbox('all'))
self.topframe = tk.Frame(root)
self.topframe.pack(side=tk.TOP, pady=30)
self.canvas = tk.Canvas(root)
self.bottomframe = tk.Frame(self.canvas)
# self.bottomframe.pack( side = tk.BOTTOM )
self.scrollbar = tk.Scrollbar(root, orient='vertical', command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.pack(side=tk.LEFT, fill='y')
self.scrollbar.pack(side=tk.LEFT, fill='y')
self.canvas.create_window(0, 0, window=self.bottomframe, anchor='nw')
self.bottomframe.bind('<Configure>', lambda event, canvas=self.canvas: onFrameConfigure(self.canvas))
# --- Changes ends here --- #
self.button = tk.Button(self.topframe, text='Click', command=self.output_value)
self.button.pack(side="left", fill="both", expand=True)
iai = 1 # hardcoded value. should be set to the number of time flag1 has been ticked over all labels
ihf = 2 # hardcoded value. should be set to the number of time flag2 has been ticked over all labels
tk.Label(self.topframe, text='number of flag1 selected (/10):%i' % iai).pack()
tk.Label(self.topframe, text='number of flag2 selected (/10):%i' % ihf).pack()
def output_value(self):
urls = ["http://www.google.com", "http://www.facebook.com", "http://bbc.co.uk"]
chk_lbl = ['flag1', 'flag2']
Pierre(self.bottomframe, urls, chk_lbl)
if __name__ == "__main__":
root = TestClass()
root.mainloop()
Aucun commentaire:
Enregistrer un commentaire