Search code examples
pythontkintertexttagshighlight

Tkinter : Text highlight appears while pressed but disappear next


I've been working on a Tkinter (Python) project that displays a list of strings using the Text widget recently, but I ran into an issue I couldn't manage to solve : On startup, I want the first line to be highlighted, and when I click on up/down arrows, the highlight goes up/down, as a selection bar. I succeed to do that, but the problem is that the highlight only appears when arrows are pressed, and when they are released, it disappear. I'd like it to stay even when I'm not pressing any key.

Here is my code :

class Ui:
  def __init__(self):
    # the list I want to display in Text
    self.repos = repos

    # here is the entry keys are bind to
    self.entry = Entry(root)
    self.entry.pack()
    self.bind('<Up>', lambda i: self.changeIndex(-1))
    self.bind('<Down>', lambda i: self.changeIndex(1))

    # here is the Text widget
    self.lists = Text(root, state=NORMAL)
    self.lists.pack()
    
    # inits Text value
    for i in self.repos:
      self.lists.insert('insert', i + '\n')
    self.lists['state'] = DISABLED

    # variable I use to navigate with highlight
    self.index = 0
    self.lists.tag_add('curr', str(self.index) + '.0', str(self.index + 1) + '.0') # added + '.0' to make it look '0.0' instead of '0'
    self.lists.tag_config('curr', background='#70fffa', background='#000000')

    self.root.mainloop()

  def changeIndex(self, n):
    # error gestion (if < 0 or > len(repos), return)
    self.lists.tag_delete('curr')
    self.lists.tag_add('curr', str(self.index) + '.0', str(self.index + 1) + '.0')
    self.index = self.index + n
    # to make it scroll if cannot see :
    self.lists.see(str(self.index) + '.0')

I haven't seen any similar problem on Stack, so I asked, but do not hesitate to tell me if it is a duplicate.

Do you guys could help me please ? Thanks !

EDIT: Here is the full code if you want to give it a try : https://github.com/EvanKoe/stack_tkinter.git

EDIT : I added the main.py file (the """backend""" file that calls ui.py) to the demo repository. This way, you'll be able to run the project (be careful, there are "YOUR TOKEN" and "YOUR ORGANIZATION" strings in main.py you'll have to modify with your own token/organization. I couldn't push mine or Github would've asked me to delete my token)


Solution

  • I finally managed to solve the problem, and it was due to my event bindings. I made the decision (to improve the UX) to bind up/down arrows on the top Entry instead of binding em on the Text widget. I bind 4 events :

    • Up arrow => move highlight up,
    • Down arrow => move highlight down,
    • Return key => calls get_name(), a function that returns the selected option,
    • Any other Key => calls repo_filter(), a function that updates the displayed options in the Text widget, according to what has been typed in the Entry.

    The problem was that pressing the up/down arrow was triggering "up/down key" event AND "any other key" event, so the highlight was removed since the Text value was refreshed.

    To solve this problem, I just had to verify that the pressed key was neither up nor down arrow in the "any other key" event callback :

    def repo_filter(evt):
      if evt.keysym == 'Up' or evt.keysym == 'Down': # verify that pressed key
        return                                       # isn't '<Down>' or '<Up>'
      # filter Text widget
    

    Also, I am sorry I didn't give you all the code at the beginning, because, indeed you couldn't guess about those event bindings.

    Thanks to everyone who tried to help me !