Reputation: 1143
I have placed multiple Combobox in the grid as shown below.
These comboboxes are stored in array with corresponding row, column for eg as shown below.
self.c_arr[row,column] = ttk.Combobox(what_ev,
textvariable=self.c_it_val[row,column],
values=list(self.c_list[row,column].keys()))
self.c_arr[row,column].bind("<<ComboboxSelected>>", lambda
x:self.c_capture(what_ev, <Want to pass row, column information as well>))
Whenever the function bound to the combobox is called/triggered, I wanted to pass the respective row, column number to the called function along with other variables.
How to get/retrieve the respective widgets row,column number/location details placed on the grid?
UPDATE: Adding with an Example code below.
Example Code:
import Tkinter
from Tkinter import *
from Tkinter import Tk, StringVar
import ttk
import tkFont
class Application:
def __init__(self, parent):
self.parent = parent
self.sco1 = None
self.c_int_val = StringVar()
self.box = {}
self.box_value = {}
self.box_int_list = {}
self.combo()
def combo(self):
for row in range (3):
for column in range(3):
self.box_int_list[row,column] = {key: None for key in range(1,10)}
self.box_value[row,column] = StringVar()
self.box_value[row,column].set("0")
self.box[row,column] = ttk.Combobox(self.parent, textvariable=self.box_value[row,column], values = list(self.box_int_list[row,column].keys()), state='readonly', width=39)
self.box[row,column].bind("<<ComboboxSelected>>", lambda x:self.print_selected_value("Eurekaa", len(self.box_int_list[row,column])))
self.box[row,column].grid(row=row, column=column)
def print_selected_value(self, what_name, list_len, *args):
print "Value selected is: %s"%(self.box[row,column].get())
print what_name
print list_len
if __name__ == '__main__':
root = Tk()
app = Application(root)
root.mainloop()
But since I wasnt able to get the row,column info, was unable to get the value from the selected combobox. How can that be done ?
Upvotes: 0
Views: 2464
Reputation: 386342
If you pass the event to your callback, you can use grid_info
on the widget associated with the event.
def callback(event, row, column):
print(row, column)
cb.bind("<<ComboboxSelected>>", callback)
If you want to pass the information, there's nothing preventing you from doing that using lambda
or functools.partial
:
c.bind("<<ComboboxSelected>>",
lambda event, row=row, column=column: callback(event, row, column))
Upvotes: 1