Issue
I've re-written the question for clarity. I'm open to anything- complete redesign if needed.
I am having a problem and trying to start from the ground up since I can't find any solutions that work.
I have a file with a 75 columns, and each column is a feature. I would like the user to be able to select which features to include in the analysis.
Question 1. What is the best widget to use for this? First, I tried using a listbox, but it wouldn't let me select non-consecutive items except on the default state which required selecting each individual item (there are 75 items so this would be a lot of click).
Next, I tried using checkboxes. I think this is the best solution, but I'm having problems implementing. Here's what the UI looks like:
I am trying to associate this with a list of 'clicked' boxes that I can then pass to my back-end to remove unwanted variables since the rest of the application is pretty data intensive.
The select all and deselect all buttons work fine; my problem is with the individual selections.
Is this the right way to accomplish this goal at all? If so, how might this be accomplished? TIA- I started using tkinter yesterday so I know very little.
Here is how I'm generating the below (simplified)
Code that creates the button:
import tkinter as tk
settings.data_included_cols = ['button1'] #This is the list of clicked buttons
checkbox_buttons=dict()
checkbox_variables=dict()
button_names=['button1', 'button2', 'button3']
i=0
for i in range(len(button_names)):
checkbox_variables[i]=1
checkbox_button[i] = tk.Checkbutton(frame, text=button_names[i],
variable=checkbox_variables[i],
command=checkbox_click)
Command Code (checkbox_click)- I don't know what goes here but nothing I've tried so far has worked. Originally I was calling it as: command=lambda: checkbox_click(i)
, and it tried to work like the below:
def checkbox_click(i):
if i in settings.data_included_cols:
settings.data_included_cols.remove(button_name[i])
This doesn't work because 'i' is not associated with the button, it's associated with the loop so it will always be whatever the final value is (in this case 3 which maps to button3).
Any thoughts?
Solution
Checkboxes do not need to have a command specified.
They do however need a StringVar
or IntVar
associated with them, so you can query the values.
So I would build your code like this:
names = {
"id", "member_id", "loan_amnt"
}
values = {}
boxes = {}
for name in names:
values[name] = tk.Intvar(value=1)
boxes[name] = ttk.Checkbox(root, text=name, variable=values[name])
Once the user has made submitted their choice, you can query the dict of values
to see which options were selected.
Answered By - Roland Smith
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.