mercredi 19 août 2020

Aligning and scrolling with checkboxs in python tkinter

Good morning,

I am trying to create a list of checkboxes embedded in a Text widget with a scroll bar. this is part of a larger application and i do not want he number of checkboxes to drive the size of the window.

This is a working snippet of code for just this part of my application:

from tkinter import *

app = Tk()

col_count, row_count = app.grid_size()
scroller = Scrollbar(app)
scroller.grid(column=6, columnspan=1, row=0, rowspan=6, sticky='nsw')
outputArea = Text(app, height=26, width=100, state=DISABLED, yscrollcommand = scroller.set)
outputArea.grid(column=0, columnspan=6, row=0, rowspan=6, sticky='nesw')
scroller.config(command=outputArea.yview)

cb = []
for i in range(1000):
    bg = 'grey'
    if i % 2 == 0:
        bg = 'white'
    cb.append(Checkbutton(outputArea, text="checkbutton #%s" % i, bg=bg, width=100, justify=LEFT))
    #cb[i].grid(sticky='W')
    cb[i].pack(side=LEFT, anchor='w', expand=YES)
    outputArea.window_create("end", window=cb[i])
    outputArea.insert("end", "\n") # to force one checkbox per lin
app.update()
app.mainloop()

That creates this: enter image description here

The two issues I have at the moment which i have not been able to find a working solution for are:

  1. I want the checkboxs and their text to align to the left as the text length will be variable in the real application so i want the boxs to lign up.
  2. When I hover over the checkboxs the scroll no longer works I understand this is because the focus is now on the checkbox rather than text widget and found a post that said to use the following but this seems to have no affect.
    cb[i].bind('<Button-4>', lambda event: text.yview_scroll(-1, tk.UNITS))
    cb[i].bind('<Button-5>', lambda event: text.yview_scroll( 1, tk.UNITS))

Sean




Aucun commentaire:

Enregistrer un commentaire