4001jh
4001jh

Reputation: 9

How to pass arguments to lambda function in yscrollcommand in tkinter Listbox?

6 Listbox objects are generated in a for loop in app class. And its yscrollcommand, 6 functions are hard coded. (def function for 0, def function for 1...)

It seems if I can pass the index argument to lambda function, 6 functions for list scroll can be compressed to 1 for loop.

But the funtion in a class, it has argument 'self'. It make me confusing.

How can I pass index argument to lambda function in yscrollcommand?

class app(tk.Frame):
    def __init__(self):
        self.root = tk.Tk()
        self.root.title('title something')

        # showing data frame
        self.data_frame = tk.LabelFrame(self.root, text='')
        self.data_frame.pack(fill='x')
        
        self.scrollbar = tk.Scrollbar(self.data_frame)
        self.scrollbar.pack(side='right', fill='y')
        
        self.listboxes = []
        self.listboxes_column = 6 # This can be vary.
        
        # listboxes are in a list.
        for i in range(self.listboxes_column):
            self.listboxes.append(tk.Listbox(self.data_frame, selectmode='extended', height=20, width=0, yscrollcommand = self.scrollbar.set))
            self.listboxes[i].pack(side='left')
            
        # when self.listboxes_column == 3
        # self.list_indexes == [[1, 2, 3], [0, 2, 3], [0, 1, 3], [0, 1, 2]]
        self.list_indexes = []
        for i in range(self.listboxes_column):
            indexes = [j for j in range(self.listboxes_column)]
            indexes.remove(i)
            self.list_indexes.append(indexes)

        # a listbox can scroll the others     
        # with lambda function and argument passing,
        # I want to make these 6 lines be in a for loop.
        # The from like this
        # for i in range(6):
        #     self.listboxes[index_argument].config(yscrollcommand = lambda ????? : self.list_scrolls_all(index_argument  ????? ))

        self.listboxes[0].config(yscrollcommand = self.list0_scrolls_all)
        self.listboxes[1].config(yscrollcommand = self.list1_scrolls_all)
        self.listboxes[2].config(yscrollcommand = self.list2_scrolls_all)
        self.listboxes[3].config(yscrollcommand = self.list3_scrolls_all)
        self.listboxes[4].config(yscrollcommand = self.list4_scrolls_all)
        self.listboxes[5].config(yscrollcommand = self.list4_scrolls_all)
        
        self.scrollbar.config(command=self.bar_scrolls_all)
        self.root.mainloop()


   # functions for lists scroll from 0 to 5.
   # I don't know how to pass argument via yscrollcommand in Listbox.
   # I want a form like this.
   #
   # def list_scrolls_all(self, index, *args):
   #     for i in self.list_indexes[index] :
   #         self.listboxes[i].yview_moveto(args[0])
   #     self.scrollbar.set(*args)

   def list0_scrolls_all(self, *args):
        for i in self.list_indexes[0] :
            self.listboxes[i].yview_moveto(args[0])
        self.scrollbar.set(*args)

    def list1_scrolls_all(self, *args):
        for i in self.list_indexes[1] :
            self.listboxes[i].yview_moveto(args[0])
        self.scrollbar.set(*args)


   # scroll bar
   def bar_scrolls_all(self,*args):
        for i in range(self.listboxes_column):
            self.listboxes[i].yview(*args)

Upvotes: 0

Views: 185

Answers (1)

Derek
Derek

Reputation: 2234

I just happen to be working on a multi listbox program so I've made a few modifications to your code.

I've included flexx function so all objects for totally flexible, although I had to replace pack the grid.

import tkinter as tk

def flexx(m, r = 0, c = 0, rw = 1, cw = 1):
    if r !=  None:
        m.rowconfigure(r, weight = rw)
    if c !=  None:
        m.columnconfigure(c, weight = cw)

class app:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('title something')
        # make root flexible
        flexx(self.root)

        # showing data frame
        self.data_frame = tk.LabelFrame(self.root, text='Multiple Linked Listboxes')
        self.data_frame.grid(row = 0, column = 0, sticky = tk.NSEW)

        self.listboxes = []
        self.listboxes_column = 6 # This can be vary.

        self.scrollbar = tk.Scrollbar(self.data_frame)
        self.scrollbar.grid(row = 0, column = self.listboxes_column, sticky = tk.NS)

        # listboxes are in a list.
        for i in range(self.listboxes_column):
            self.listboxes.append(tk.Listbox(
                self.data_frame, selectmode='extended',
                height=20, width=0))
            self.listboxes[i].grid(row = 0, column = i, sticky = tk.NSEW)

            # make data_frame flexible
            flexx(self.data_frame, c = i)

            # populate listbox with some data for testing purposes
            for b in dir(self):
                self.listboxes[i].insert("end", b)

        # connect yscollcommand to all listboxes
        for a in self.listboxes:
            a["yscrollcommand"] = self.move_to

        if False: # Not sure waht this does
            # when self.listboxes_column == 3
            # self.list_indexes == [[1, 2, 3], [0, 2, 3], [0, 1, 3], [0, 1, 2]]
            self.list_indexes = []
            for i in range(self.listboxes_column):
                indexes = [j for j in range(self.listboxes_column)]
                indexes.remove(i)
                self.list_indexes.append(indexes)

        # connect scrollbar command to all y_views
        self.scrollbar.config(command = self.y_view)

    def y_view(self, *args):
        for a in self.listboxes:
            a.yview(*args)

    def move_to(self, *args):
        self.scrollbar.set(*args)
        self.y_view("moveto", args[0])

if __name__ == "__main__":
    demo = app()
    demo.root.mainloop()

Upvotes: 0

Related Questions