【问题标题】:How to stop tab from selecting text on Tkinter EntryBox如何停止选项卡在 Tkinter 输入框上选择文本
【发布时间】:2016-07-28 14:39:52
【问题描述】:

我正在使用AutocompleteEntry 上的这段代码,为 Tkinter 附带的 Entry 小部件创建一个子类。

在第 57 行,handle_keyrelease() 函数似乎处理 AutocompleteEntry 对特定按键的反应:

def handle_keyrelease(self, event):
    """event handler for the keyrelease event on this widget"""
    if event.keysym == "BackSpace":
        self.delete(self.index(Tkinter.INSERT), Tkinter.END) 
        self.position = self.index(Tkinter.END)
    if event.keysym == "Left":
        if self.position < self.index(Tkinter.END): # delete the selection
            self.delete(self.position, Tkinter.END)
        else:
            self.position = self.position-1 # delete one character
            self.delete(self.position, Tkinter.END)
    if event.keysym == "Right":
        self.position = self.index(Tkinter.END) # go to end (no selection)
    if event.keysym == "Down":
        self.autocomplete(1) # cycle to next hit
    if event.keysym == "Up":
        self.autocomplete(-1) # cycle to previous hit
    # perform normal autocomplete if event is a single key or an umlaut
    if len(event.keysym) == 1 or event.keysym in tkinter_umlauts:
        self.autocomplete()

并且右键设置为我想要的,完成我输入的第一个单词并跳到它的末尾,我的问题是以下,我想将右键更改为 Tab 键,但是我的输入框上的 tab 键选择了所有文本,我找不到改变这种行为的方法,有没有办法?

这是我创建输入框以供参考的代码部分,对此感到抱歉:

from tkinter import *
import entryautocomplete as eac

if __name__ == '__main__':
    # create Tkinter window
    master = Tk()
    # change the window name
    master.title('Jarbas')
    # avoids resizing of the window
    master.resizable(width=False, height=False)
    # center top the window on my computer
    master.geometry('+400+0')
    # adds an icon
    img = Image("photo", file="jarbas.png")
    master.tk.call('wm', 'iconphoto', master._w, img)

    # create the entry frame
    uinput = Frame(master)
    # create the other frame
    resultado = LabelFrame(
        master, text='###', labelanchor='n', font='arial 12', relief='flat')

    # places the two frames on the window
    uinput.grid()
    resultado.grid()

    # place a label on the Entry frame, picked random from a list
    Label(uinput, text=ola[randint(0, len(ola) - 1)]).grid()
    # Creates the entry
    texto = eac.AutocompleteEntry(
        uinput, font='arial 14 bold', width='60', takefocus='off')
    texto.grid(padx=5, pady=4)
    texto.set_completion_list(comandos)

    # calls the function 'get_input' once you press Return on the Entry box
    # the function reads what is typed and does what it should do
    texto.bind('<Return>', get_input)

    # tkinter main loop
    mainloop()

【问题讨论】:

    标签: python python-3.x tkinter tk


    【解决方案1】:

    为了进一步参考,基于this question,我设法通过简单地将bind 添加到标签调用returns break 的函数来使其工作,如下所示:

    def tab_handler(event):
        return 'break'
    
    entry.bind('<Tab>', tab_handler)
    

    只需将 EntryAutoComplete 文件中的 if event.keysym == "Right": 更改为 if event.keysym == "Tab":

    效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 2013-08-27
      • 2015-05-27
      • 1970-01-01
      相关资源
      最近更新 更多