0

我正在使用 tkinter 构建一个小型应用程序,它向您显示在特定给定时间间隔内完成的工作列表,格式如下

      co11              col2

   Time done         Activity name

下面是代码。因为当我将鼠标悬停在 for 循环内的两个标签之一上时,两列距离很远,所以我希望两个标签都突出显示,以便于识别时间完成活动名称。我已经将网格布局中的标签实现为单独的标签。

                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
4

2 回答 2

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()
于 2021-02-18T15:34:05.683 回答
0

一个简单的解决方案可能是将需要配置的项目分组,例如list

例子

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()
于 2021-02-17T10:18:15.897 回答