0

how to change values ​​in buttons done in a loop?

I mean a different text, a command for each of the resulting buttons.

the program counts files in a given folder, then this value is passed to the loop that displays the appropriate number of buttons. here is the sample text:

files = os.listdir(".")
files_len = len(files) - 1
print(files_len)

def add():
    return

for i in range(files_len):
        b = Button(win, text="", command=add).grid()
  • you can show it with an example? I'm a beginner and I don't understand many things –  Dec 28 '19 at 22:14
  • Read [Get the text of a button if clicked](https://stackoverflow.com/a/6205482/7414759) – stovfl Dec 29 '19 at 10:56

2 Answers2

1

You don't really need to change the Button's text, because you can just put the filename in when they're created (since you can determine this while doing so).

To change other aspects of an existing Button widget, like its command callback function, you can use the universal config() method on one after it's created. See Basic Widget Methods.

With code below, doing something like that would require a call similar to this: buttons[i].config(command=new_callback).

To avoid having to change the callback function after a Button is created, you might be able to define its callback function inside the same loop that creates the widget itself.

However it's also possible to define a single generic callback function outside the creation loop that's passed an argument that tells it which Button is causing it to be called — which is what the code below (now) does.

from pathlib import Path
import tkinter as tk


win = tk.Tk()

dirpath = Path("./datafiles")  # Change as needed.
buttons = []

def callback(index):
    text = buttons[index].cget("text")
    print(f"buttons[{index}] with text {text!r} clicked")

for entry in dirpath.iterdir():
    if entry.is_file():
        button = tk.Button(win, text=entry.stem,
                           command=lambda index=len(buttons): callback(index))
        button.grid()
        buttons.append(button)

win.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Thank you, there is still a problem with the displayed file extension next to the name and command. –  Dec 29 '19 at 07:02
  • marcin: The extension can easily be removed—see updated answer. I also modified it to create a `list` of the `Button` widgets created and described how to change them after creation. – martineau Dec 29 '19 at 10:30
  • marcin: Based on your (now deleted) follow-on comment, I modified my answer again to show how to pass an argument to a more generic callback function that indicates which `Button` widget click caused it to be called. – martineau Dec 29 '19 at 11:47
0

Try this. By making a list.

files = os.listdir(".")
files_len = len(files) - 1
print(files_len)

def add():
    return
b=[]
for i in range(files_len):
        b[i] = Button(win, text="", command=add).grid(row=i)
Inoejj
  • 32
  • 4
  • thank you, how can I make each of the buttons perform a different function? –  Dec 29 '19 at 11:25