-1

I'm working on a tkinter ttk interface to automate/facilitate some relatively complex reporting.

I have a function which performs a complex data scan across a directory. To execute the function you click a button widget in the toolbar:

fileMenu.add_command(label="Generate Report",command=ScanActiveProjects)

However I would like to reuse the code in this function by adding an additional parameter (simple=True/False) so that I can map a partial and a complete execution of the function to individual buttons.

I thought that I could do this fairly simply like this:

fileMenu.add_command(label="Generate Management Report",command=ScanActiveProjects(simple=True)
fileMenu.add_command(label="Generate Detailed Report",command=ScanActiveProjects(simple=False)

The result of this is that the button is automatically pressed on starting up the program - which I don't understand. So my questions are:

1) Why is the button automatically pressed when adding the (simple=True)/(simple=False)parameters?

2) How can I bind a callback function to a button but have the button provide a specific constructor parameter?

user3535074
  • 1,268
  • 8
  • 26
  • 48

1 Answers1

1
  1. The button is automatically pressed since you do a function call to SendActiveProjects in the add_command.
  2. You can use functools.partial to do what you want.
Miki Tebeka
  • 13,428
  • 4
  • 37
  • 49
  • The simplest solution was to change: command=ScanActiveProjects(True) to: command= lambda: ScanActiveProjects(False) – user3535074 Sep 10 '16 at 09:55