list file names from a folder to a tkinter window, with python 3

Wanek T

i have the following problem: i would like to list all filenames from a folder to a tkinter window + a checkbox (with a unique variable) near each filename. so far i have this:

import tkinter as tk

def gui():
    master=tk.Tk()
    files=next(os.walk('forms'))[2]
    i=1
    for f in files:
        'unique var name??'=tk.IntVar()
        tk.Checkbutton(master, text=f, variable='unique var name??').grid(row=i)
    master.mainloop()


gui()

this code works, but returns only the last file name from the respective folder + a checkbox in the tkinter window. i don't know how to define a unique tk.IntVar() variable for every checkbox and how to make the master.mainloop() window list all file names. i use python 3.4 on win 7.

thank you in advance!

Bryan Oakley

You don't need unique variable names, you only need unique variables. A list or dictionary works great. Since you're associating the variables with filenames, a dictionary with the filename as a key makes sense:

vars = {}
for f in files:
    var = tk.IntVar()
    tk.Checkbutton(master, text=f, variable=var).grid(row=i)
    vars[f] = var

Later, to print the value of all the variables, just iterate over the dictionary:

for (name, var) in vars.iteritems():
    print(name, var.get())

BTW: you have a bug in your code, in that you never increment the row number. You end up stacking all of the buttons on top of each other in the same row. You need to add something like i += 1 inside your loop.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

list file names from a folder to a tkinter window, with python 3

From Dev

How to delete files from a folder using a list of file names in windows?

From Dev

Batch Files: List file names and folder names

From Dev

Rename file names in a folder - Python

From Dev

Python 3 | Closing Window on Tkinter

From Dev

Python, string slicing (getting file names from a list of file locations)

From Dev

Read nested folder and file names as nested list

From Dev

Pythonic way to create a list of strings from file names (without file extension) in folder?

From Dev

Recursively list files with file names, folder names and permission

From Dev

Selecting file names from a folder in php

From Dev

Batch For Loop - removing file from folder names

From Dev

Getting folder names from text file

From Dev

Get a list of file names from HDFS using python

From Dev

Extract substring from list of file names in Python or R

From Dev

Extract substring from list of file names in Python or R

From Dev

python 3.x tkinter, integrating frames from opencv cv2 into tkinter window

From Dev

python 3.x tkinter, integrating frames from opencv cv2 into tkinter window

From Dev

Get file names of tarred folder contents in Python

From Dev

How to get top 3 count of names from list of dictionary in python

From Dev

Create Gtk3 Treeview with CellRendererToggle and Names from List with Python

From Dev

Real Time text from a file on tkinter window

From Dev

Set window dimensions in Tkinter, Python 3

From Dev

Set window dimensions in Tkinter, Python 3

From Dev

gulp Read all file names from folder and store in some file

From Dev

Get list of file names in folder/directory with Excel VBA

From Dev

Changing color of text in a tkinter window by using colorchooser in python 3 and tkinter

From Dev

How to display a value from children window to parent window in python tkinter?

From Dev

Closing main window from toplevel window with tkinter in python

From Dev

get the names of txt files from assets folder into a list

Related Related

  1. 1

    list file names from a folder to a tkinter window, with python 3

  2. 2

    How to delete files from a folder using a list of file names in windows?

  3. 3

    Batch Files: List file names and folder names

  4. 4

    Rename file names in a folder - Python

  5. 5

    Python 3 | Closing Window on Tkinter

  6. 6

    Python, string slicing (getting file names from a list of file locations)

  7. 7

    Read nested folder and file names as nested list

  8. 8

    Pythonic way to create a list of strings from file names (without file extension) in folder?

  9. 9

    Recursively list files with file names, folder names and permission

  10. 10

    Selecting file names from a folder in php

  11. 11

    Batch For Loop - removing file from folder names

  12. 12

    Getting folder names from text file

  13. 13

    Get a list of file names from HDFS using python

  14. 14

    Extract substring from list of file names in Python or R

  15. 15

    Extract substring from list of file names in Python or R

  16. 16

    python 3.x tkinter, integrating frames from opencv cv2 into tkinter window

  17. 17

    python 3.x tkinter, integrating frames from opencv cv2 into tkinter window

  18. 18

    Get file names of tarred folder contents in Python

  19. 19

    How to get top 3 count of names from list of dictionary in python

  20. 20

    Create Gtk3 Treeview with CellRendererToggle and Names from List with Python

  21. 21

    Real Time text from a file on tkinter window

  22. 22

    Set window dimensions in Tkinter, Python 3

  23. 23

    Set window dimensions in Tkinter, Python 3

  24. 24

    gulp Read all file names from folder and store in some file

  25. 25

    Get list of file names in folder/directory with Excel VBA

  26. 26

    Changing color of text in a tkinter window by using colorchooser in python 3 and tkinter

  27. 27

    How to display a value from children window to parent window in python tkinter?

  28. 28

    Closing main window from toplevel window with tkinter in python

  29. 29

    get the names of txt files from assets folder into a list

HotTag

Archive