bigl
bigl

Reputation: 1083

python tkinter button events

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

Upvotes: 1

Views: 1231

Answers (1)

Rushy Panchal
Rushy Panchal

Reputation: 17532

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

Upvotes: 3

Related Questions