-1
import os
import tkinter as tk
used_list = []
image_list= os.listdir("Faces")
class_names = []
for file in image_list:
   class_names.append(os.path.splitext(file)[0])
add_key = 0
width = '400'
height = '800'
width_int = int(width) 
height_int = int(height) 
but_height = height_int / len(image_list)
but_width = width_int / len(image_list)
lbl_add = but_height / 2
but_width = int(but_width)
but_height = int(but_height)
root = tk.Tk()
root.geometry(f"{width}x{height}")
for fle in class_names:
  def fun2():
    print(fle)
  cur_but = tk.Button(root,width = but_width,height = height_int,bg='#8a081e',command = lambda:[fun2()])
  cur_but.place(x = 0,y = add_key)
  add_key += but_height
for thing in class_names:
  txt = tk.Label(root,text = f"{thing}",bg='#8a081e',fg = 'white')
  txt.place(x = (width_int / 2) - 30,y =lbl_add)
  lbl_add += but_height
big_text = tk.Label(root,text = '                   Pick Two Files to see if both people have matching faces',width = 100,height = 5, anchor='w')
big_text.place(x=0,y=0)
root.mainloop()

this if my code , but the only thing getting appended is only one of the files in the directory, can i give each button a certain special entity to call it later

  • Why do You have Your code so much packed together? It would be way easier to read and debug if You were to leave empty line between. Especially between functions: per PEP 8 there should be two blank lines before and after a function definition. – Matiiss May 19 '21 at 16:29
  • Does this answer your question? [tkinter creating buttons in for loop passing command arguments](https://stackoverflow.com/questions/10865116/tkinter-creating-buttons-in-for-loop-passing-command-arguments) – jasonharper May 19 '21 at 16:32

2 Answers2

0

This should do:

for fle in class_names:
  cur_but = tk.Button(root, width=but_width, height=height_int, bg='#8a081e', command=lambda fle=fle: print(fle))
  cur_but.place(x=0, y=add_key)
  add_key += but_height

But You should really think of how You format Your code (because IMO it just looks terrific, because it is not readable).

Anyways, another option would be to use partial so at the start You would:

from functools import partial

And then use:

command=partial(print, fle)
Matiiss
  • 5,970
  • 2
  • 12
  • 29
0

replace your function with this:

def fun2(flee):
    print(flee)

Also, replace your command : command = lambda :fun2(fle) with command = lambda i=fle:fun2(i)