1

I have this few lines of code which print the selected content in the Listbox when double clicked but i want to display text beside the cursor like double click the selected content to print to prompt the user to take such action before it can be printed.

I search the listbox documentation and found the is an attribute cursor which you set the cursor type to display when the widget has focus but didn't find something like cursor-text to do that.Is the a way i can work around to achieve that, your suggestions are welcomed.

from tkinter import *


def test(event=None):
    print("woow test works")
    print(l.get(ACTIVE))


root = Tk()

l = Listbox(root, cursor="tcross")
l.pack()
l.insert(END, ("today"),("tomorrow"))
l.bind("<Double-Button-1>", test)

root.mainloop()
AD WAN
  • 1,414
  • 2
  • 15
  • 28
  • You might be able to implement what are called "tooltips" to do something like you want. See [this](https://stackoverflow.com/questions/3221956/how-do-i-display-tooltips-in-tkinter) for example. I think it would be at best very difficult, if not impossible, to do it with a `Listbox` by itself. – martineau Apr 12 '18 at 17:41
  • Does this help? l.focus_set() – toyota Supra Jan 04 '23 at 20:55

2 Answers2

0

I don't quite understand your English, but you can see this page for an example of using tooltips, which make text appear on mouse hover.

Artemis
  • 2,553
  • 7
  • 21
  • 36
0

Yes, it's possible to add specific info or comments to listbox lines using Hovertip from idlelib.tooltip. In the example below, Mark Lutz's customizable scrolled listbox works normally, except that right-clicking on any line opens a tip with info/comments for that line. Ref. : Programming Python 4th ed. Example 9-9

from tkinter import *
from idlelib.tooltip import Hovertip

class ScrolledList(Frame):
    def __init__(self, options, parent=None):
        Frame.__init__(self, parent)    
        self.pack(expand=YES, fill=BOTH)
        botfrm = Frame(parent)
        botfrm.pack(side=BOTTOM)
        Label(botfrm,
            text="Select a line and right-click for info").pack()
        self.makeWidgets(options)
        self.myTip = None

    def handleList(self, event):
        if self.myTip:
            self.myTip.__del__()
        label = self.lstbx.get(ACTIVE)
        self.runCommand(label)

    def overflyLine(self, event):
        if self.myTip:
            self.myTip.__del__()
        self.myTip = Hovertip(self.lstbx,f"Comments for {self.lstbx.get(ACTIVE)}")
        self.myTip.showtip()

    def makeWidgets(self, options):
        sbar = Scrollbar(self)
        list = Listbox(self, relief=SUNKEN, bg='misty rose')
        sbar.config(command=list.yview)
        list.config(yscrollcommand=sbar.set)
        sbar.pack(side=RIGHT, fill=Y)
        list.pack(side=LEFT, expand=YES, fill=BOTH)
        for label in options:
            list.insert(END, label)
        self.lstbx = list
        list.bind('<Button-3>', self.overflyLine)
        list.bind('<Double-1>', self.handleList)
        list.bind('<Return>', self.handleList)

    def runCommand(self, selection):
        print('You selected:', selection)

if __name__ == '__main__':
    options = (('Lumberjack-%s' % x) for  x in range(20))
    scr = ScrolledList(options).mainloop()
mlibrt
  • 21
  • 4