Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Python - Can someone explain this function to me?

CandleJakk
 Share

Hi all, using Tkinter and started using Treeview from the ttk module in order to create a table. I searched for a function to sort columns in a Tv table, and got this:

def sort_column(a, col, reverse): # https://stackoverflow.com/questions/22032152/python-ttk-treeview-sort-numbers/46712710
        l = [(a.set(k, col), k) for k in a.get_children('')]
        l.sort(key = lambda t: float(t[0].strip("%")), reverse=reverse)

        for index, (val, k) in enumerate(l):
            a.move(k, '', index)

        a.heading(col, command = lambda: sort_column(a, col, not reverse))

 

This is a slightly adapted version so it would work with %'s, but other than that it's the same. Could someone please talk me through what's going on? I understand the individual lines(ish) but struggle to understand it as a whole.

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

You just wanna know what it does?

Well, it does this. As you can see, it sort the data(which is integers in this case) in a table column by their numerical values. 

 

The GUI API this coder is using is TKinders. If you wish to know more specific details about the APIs, just read up it's documentation. 

http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Treeview.html

Screenshot_20181118-194050.jpg

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×