【问题标题】:TKINTER get the position of LABEL in TEXT FRAMETKINTER 获取 LABEL 在 TEXT FRAME 中的位置
【发布时间】:2022-11-10 23:08:27
【问题描述】:

我有一个 TEXT Widget 作为 FRAME 并在其中添加 LABELS。有什么方法可以通过单击获得 LABEL 的位置?

不是坐标而是位置。 例子: 这些是 15 个不同的标签,我需要标签的位置“在位置 4 不同”。 结果: 4 点击“不同”标签后

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()

【问题讨论】:

  • 所以,当你说“不是坐标而是位置”时,你的意思是像一个指数而不是物理位置?
  • 您已经导入了 tkinter 三种不同的方式,一种方式两次。您应该只导入一次。

标签: python tkinter window frame


【解决方案1】:

如果你想要一个索引,你需要点击调用的函数来返回索引号。可以使用如下所示的函数闭包来附加它,或者如果您对此更满意,可以使用functools.partial

我已经展示了按行递增的索引。它可以为每个标签或任何需要的东西增加。在创建标签时需要识别标签的标识符,以便在单击时知道它。

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", '
' )  # Use this if each 'line' should 
                                # be on a different line in the text widget. 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    相关资源
    最近更新 更多