【问题标题】:tkinter Entry widget detect if there is any selectiontkinter Entry 小部件检测是否有任何选择
【发布时间】:2021-07-16 23:07:49
【问题描述】:

有什么方法可以在 tkinter 中检测 Entry 小部件中的选择,就像我们在 Text 小部件 txt.get(SEL_FIRST, SEL_LAST) 和 txt.tag_ranges("sel") 中得到的一样

【问题讨论】:

    标签: tkinter selection


    【解决方案1】:

    如果您只想检查选择是否存在,您可以使用Entry.select_present() 方法,如果选择存在则返回True

    如果您想获得选择,可以使用Entry.selection_get()

    小例子:

    from tkinter import *
    
    def selection_present():
        print(entry.select_present())
        if entry.select_present():
            print(entry.selection_get())
    
    root = Tk()
    
    entry = Entry(root)
    entry.pack()
    
    Button(root, text="Check selection", command=selection_present).pack()
    
    root.mainloop()
    

    【讨论】:

    • 非常感谢,Entry.select_present() 正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 2012-04-06
    • 1970-01-01
    • 2012-06-27
    相关资源
    最近更新 更多