我使用 window_create 在 Text 元素中创建交互式按钮。按钮代表随机或静态值,我希望能够编译文本元素的内容并用它们各自的值替换按钮。但是,我找不到任何按钮的位置。
我试过self.text.get("1.0",tk.END)
了,但它只返回文本,不包括按钮元素
按钮元素的创建方式如下:
btn_text = tk.StringVar()
value = StaticValue('static', btn_text, self.custom_val_veiwer, idx)
button = tk.Button(self.text,
textvariable=btn_text, command=lambda v=value:
self.veiw_custom_val(None, val=v))
btn_text.set('static')
self.custom_vals.append(value)
self.text.window_create(tk.INSERT, window=button)
编辑:如果您想重新创建问题,请使用:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
text.pack()
text.insert(tk.END, 'before button')
button = tk.Button(text, text='button')
text.window_create(tk.END, window=button)
text.insert(tk.END, 'after button')
print(text.get("1.0",tk.END))
root.mainloop()
注意按钮在文本字段中的显示方式,但没有打印出来
(输出是 before buttonafter button
我想要类似的东西before button<button>after button
或一个函数,它会告诉我在索引 y 处的 x 行有一个按钮)