Advertisement
here2share

# tk_power_listbox.py

Dec 11th, 2022
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. # tk_power_listbox.py
  2.  
  3. from tkinter import *
  4. from random import shuffle
  5. root = Tk()
  6.  
  7. root.title("tk_power_listbox")
  8.  
  9. menu_options = list(range(300))
  10.  
  11. frame = Frame(root)
  12. frame.pack()
  13.  
  14. entry = Entry(frame)
  15. entry.pack(side=LEFT)
  16.  
  17. button = Button(frame, text="Click Me")
  18. button.pack(side=LEFT)
  19.  
  20. label = Label(frame, text="Hello Tkinter")
  21. label.pack(side=LEFT)
  22.  
  23. listbox = Listbox(frame, height=1, bg='yellow')
  24. for option in menu_options:
  25.     listbox.insert(END, option)
  26.     listbox.pack(side=LEFT)
  27.    
  28. listbox.activate(10)
  29. listbox.selection_set(10)
  30. selection = listbox.curselection()[0]
  31.  
  32. def on_active(event):
  33.     global selected_index
  34.     listbox2.itemconfig(selected_index, bg='white')
  35.     selected_index = listbox2.nearest(event.y)
  36.     listbox2.itemconfig(selected_index, bg='yellow')
  37.  
  38. def on_select(event):
  39.     global selection
  40.     try:
  41.         selection = listbox2.curselection()[0]
  42.         listbox2.activate(selection)
  43.         listbox2.selection_set(selection)
  44.     except: 0
  45.  
  46. srceen_h = 564 # root.winfo_screenheight():
  47. def on_enter(event):
  48.     global root2, listbox2, selected_index
  49.     try:
  50.         root2.destroy()
  51.     except: 0
  52.     selected_index = 0
  53.     root2 = Toplevel()
  54.     root2.overrideredirect(True)
  55.     Lmenu = min(40, len(menu_options))
  56.     Lb2 = Lmenu * 14
  57.     listbox2 = Listbox(root2, height=Lmenu)
  58.     yyy = root.winfo_y() + 20
  59.     if yyy + Lb2 > srceen_h:
  60.         yyy = srceen_h - Lb2
  61.     root2.geometry("+{}+{}".format(listbox.winfo_x()+root.winfo_x()+8, yyy))
  62.     for option in menu_options:
  63.         listbox2.insert(END, option)
  64.        
  65.     listbox2.activate(selection)
  66.     listbox2.selection_set(selection)
  67.  
  68.     scrollbar = Scrollbar(root2, orient=VERTICAL, command=listbox2.yview)
  69.     scrollbar.pack(side=RIGHT, fill=Y)
  70.     scrollbar.config(width=40)
  71.     listbox2.config(yscrollcommand=scrollbar.set)
  72.    
  73.     root2.bind("<Motion>", on_active)
  74.     root2.bind("<Leave>", on_leave)
  75.     listbox2.bind("<<ListboxSelect>>", on_select)
  76.     listbox2.pack()
  77.  
  78. def on_leave(event):
  79.     global root2
  80.     x = root.winfo_pointerx()
  81.     geom = root2.geometry()
  82.     w = int(geom.split("x")[0])
  83.     x2 = int(geom.split("+")[1])
  84.     if x > x2 + w - 50 and x < x2 + w:
  85.         return
  86.     listbox.activate(selection)
  87.     listbox.selection_set(selection)
  88.     root2.destroy()
  89.    
  90. listbox.bind("<Enter>", on_enter)
  91.  
  92. checkbox = Checkbutton(frame, text="Check Me")
  93. checkbox.pack(side=LEFT)
  94.  
  95. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement