mardi 28 août 2018

Python tkinter Checkboxes

I trying to work checkboxes, but am having trouble getting the selected values back (ideally as a list of strings).

I need the list of check boxes to be variable. In this case, the column titles for items in my dataset. If a certain column exists, i need the list of options to be all values after that point.

For example, my dataframe could have columns:

Col1  Col2  Col3  Col4  Col5

If Col2 exists, The list of options for the tkinter checkboxes should be:

Col3  Col4  Col5 

Then if Col3 and Col5 are selected with checkboxes, i need the output to be:

choices = ["Col3", "Col5"]

This is my current attempt:

import tkinter as tk
from tkinter import *
import pandas as pd

df1 = pd.DataFrame(columns=['Col1','Col2','Col3','Col4','Col5'])

l1 = list(df1.columns.values)
target_element = "Col2"
try:
    target_index = l1.index(target_element)
except ValueError:
    target_index = None
    l2 = l1[target_index + 1 :]
    my_list = list(l2)

class CheckbuttonList(tk.LabelFrame):
    def __init__(self, master, text=None, list_of_cb=None):
        super().__init__(master)

        self['text'] = text
        self.list_of_cb = list_of_cb
        self.cb_values = dict()
        self.check_buttons = dict()


    if self.list_of_cb:
        for item in list_of_cb:
            self.cb_values[item] = tk.BooleanVar()
            self.check_buttons[item] = tk.Checkbutton(self, text=item, variable=self.check_buttons)
            self.check_buttons[item].config(onvalue=True, offvalue=False, variable= self.cb_values[item])
            self.check_buttons[item].pack(anchor= W)

if __name__ == '__main__':

root = tk.Tk()

choices = {item:IntVar() for item in my_list} #create dict of check_boxes

my_cbs = CheckbuttonList(root, "SELECT VARIABLE", check_boxes)
my_cbs.pack()

root.mainloop()

print(choices.get())

From this point I have been running into several problems. the most recent being: TypeError: get expected at least 1 arguments, got 0

If anyone knows how to get this working or possibly a better solution to the problem, I would greatly appreciate any help!




Aucun commentaire:

Enregistrer un commentaire