gpt4 book ai didi

python - 如何从 Tkinter 文本框中获取选定的字符串?

转载 作者:太空狗 更新时间:2023-10-29 20:38:22 25 4
gpt4 key购买 nike

我希望能够从文本的突出显示部分中获取字符串,我该怎么做?

最佳答案

对于 Tkinter.TextTkinter.Entry 小部件,您可以使用:

content = text.selection_get()

从 Python 控制台中的 Entry 和 Text 小部件获取选定文本的交互式示例:

>>> import Tkinter
>>> w = Tkinter.Tk()
>>> e = Tkinter.Entry(w)
>>> e.pack()
>>> t = Tkinter.Text(w)
>>> t.pack()
#(scribble at the widgets in the created window, and select text in the Entry)
>>> e.selection_get()
'1234'
#(select text)
>>> t.selection_get()
'1234'
>>>

根据 Bryan Oakley 的评论:

selection_get is a generic widget method available to all widgets. It does not always get the selected string from a Text widget. It might, but it's not guaranteed. What it does is get what's called the "X" selection (from tk's X11 roots). If you set exportselection to False for the Text widget, selection_get will fail to work. So your suggest will work in the normal case, but not in all cases.

关于python - 如何从 Tkinter 文本框中获取选定的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4073468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com