1

I have written the following code to list all directories/files in a given path and then write them to a button. How can I use tkinter's events handler so that whenever any button is double clicked within the widget window it calls a new function.

def display_toplevel(globpath):
    global row, column
    dir=globpath
    dirs = os.listdir(dir)
    for file in dirs:
        Button(master, width=8, height=4, text=file).grid(row=row, column=column, padx=10, sticky=W)
        column = column + 2
        if column == 10:
            row = row + 3
            column = 0
            column = column + 2
            break
1
  • Are you trying to reinvent tkFileDialog? Quote: "If you want to open or save a file or to choose a directory using a filedialog you dont need to implement it on your own. The module tkFileDialog is just for you." Commented Feb 28, 2013 at 10:43

1 Answer 1

3

This works for single clicks; in the code where you create the button, add the command = # function parameter:

Button(master, width=8, height=4, text=file,command=my_funct).grid(row=row, column=column, padx=10, sticky=W)
# note how the function does not have parentheses (after command=) 

def my_funct():
    # code

Reference: Tkinter Button Widget and Parameters

Sign up to request clarification or add additional context in comments.

2 Comments

thank you, i was looking at the events binding and overlooked something so trivial
@bigl: You'll need to define the function before binding it, of course.

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.