简体   繁体   English

如何阻止选项卡从Tkinter EntryBox上选择文本

[英]How to stop tab from selecting text on Tkinter EntryBox

I'm using this code avaliable on AutocompleteEntry , to create a subclass for the Entry widget that ships with Tkinter. 我正在AutocompleteEntry上使用此代码,以为Tkinter附带的Entry小部件创建一个子类。

At line 57, the handle_keyrelease() function seems to handle how te AutocompleteEntry reacts to certain keypress: 在第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()

And the Right key is set to do what I want, complete the first word I type in and skip to the end of it, my problem is the following, I want to change the Right key for the Tab key, but the tab key on my entry box selects all the text, and I couldn't find a way to change this behaviour, is there a way? 并且右键设置为执行我想要的操作,完成输入的第一个单词,然后跳到结尾,我的问题是以下内容,我想更改Tab键的Right键,但是Tab键在我的输入框选择了所有文本,但我找不到改变这种行为的方法,有办法吗?

And here's the part of my code where I create my entry box for reference, sorry for that: 这是我的代码部分,在其中创建了我的输入框以供参考,对此感到抱歉:

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()

For further reference, based on this question , I managed to get it working by simply adding a bind to tab calling a function that return s break , like so: 为了进一步参考,基于这个问题 ,我设法通过简单地将一个bind添加到选项卡上来调用return s break的函数,使它正常工作,如下所示:

def tab_handler(event):
    return 'break'

entry.bind('<Tab>', tab_handler)

and simply changed the if event.keysym == "Right": on the EntryAutoComplete file to if event.keysym == "Tab": 并简单地将EntryAutoComplete文件上的if event.keysym == "Right":更改为if event.keysym == "Tab":

Worked great. 很棒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM