0

I am building a small application using tkinter where it shows you the list of work done in the particular given interval ,the format is as below

      co11              col2

   Time done         Activity name

Below is the code.since the two columns are far away when i hover on one of the two labels inside the for loop, i want both the labels to be highlight for easy identification of time done-activity name. i have implemented the labels in a grid layout as separate labels.

                def on_click(e):
                    e.widget['bg']='#fafafa'
                def on_leave(e):
                    e.widget['bg']='#dcdcdc'
                if result1:
                    Label(second_frame, text=start_time[11:], width=115, font=('Tahoma', 15),
                          fg='black',bg='#aaaaaa').grid(row=x,column=0, columnspan=2)
                    x += 1
                    for j in result1:
                        temp = int(j[2]) * int(j[3])
                        # print(j[0][11:], j[1], j[2], j[3], temp)
                        sub_total += temp
                        total_pts += temp
                        e1=Label(second_frame, text=j[0][11:], font=('Tahoma', 12), bg='#dcdcdc', width=71)
                        e1.grid(row=x,column=0)

                        e2=Label(second_frame, text=str(j[1]), font=('Tahoma', 12), bg='#dcdcdc', width=71)
                        e2.grid(row=x,column=1)
                        e1.bind('<Enter>',on_click)
                        e1.bind('<Leave>',on_leave)
                        e2.bind('<Enter>', on_click)
                        e2.bind('<Leave>', on_leave)
                        x += 1
Niresh
  • 67
  • 7

2 Answers2

1

You can link the pair buttons in each row to each other using a user-defined attribute of the button, then you can highlight them together easily.

Below is an example:

import tkinter as tk

root = tk.Tk()

def on_click(e):
    e.widget['bg'] = "#fafafa"
    e.widget.peer['bg'] = "#fafafa" # set the peer as well

def on_leave(e):
    e.widget['bg'] = "#dcdcdc"
    e.widget.peer['bg'] = "#dcdcdc" # set the peer as well

for i in range(10):
    e1 = tk.Label(root, text=f"Left {i+1}", width=10, bg="#dcdcdc", font="Tahoma 12")
    e1.grid(row=i, column=0, padx=(0,300))

    e2 = tk.Label(root, text=f"Right {i+1}", width=10, bg="#dcdcdc", font="Tahoma 12")
    e2.grid(row=i, column=1, padx=(300,0))

    e1.bind("<Enter>", on_click)
    e1.bind("<Leave>", on_leave)
    e2.bind("<Enter>", on_click)
    e2.bind("<Leave>", on_leave)

    # link the pair buttons together
    e1.peer = e2
    e2.peer = e1

root.mainloop()

Update: example with more than 2 entries in a row:

import tkinter as tk

root = tk.Tk()

def on_click(e):
    for entry in rows[e.widget.row]:
        entry['bg'] = "#fafafa"

def on_leave(e):
    for entry in rows[e.widget.row]:
        entry['bg'] = "#dcdcdc"

# use 2-dimensional list to store the entries
rows = []
for i in range(10):
    entries = []  # store entries in a row
    for c in range(4):
        e = tk.Label(root, text=f"Item {i+1},{c+1}", width=10, bg="#dcdcdc", font="Tahoma 12")
        e.grid(row=i, column=c, padx=50)
        e.bind("<Enter>", on_click)
        e.bind("<Leave>", on_leave)
        e.row = i  # save the row number
        entries.append(e)
    rows.append(entries)

root.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • State clear what the requirement you want finally. – acw1668 Feb 18 '21 at 23:26
  • I have another function which is similar to the above but then with 4 labels .Same problem of highlighting multiple labels on hover but 4 labels now .What can be done ? – Niresh Feb 19 '21 at 03:56
0

An easy solution could be to group the items that need to be configured like in a list

Example

from tkinter import *

def on_enter(event):
    if event.widget in labels:
        for label in labels:
            label['bg']='#fafafa'

def on_leave(event):
    if event.widget in labels:
        for label in labels:
            label['bg']='#dcdcdc'

root=Tk()

hello_label=Label(root,text='Hello')
hello_label.pack(side='left',pady=20)
world_label=Label(root,text='World')
world_label.pack(side='left',pady=20)
labels=[hello_label,world_label]

root.bind_all('<Enter>',on_enter)
root.bind_all('<Leave>',on_leave)

root.mainloop()
astqx
  • 2,058
  • 1
  • 10
  • 21