0

I want to create a tkinter window, where it will appear the files of a folder as a dropdown menu and a Select button, such that when I select an element from the previous list the full path will be saved into a new variable. Apparently, I need to give an appropriate command.

from Tkinter import *
import tkFileDialog
import ttk
import os




indir= '/Users/username/results'


root = Tk()

b = ttk.Combobox(master=root, values=os.listdir(indir)) # This will create a dropdown menu with all the elements in the indir folder.
b.pack()
w = Button(master=root, text='Select', command= ?)
w.pack()

root.mainloop()
2
  • widget command are normally used to link to a function/method. There is bind() that can be used to bind events, for example bind("<Button-1", some_function_name) will bind the left mouse click to execute said function that is linked in the bind. If you have a drop down list you are using there is most likely a way to apply commands or StringVar() to each drop down item. Commented Oct 11, 2017 at 15:38
  • I updated my answer to include a method of combining the directory and file name together to get a complete file path. There are several methods to get a file path so if you need something more specific let me know. Commented Oct 11, 2017 at 16:07

3 Answers 3

1

I think what you need here is actually a binding. Button not required.

Here is an example that will list everything in your selected directory and then when you click on it in the Combo Box it will print out its selection.

Update, added directory and file name combining to get new full path:

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer. However, I need to save the result and not print it, because I need to use that into another function.
0

Try something like this:

w = Button(master=root, text='Select', command=do_something)

def do_something():
    #do something

In the function do_something you create what you need to get the full path. You can also pass vars into the command.

2 Comments

How can I do that? I can create a function like: def return_full_path(): return indir + ? . But what would I put in the place of the question mark? The question mark is going to be selected by the mouse, as it is an element in the dropdown list.
I'm not exactly sure what you mean. The example code I have above will execute when you press the button. So you'll just need to include the code to get the path in the function.
0

get() Returns the current value of the combobox.(https://docs.python.org/3.2/library/tkinter.ttk.html)

from Tkinter import *
import tkFileDialog
import ttk
import os


indir= '/Users/username/results'

#This function will be invoked with selected combobox value when click on the button
def func_(data_selected_from_combo):
    full_path = "{}/{}".format(indir, data_selected_from_combo)
    print full_path
    # Use this full path to do further



root = Tk()

b = ttk.Combobox(master=root, values=os.listdir(indir)) # This will create a dropdown menu with all the elements in the indir folder.
b.pack()


w = Button(master=root, text='Select', command=lambda: func_(b.get()))
w.pack()

root.mainloop()

3 Comments

This method only prints the path to the console. It doesn't assign the path string to a variable as OP requested.
@KesPerron The path is already available in the variable full_path.
Don't variables assigned inside a function get purged from memory once the function completes? Your function neither returns full_path nor declares it as global.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.