Vimo
Vimo

Reputation: 1143

How To Get the row and column number for a widget placed in grid in tkinter python?

I have placed multiple Combobox in the grid as shown below.

enter image description here

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.

  1. The below code create a 3x3 COmbobox and binded with a function to get called when any of the respective combobox is selected. When gets selected I wanted to get the value selected in that box and print it.

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

Answers (1)

Bryan Oakley
Bryan Oakley

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

Related Questions