Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checkbox snippet
#1
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


Reply


Messages In This Thread
Checkbox snippet - by menator01 - May-16-2020, 08:26 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Advanced CLI Snippet Manager bytebutcher 1 2,618 Sep-20-2020, 11:58 AM
Last Post: bytebutcher
  Cute oscillating range generation snippet I saw on irc league55 1 2,778 Mar-26-2018, 04:19 PM
Last Post: nilamo
  snippet: dp and pv Skaperen 0 2,946 Apr-08-2017, 07:17 AM
Last Post: Skaperen
  Password Snippet Kai. 9 8,238 Nov-10-2016, 08:11 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020