thanasissdr
thanasissdr

Reputation: 817

How to assign a button click into a variable on tkinter

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()

Upvotes: 0

Views: 3192

Answers (3)

user459872
user459872

Reputation: 24897

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()

Upvotes: 0

Mike - SMT
Mike - SMT

Reputation: 15236

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()

Upvotes: 1

GreenSaber
GreenSaber

Reputation: 1148

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.

Upvotes: 0

Related Questions