如何使用python检测选定的文本?

时间:2019-07-01 08:35:02

标签: python python-3.x tkinter operating-system copy-paste

我正在寻找一种在An error occurred while loading the map layer 'default': Postgis Plugin: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? Connection string: ' dbname=gis connect_timeout=4' encountered during parsing of layer 'landcover-low-zoom' in Layer at line 755 of '/home/betrieb /src/openstreetmap-carto/mapnik.xml' 中复制所选文本并将其自动粘贴到tkinter.scrolledtext中的方法。

意味着,每次用户选择文本时,它将自动添加到Entry中。

有什么建议吗?专门介绍如何获取所选文本。

1 个答案:

答案 0 :(得分:1)

您可以使用从selection小部件生成的text事件添加到您的entry小部件。

import tkinter as tk
from tkinter import scrolledtext as tkst

root = tk.Tk()

txt = tkst.ScrolledText(root)
txt.insert(tk.END, "This is a test phrase")
txt.pack()

entry = tk.Entry(root)
entry.pack()

def select_event(event):
    try:
        entry.delete(0, tk.END)
        entry.insert(0, txt.get(tk.SEL_FIRST, tk.SEL_LAST))
    except tk.TclError:
        pass

txt.bind("<<Selection>>", select_event)

root.mainloop()