Je pense que ce dont vous avez besoin ici est en fait une liaison. Bouton non requis
Voici un exemple qui va lister tout dans votre répertoire sélectionné, puis lorsque vous cliquez dessus dans la zone de liste déroulante, il imprimera sa sélection. Mise à jour
, a ajouté le répertoire et le nom du fichier combinant pour obtenir un nouveau chemin complet.
from Tkinter import *
import tkFileDialog
import ttk
import os
indir= '/Users/username/results'
new_full_path = ""
root = Tk()
# we use StringVar() to track the currently selected string in the combobox
current_selected_filepath = StringVar()
b = ttk.Combobox(master=root, values=current_selected_filepath)
function used to read the current StringVar of b
def update_file_path(event=None):
global b, new_full_path
# combining the directory path with the file name to get full path.
# keep in mind if you are going to be changing directories then
# you need to use one of FileDialogs methods to update your directory
new_full_path = "{}{}".format(indir, b.get())
print(new_full_path)
# here we set all the values of the combobox with names of the files in the dir of choice
b['values'] = os.listdir(indir)
# we now bind the Cobobox Select event to call our print function that reads to StringVar
b.bind("<<ComboboxSelected>>", update_file_path)
b.pack()
# we can also use a button to call the same function to print the StringVar
Button(root, text="Print selected", command=update_file_path).pack()
root.mainloop()
commande widget sont normalement utilisé pour lier à une fonction/méthode. Il existe 'bind()' qui peut être utilisé pour lier des événements, par exemple 'bind ("
J'ai mis à jour ma réponse pour inclure une méthode de combinaison du répertoire et du nom de fichier pour obtenir un chemin de fichier complet. Il existe plusieurs méthodes pour obtenir un chemin de fichier, donc si vous avez besoin de quelque chose de plus spécifique, faites le moi savoir. –