Question
Windows
3.xx (newest stable)
Newest
__beginner_ Python programming experience
__beginner/above beginner__ Programming experience overall
__yes_ Have used another Python GUI Framework (tkinter, Qt, etc) previously (yes/no is fine)?
I have a table generated and would like to make a sort function when clicking on a header, like ascending/descending.
I am aware there is an example on the DEMO page, but it has some issues:
-Seems like it works by overlaying an invisible button which accepts the event. This doesn't seem optimal?
-It only sorts integers, I'm trying to sort by date, which is a string, but even if converted to int it can't be sorted that way.
-It doesn't toggle / sort twice. You can only click it once.
By default, the header changes color when you hover your mouse over it, so I assume there must be some underlying event-handler?
Besides that, I'm not actually really sure how to approach this, but I'm thinking something like:
-User clicks columm header
-Regular python function that re-sorts my list
-Table gets updated with new list
Or is there a better / another way?
COL_HEADINGS = ["Date", "Ref", "ID", "Owner", "Customer", "Price", "Size", "Path"]
layout = [[sg.Table(values=newlist, headings=COL_HEADINGS, max_col_width=25,
# background_color='light blue',
auto_size_columns=True,
display_row_numbers=False,
justification='right',
num_rows=20,
alternating_row_color='lightgrey',
key='-TABLE-',
row_height=35,
enable_events=True,
)],
[sg.Button('Get row ID')],
[sg.Text('Text boxx')],]
#Window
window = sg.Window('Table', layout, size=(1200,900)
# font='Helvetica 25',
)
while True:
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED:
break
if event == 'Get row ID':
print("Tryk p氓 knap")
##this doesnt work:
if event in COL_HEADINGS:
col_clicked = COL_HEADINGS.index(event)
print("Column")
Just general advice would be very much appreciated, thank you in advance.
Bonus question: Is there any way i can populate a table using dicts instead of lists?
There isn't a way at the moment to get this event.
Is there any way i can populate a table using dicts instead of lists?
Depends on what's in your dictionary. You basically would convert your dictionary into a list. dict.keys() provides a list of the dictionary's keys and dict.values() is a list of the values. Not sure of exactly how you're storing your data so difficult to advise you on how to go from a dictionary to a list of lists.
Thank you, that's how I'm currently doing it, but I use another framework that needs the same data as a dict, so I'm converting a bit back and fourth and was just wondering if there was a way to make tables take dicts as default, but it's okay, was just a minor thing :-)
@PySimpleGUI Any news on this (clicking on table headers should generate event)? :-)
Nothing new. Work on tkinter is halted while the Qt work is happening. I may try and get the tkinter code on GitHub posted to PyPI soon to get some of that stuff available to everyone, but new feature additions aren't going to happen for a while.
a way to make tables take dicts as default,
I should have addressed this question specifically. Sorry I missed it. There won't be an option to do this. Tables take lists. It's up to the user to convert their data into the format needed by PySimpleGUI. The same is true for Trees. PySimpleGUI has its own tree data structure. The user's code needs to convert their data into that format.