nilesh
nilesh

Reputation: 347

How to add entry box together with checkbutton in python tkinter UI

I created a checkbox bar. How to add entry box with each check button and disable entry box if checkbox is disabled?

code for creating checkbox is below

#!/usr/bin/python3
import sys
from tkinter import *
from tkinter import filedialog
import tkinter.messagebox
import os


class Checkbar(Frame):
   def __init__(self, parent=None, picks=[], side=LEFT, anchor=W):
      Frame.__init__(self, parent)
      self.vars = []
      for pick in picks:
         var = IntVar(value=1)
         chk = Checkbutton(self, text=pick, variable=var)
         chk.pack(side=side, anchor=anchor, expand=YES)
         self.vars.append(var)
   def state(self):
       return map((lambda var: var.get()), self.vars)


if __name__ == '__main__':
    root = Tk()
    root.title('Test')
    lng = Checkbar(root, ['ASD', 'BSD', 'CSD'])
    lng.grid(row=0, columnspan=6)

Below is expected UI: enter image description here

Upvotes: 0

Views: 1254

Answers (1)

Henry Yik
Henry Yik

Reputation: 22493

Create Entry widgets in the same for loop and associate them to the Checkbutton.

class Checkbar(Frame):
    def __init__(self, parent=None, picks=[], side=LEFT, anchor=W):
        Frame.__init__(self, parent)
        self.vars = []

        for pick in picks:
            var = IntVar(value=1)
            entry = Entry(self)
            chk = Checkbutton(self, text=pick, variable=var, command=lambda v=var, e=entry: self.show_entry(v,e))
            chk.pack(side=side, anchor=anchor, expand=YES)
            entry.pack(side=side, anchor=anchor, expand=YES)
            self.vars.append(var)

    def show_entry(self, var, widget):
        if var.get() == 0:
            widget.configure(state='disabled')
        else:
            widget.configure(state='normal')

Upvotes: 0

Related Questions