Python 3: Tkinter OptionMenu Detect Change & Pass Variables
<p>So I am working on my first "large" python project (second GUI), and it is a simple SQLite database manager. As of now, this is what it looks like...
<a href="http://i.stack.imgur.com/GPTlJ.png" rel="nofollow"><img src="http://i.stack.imgur.com/GPTlJ.png" alt="Current state of database manager."></a></p>

<p>At the top, as you can see, there is a change table button and a drop-down to select what table you wish to switch to. </p>

<p>What I want to do is to remove the button, replace it with an ordenary label(This is easy to do), and then have the OptionMenu change the table a different one is selected automatically.</p>

<p>This is what I have for the changing.</p>

<pre><code>def change(self, root, c, tableToChangeTo, table, list_columns):
    table = tableToChangeTo.get()
    cursor = c.execute('select Rowid, * from ' + table)
    list_columns = [description[0] for description in cursor.description]

    self.clear()
    self.table(root, c, table, list_columns)
    self.addGui(root, c, table, list_columns)
    self.editGui(root, c, table, list_columns)
    self.delGui(root, c, table, list_columns)
    self.changeGui(root, c, table, list_columns)


def changeGui(self, root, c, table, list_columns):
    gridRow = self.rowTotal
    self.changeButton = Button(root, text="Change Table", command= lambda: self.change(root, c, var, table, list_columns))
    self.changeButton.grid(row = 0 , column = 0)

    var = StringVar(root)
    var.set(table)

    options = {""}
    global tableList
    ta = tableList

    for t in ta:
        options.add(t)

    self.changeOption = OptionMenu(root, var, *options)
    self.changeOption.grid(row = 0, column = 1)
</code></pre>

<p>So to make it short and sweet, I want to make it where when you select another table, it will automatically run the change method. The big problem is I can not seem to figure out how to make a method an event while at the same time being able to pass variables to it.</p>

<p>So thank you for any help, and if you need anything else, let me know!</p>

<p>For anyone else having the same problem as me, I was able to get mine working by using this line of code.</p>

<pre><code>self.changeOption = OptionMenu(root, var, *options, command= lambda event: self.change(event,root, c, var, table, list_columns))
</code></pre>

python sqlite python-3.x user-interface tkinter