Home > Mobile >  TKINTER get the position of LABEL in TEXT FRAME
TKINTER get the position of LABEL in TEXT FRAME

Time:11-12

I have a TEXT Widget as a FRAME und add LABELS to it. Any way to get the position of the LABEL by clicking it?

Not coordinates but rather position. Example: These are 15 different LABELS and I need the position of the LABEL 'different at position 4'. Result: 4 after clicking 'different' LABEL

import tkinter as tk
from tkinter import *
from tkinter import ttk
import tkinter as tk
import re
import tkinter
from tkinter.tix import COLUMN
from turtle import bgcolor

linelist1 = ['some long text 1 as a label!!!', '@combo@Aa Bb Cc Dd', 'some long text 2 as a label!!!',
 'some long text 3 as a label!!!', '@combo@Ee Ff Gg Hh', 'some long text 4 as a label!!!']
lines_with_combobox = [e for e, s in enumerate(linelist1) if '@combo@' in s]

root = tk.Tk()
root.geometry(f'400x100')
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)

bframe = tk.Frame(root, width=100, height=100, bg='red')
bframe.grid(row=0, column=0)

text = tk.Text(bframe, wrap="char", background=root.cget("background"))
text.pack(fill="both", expand=True)

#def get_position():

for line in range(0, len(linelist1)):
    if line in lines_with_combobox:
        delete_combo_marker = re.split("@combo@", linelist1[line])
        words = delete_combo_marker.pop(0)
        word_as_values = re.split('\s ', delete_combo_marker[0])
        combobox = ttk.Combobox(text, values=word_as_values)
        text.window_create("end", window=combobox)

    else:
        textui = linelist1[line]
        for word in textui.split(" "):
            label = tk.Label(text, text=word)
            #label.bind('<Button-1>', get_position)
            text.window_create("end", window=label)

root.mainloop()

CodePudding user response:

If you want an index you'll need the function called by the click to return the index number. This can be attached with a function closure as shown below, or with functools.partial if you're more comfortable with that.

I've shown the index incrementing by line. It could be incremented for each label or whatever is required. The identifier for the label will need to be identified as the label is created, so that it's known when it's clicked.

Code shown from def get_position to just above root.mainloop()

def get_position( n ):
    """ Returns a function bound to n. """

        def func( e ):  # func will be passed an event. 
            print( n )
            
    return func
 
ix = 0   # Starting Index
 
for line in range(0, len(linelist1)):
    if line in lines_with_combobox:
        delete_combo_marker = re.split("@combo@", linelist1[line])
        words = delete_combo_marker.pop(0)
        word_as_values = re.split('\s ', delete_combo_marker[0])
        combobox = ttk.Combobox(text, values=word_as_values)
        text.window_create("end", window=combobox)

    else:
        textui = linelist1[line]
        ix  = 1                     # Increment the index for each line.
        for word in textui.split(" "):
            # ix  = 1  # OR - Increment index for each label.
            label = tk.Label(text, text=word)
            label.bind('<Button-1>', get_position( ix ) )  # Bind the label to ix
            text.window_create("end", window=label)
    text.insert( "end", '\n' )  # Use this if each 'line' should 
                                # be on a different line in the text widget. 
  • Related