0

--Edit: my currently attempt, pretty ugly

    import tkinter as tk

class Example(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)


        self.labelLists = []
        self.labelBLists = []

        self.Label1 = tk.Label(self,text=str(1),bg="red")
        self.Label1.pack()
        self.Label1.bind("<Enter>", self.on_enter1)
        self.Label1.bind("<Leave>", self.on_leave1)
        self.Labela = tk.Label(self,text="",bg="blue")
        self.Labela.pack()

        self.Label2 = tk.Label(self,text=str(2),bg="red")
        self.Label2.pack()
        self.Label2.bind("<Enter>", self.on_enter2)
        self.Label2.bind("<Leave>", self.on_leave2)
        self.Labelb = tk.Label(self,text="",bg="blue")
        self.Labelb.pack()

        self.Label3 = tk.Label(self,text=str(3),bg="red")
        self.Label3.pack()
        self.Label3.bind("<Enter>", self.on_enter3)
        self.Label3.bind("<Leave>", self.on_leave3)
        self.Labelc = tk.Label(self,text="",bg="blue")
        self.Labelc.pack()


    def on_enter1(self, event):
        self.Labela.config(text=self.Label1.cget("text"))

    def on_leave1(self, enter):
        self.Labela.config(text="")

    def on_enter2(self, event):
        self.Labelb.config(text=self.Label2.cget("text"))
    def on_leave2(self, enter):
        self.Labelb.config(text="")
    def on_enter3(self, event):
        self.Labelc.config(text=self.Label3.cget("text"))
    def on_leave3(self, enter):
        self.Labelc.config(text="")

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(side="top", fill="both", expand="true")
    root.mainloop()

I want to create a group of label, say L1, L2, L3, and each one has a corresponding label La, Lb, Lc. What I want to do is when I hover over L1, La display the translation of word on L1. Currently I'm looking at Display message when going over something with mouse cursor in Python and Python Tkinter: addressing Label widget created by for loop, but neither of them addresses binding corresponding method. Is there a way that I can achieve this without creating three pairs of different methods?

Thanks!

Community
  • 1
  • 1
angieShroom
  • 133
  • 3
  • 9

1 Answers1

1

Store each set of labels in a list. Then you can go through them together, along with a translation dictionary, and connect the secondary labels (that display a translation) to the primary labels (that respond to user input). This allows you to create a single enter method and a single leave method, using event.widget to access the widget that triggered the event.

import tkinter as tk

class Example(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.translations = {'a':'A', 'b':'B', 'c':'C'}

        self.labelLists = [tk.Label(self,text=str(x),bg="red") for x in range(1,4)]
        self.labelBLists = [tk.Label(self,text="",bg="blue") for x in range(3)]
        for x,y,tr in zip(self.labelLists, self.labelBLists, sorted(self.translations)):
            x.bind('<Enter>', self.enter)
            x.bind('<Leave>', self.leave)
            x.connected = y
            x.key = tr
            x.pack()
            y.pack()

    def enter(self, event):
        widget = event.widget
        widget.connected.config(text=self.translations[widget.key])

    def leave(self, event):
        widget = event.widget
        widget.connected.config(text='')

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(side="top", fill="both", expand="true")
    root.mainloop()
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • I didn't quite understand how the translation work here, can you explain more? – angieShroom Dec 06 '16 at 02:02
  • 1
    @angieShroom - Just as this assigns each `x` widget an attribute of a connected `y` widget `connected`, it also assigns an attribute of a `tr` key `key`. That attribute is used as a key to look up the corresponding value in `self.translations`. – TigerhawkT3 Dec 06 '16 at 02:19