简体   繁体   English

TKINTER 在文本框中获取 LABEL 的 position

[英]TKINTER get the position of LABEL in TEXT FRAME

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?有什么办法可以通过点击得到LABEL的position吗?

Not coordinates but rather position. Example: These are 15 different LABELS and I need the position of the LABEL 'different at position 4'.不是坐标,而是 position。示例:这些是 15 个不同的标签,我需要 position 中的 LABEL“在 position 4 处不同”。 Result: 4 after clicking 'different' LABEL结果:单击“不同”后为 4 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()

EDIT: Decided to go with a different solution:编辑:决定使用不同的解决方案 go:

def get_position2( label ):
    x = text.index(str(label))
    print(x.split('.')[-1])

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>', lambda event, label=label: get_position2(label)) 
            text.window_create("end", window=label)

If you want an index you'll need the function called by the click to return the index number.如果你想要一个索引,你需要点击调用的 function 来返回索引号。 This can be attached with a function closure as shown below, or with functools.partial if you're more comfortable with that.这可以附加一个 function 闭包,如下所示,或者如果您更愿意的话,可以附加functools.partial

I've shown the index incrementing by line.我已经显示了按行递增的索引。 It could be incremented for each label or whatever is required.它可以为每个 label 或任何需要的值递增。 The identifier for the label will need to be identified as the label is created, so that it's known when it's clicked. label 的标识符需要在创建 label 时进行标识,以便在单击时知道。

Code shown from def get_position to just above root.mainloop()def get_positionroot.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. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM