May-16-2020, 08:26 AM
A tkinter checkbox snippet I wrote and am playing around with. Maybe it will be useful for someone learning tkinter checkboxes and passing their values.
#! /usr/bin/env python3 # Do the imports import tkinter as tk from functools import partial # Create the class class CheckBoxes: def __init__(self, master): self.master = master self.master.columnconfigure(0, weight=1) # Build the frames self.mainframe = tk.Frame(self.master) self.mainframe.grid(column=0, row=0, sticky='news') self.mainframe.grid_columnconfigure(0, weight=3) self.chkbox_frame = tk.Frame(self.mainframe, border=5, relief='ridge') self.chkbox_frame.grid(column=0, row=0, sticky='new') self.chkbox_frame.grid_columnconfigure(0, weight=3) self.textframe = tk.Frame(self.mainframe, border=5, relief='ridge', height=73) self.textframe.grid(column=0, row=1, sticky='new') self.btnframe = tk.Frame(self.mainframe, border=5, relief='ridge') self.btnframe.grid(column=0, row=2, sticky='new') # Set a list variable self.myvars = [] # Initiate the chkbox function/method self.chkboxes() self.btnbox() # The chkbox function def chkboxes(self): # Set a list variable self.boxes = [] # How many checkboxes do we want how_many = 9 for i in range(how_many): # append to the boxes list. Added 1 to i so # the start number would be 1 self.boxes.append(f'Checkbox {i+1}') #Set some loop variables i = 0 j = 0 # Create our checkboxes for self.box in self.boxes: myvar = tk.StringVar() self.chkbox = tk.Checkbutton(self.chkbox_frame, text=self.box, variable=myvar, onvalue='Checked', offvalue='Un-Checked') self.chkbox.deselect() self.chkbox.grid(column=i, row=j, pady=8, padx=8) self.chkbox.grid_columnconfigure(0, weight=3) self.chkbox.grid_rowconfigure(0, weight=3) # This will tell how many checkboxes to display in the row # before starting a new one. The first i is = 0, meaning # that 2 will be the third check box if i >= 2: # increase the row by 1 and reset i to 0 j += 1 i = 0 # i hasn't reached our limit yet so continue else: i += 1 # Append checkbox values to our myvars list self.myvars.append(myvar) # Create a frame to display the passed values def print_frame(self, myvars): self.textframe.destroy() self.textframe = tk.Frame(self.mainframe, border=5, relief='ridge', height=73) self.textframe.grid(column=0, row=1, sticky='new') # Set a llop variable to 0 i = 0 # Loop the checkbox values and display for myvar in myvars: label = tk.Label(self.textframe, text=f'{self.boxes[i]} -> {myvar.get()}') label.grid(column=0, row=i, sticky='w') i += 1 # Create the buttons def btnbox(self): self.button = tk.Button(self.btnframe, text='Click Me', \ command=partial(self.print_frame, self.myvars)) self.button.grid(column=0, row=0) self.myvars='' self.clear_btn = tk.Button(self.btnframe, text='Clear', \ command=partial(self.print_frame, self.myvars)) self.clear_btn.grid(column=1, row=0, padx=20) def main(): root = tk.Tk() root.title('Checkboxes') CheckBoxes(root) root.mainloop() if __name__ == '__main__': main()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts