mercredi 14 septembre 2022

How to adjust the size of checkboxes in treeview tkinter

I have been building a simple program where there is a number of tags on the right side of the window. I was wondering if there is a way that I can make my checkboxes a bit larger so that some of my users can tick those boxes more easily.

Here is my code.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()


class CbTreeview(ttk.Treeview):
    def __init__(self, master=None, **kw):
        kw.setdefault('style', 'cb.Treeview')
        kw.setdefault('show', 'headings')  # hide column #0
        ttk.Treeview.__init__(self, master, **kw)
        # create checheckbox images
        self._im_checked = tk.PhotoImage('checked',
                                         data=b'GIF89a\x0e\x00\x0e\x00\xf0\x00\x00\x00\x00\x00\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x0e\x00\x0e\x00\x00\x02#\x04\x82\xa9v\xc8\xef\xdc\x83k\x9ap\xe5\xc4\x99S\x96l^\x83qZ\xd7\x8d$\xa8\xae\x99\x15Zl#\xd3\xa9"\x15\x00;',
                                         master=self
                                         )
        self._im_unchecked = tk.PhotoImage('unchecked',
                                           data=b'GIF89a\x0e\x00\x0e\x00\xf0\x00\x00\x00\x00\x00\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x0e\x00\x0e\x00\x00\x02\x1e\x04\x82\xa9v\xc1\xdf"|i\xc2j\x19\xce\x06q\xed|\xd2\xe7\x89%yZ^J\x85\x8d\xb2\x00\x05\x00;',
                                           master=self)
        style = ttk.Style(self)
        style.configure("cb.Treeview.Heading", font=(None, 13))
        # put image on the right
        style.layout('cb.Treeview.Row',
                     [('Treeitem.row', {'sticky': 'nswe'}),
                      ('Treeitem.image', {'side': 'right', 'sticky': 'e'})])

        # use tags to set the checkbox state
        self.tag_configure('checked', image='checked')
        self.tag_configure('unchecked', image='unchecked')
        self.tag_configure('evenrow', background='lightblue')
        self.tag_configure('oddrow', background='gray')


    def tag_add(self, item, tags):
        new_tags = tuple(self.item(item, 'tags')) + tuple(tags)
        self.item(item, tags=new_tags)

    def tag_remove(self, item, tag):
        tags = list(self.item(item, 'tags'))
        tags.remove(tag)
        self.item(item, tags=tags)

    def insert(self, parent, index, iid=None, **kw):
        item = ttk.Treeview.insert(self, parent, index, iid, **kw)
        print(kw['values'][0])
        if kw['values'][0] % 2 == 0:
            self.tag_add(item, (item, 'evenrow'))
        else:
            self.tag_add(item, (item, 'oddrow'))
        if kw['values'][2] is True:
            self.tag_add(item, (item, 'checked'))
        else:
            self.tag_add(item, (item, 'unchecked'))


        self.tag_add(item, (item, 'unchecked'))
        self.tag_bind(item, '<ButtonRelease-1>',
                      lambda event: self._on_click(event, item))

    def _on_click(self, event, item):
        """Handle click on items."""
        if self.identify_row(event.y) == item:
            if self.identify_column(event.x) == '#4': # click in 'Is Lv 90' column
                # toggle checkbox image
                if self.tag_has('checked', item):
                    self.tag_remove(item, 'checked')
                    self.tag_add(item, ('unchecked',))
                else:
                    self.tag_remove(item, 'unchecked')
                    self.tag_add(item, ('checked',))



tree = CbTreeview(root, columns=("No.", "Character Name", "Leveled up to 90", "Is Lv 90"),
                  height=20, selectmode="extended")

tree.heading('No.', text="No.", anchor=tk.E)
tree.heading('Character Name', text="Character Name", anchor=tk.E)
tree.heading('Leveled up to 90', text="Leveled up to 90", anchor=tk.E)
tree.heading('Is Lv 90', text="Is Lv 90", anchor=tk.E)

tree.column('#1', stretch='no', minwidth=0, width=30, anchor=tk.E)
tree.column('#2', stretch='no', minwidth=0, width=150, anchor=tk.E)
tree.column('#3', stretch='no', minwidth=0, width=0, anchor=tk.E)
tree.column('#4', stretch='no', minwidth=0, width=100, anchor=tk.E)


tree.pack(fill='both')

tree.insert('', 'end', values=(0, 'Amber', True, ''))
tree.insert('', 'end', values=(1, 'Jean', False, ''))
tree.insert('', 'end', values=(2, 'Lisa', True, ''))
tree.insert('', 'end', values=(3, 'Keqing', True, ''))
tree.insert('', 'end', values=(4, 'Ganyu', True, ''))
tree.insert('', 'end', values=(5, 'Barbara', False, ''))
tree.insert('', 'end', values=(6, 'Paimon', True, ''))
tree.insert('', 'end', values=(7, 'Lumine', True, ''))
tree.insert('', 'end', values=(8, 'Noelle', True, ''))
tree.insert('', 'end', values=(9, 'Diluc', False, ''))
tree.insert('', 'end', values=(10, 'Ayaka', False, ''))
tree.insert('', 'end', values=(11, 'Yae Miko', True, ''))
tree.insert('', 'end', values=(12, 'Raiden Shogun', True, ''))
tree.insert('', 'end', values=(13, 'Ayato', True, ''))
tree.insert('', 'end', values=(14, 'Eula', True, ''))
tree.insert('', 'end', values=(15, 'Hu Tao', True, ''))
tree.insert('', 'end', values=(16, 'Yoimiya', True, ''))

root.mainloop()

Here is what I have got so far. Current size of checkboxes




Aucun commentaire:

Enregistrer un commentaire