Python Forum
Tkinter checkbox value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter checkbox value
#1
How do I get the current value of a checkbutton. So if someone checked a box, then how to get the value. Is the value either 1 or 0?

Example:

var5 = IntVar()
    checksmoke = Checkbutton(checkin, text="Smoker", bg="yellow", command=selectsmoke, variable=var5)
    checksmoke.place(x=105, y=420)
Reply
#2
The best way to get values from any of the Tk widgets is binding the widget to a variable and using the variable to get and set the widget value.
Reply
#3
Could you give me an example on how to do that?
Reply
#4
Some tutorials
https://www.geeksforgeeks.org/python-set...-variable/
https://www.delftstack.com/tutorial/tkin...eckbutton/
https://www.python-course.eu/tkinter_checkboxes.php
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
When I click the check button, the var value is always 0. Why is that?
Reply
#6
Here is something I been playing around with.
#! /usr/bin/env python3.8
'''Docstring'''

import tkinter as tk
from functools import partial

class CheckBoxes:
    '''Docstring'''

    def __init__(self, master):
        self.master = master
        self.master.columnconfigure(0, weight=1)

        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.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')

        self.myvars = []

        self.chkboxes()
        self.btnbox()

    def chkboxes(self):
        self.boxes = ['Checkbox 1', 'Checkbox 2', 'Checkbox 3']
        i = 0
        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=0, row=i)
            i += 1
            self.myvars.append(myvar)

    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')

        i = 0
        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

    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')
    root.geometry('400x200+50+50')
    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


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] choose checkbox devilonline 1 1,282 Feb-17-2023, 01:23 PM
Last Post: Axel_Erfurt
  [Tkinter] Dynamic checkbox treeview issue OogieM 8 4,905 Mar-20-2022, 02:10 PM
Last Post: OogieM
  tkinter change the text of the checkbox zazas321 1 3,826 Sep-17-2021, 06:19 AM
Last Post: zazas321
  How to get the value of a checkbox scratchmyhead 4 3,033 May-14-2020, 02:56 PM
Last Post: scratchmyhead
  TreeviewWith CheckBox issac_n 1 7,739 Mar-08-2020, 06:51 AM
Last Post: shamnadinn
  Tkinter Checkbox niro_one 1 2,339 Jan-13-2020, 11:31 AM
Last Post: joe_momma
  [Tkinter] Unable to create checkbox and select at run time tej7gandhi 5 4,635 May-05-2019, 04:57 PM
Last Post: tej7gandhi
  [PyQt] PyQt4 handle dynamic checkbox click littleGreenDude 1 6,578 Dec-27-2018, 09:17 PM
Last Post: littleGreenDude
  [Tkinter] Completing Action when CheckBox is Checked Anysja 2 7,963 Aug-02-2018, 04:38 PM
Last Post: Anysja
  Displaying error in QTableview and with Checkbox in Column under macOsX nieselfriem 1 5,434 Mar-19-2017, 10:43 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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