-1

I am New to use Tkinter.I want to create multiple checkbox from loop.I refer Tkinter checkboxes created in loop but i don't understand it. I want to display all files as a checkbox located in a directory.

Help me and tell me what i need to change ?

Code:

from tkinter import filedialog,Checkbutton

import tkinter,os

window = tkinter.Tk()

def browse():

    filez = filedialog.askdirectory(parent=window,title='Choose a file')#I choose a directory

    ent1.insert(20,filez)#insert the path of directory to text box
    dirs = os.listdir(filez)#gives all files of direcory
    for file in dirs:
       print(file)#Getting all files
       var = tkinter.IntVar()
       c = tkinter.Checkbutton(window,text=file,variable=var)#Create files to checkox
       c.place(x=0,y=100)



window.title("First Gui")

window.geometry("400x400")
window.wm_iconbitmap("path of icon")

lbl = tkinter.Label(window,text="path")

lbl.place(x=0,y=60)

ent1 = tkinter.Entry(window)

ent1.place(x=80,y=60)

btn1 = tkinter.Button(window,text="Set Path",command=browse)

btn1.place(x=210,y=57)


window.mainloop()

After click on button set path i want to to display all files of directory as a checkbox using browse function

sachin dubey
  • 755
  • 9
  • 28
  • you put all checkboxes in one place `c.place(x=0,y=100)` so you can't see them. Better use `pack()` or `grid()` - they are more elastick. – furas Nov 20 '17 at 06:39

1 Answers1

1

I see three problems

  1. you use c.place(x=0,y=100) for all Checkbuttons so you can see only last one - other are hidden behind last one.

  2. every Checkbutton need own IntVar which you can keep on list or in dictionary.

  3. when you select new path then you have to remove previous Checkbuttons so you have to remember them on list or in dictionary.

Example shows how you could use pack() instead of place() to easily put all Checkbuttons. It also show how to uses dictionary to keep IntVars and check which was selected, and how to use list to keep Checkbuttons and remove them later from window.

import tkinter
import tkinter.filedialog
import os

# --- functions ---

def browse():

    filez = tkinter.filedialog.askdirectory(parent=window, title='Choose a file')

    ent1.insert(20, filez)

    dirs = os.listdir(filez)

    # remove previous IntVars
    intvar_dict.clear()

    # remove previous Checkboxes
    for cb in checkbutton_list:
        cb.destroy()
    checkbutton_list.clear() 

    for filename in dirs:
        # create IntVar for filename and keep in dictionary
        intvar_dict[filename] = tkinter.IntVar()

        # create Checkbutton for filename and keep on list
        c = tkinter.Checkbutton(window, text=filename, variable=intvar_dict[filename])
        c.pack()
        checkbutton_list.append(c)

def test():
    for key, value in intvar_dict.items():
        if value.get() > 0:
            print('selected:', key)

# --- main ---

# to keep all IntVars for all filenames
intvar_dict = {}
 # to keep all Checkbuttons for all filenames
checkbutton_list = []

window = tkinter.Tk()

lbl = tkinter.Label(window, text="Path")
lbl.pack()

ent1 = tkinter.Entry(window)
ent1.pack()

btn1 = tkinter.Button(window, text="Select Path", command=browse)
btn1.pack()

btn1 = tkinter.Button(window, text="Test Checkboxes", command=test)
btn1.pack()

window.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thanks . I got files but it is going down to the screen .how to add scroll bar ? – sachin dubey Nov 20 '17 at 07:35
  • it is easy to add scrollbar to [Listbox](http://effbot.org/tkinterbook/listbox.htm) but not to group of widgets. The only solution is to put `Checkbuttons` in `Frame`, `Frame` put on `Canvas` and you can scroll `Canvas`. You can find some examples. Yesterday was event question on SO – furas Nov 20 '17 at 07:36
  • I put example with Scrolled Frame on GitHub [scrolled-frame.py](https://github.com/furas/python-examples/blob/master/tkinter/scrolled-frame-canvas/scrolled-frame.py) – furas Nov 20 '17 at 08:49