Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_power_listbox.py
- from tkinter import *
- root = Tk()
- root.title("tk_power_listbox")
- menu_options = list(range(300))
- frame = Frame(root)
- frame.pack()
- label = Label(frame, text="Hello Tkinter")
- label.pack(side=LEFT)
- text_box = Text(frame, height=1, bg='yellow')
- text_box.pack(side=LEFT, padx=3, pady=3)
- def on_active(event):
- global selected_index
- listbox2.itemconfig(selected_index, bg='white')
- selected_index = listbox2.nearest(event.y)
- listbox2.itemconfig(selected_index, bg='yellow')
- def on_select(event):
- selection = listbox2.curselection()[0]
- text_box.delete('1.0', END) # Clear the previous text
- text_box.insert(END, str(menu_options[selection])) # Insert the new text
- root2.destroy()
- screen_h = root.winfo_screenheight()
- def on_enter(event):
- global root2, listbox2, selected_index
- try:
- root2.destroy()
- except:
- pass
- selected_index = 0
- root2 = Toplevel()
- root2.overrideredirect(True)
- Lmenu = min(30, len(menu_options))
- Lb2 = Lmenu * 14
- listbox2 = Listbox(root2, height=Lmenu)
- yyy = root.winfo_y() + 20
- if yyy + Lb2 > screen_h:
- yyy = screen_h - Lb2
- root2.geometry("+{}+{}".format(text_box.winfo_x() + root.winfo_x() + 8, yyy))
- for option in menu_options:
- listbox2.insert(END, option)
- scrollbar = Scrollbar(root2, orient=VERTICAL, command=listbox2.yview)
- scrollbar.pack(side=RIGHT, fill=Y)
- scrollbar.config(width=40)
- listbox2.config(yscrollcommand=scrollbar.set)
- root2.bind("<Motion>", on_active)
- root2.bind("<Leave>", on_leave)
- listbox2.bind("<<ListboxSelect>>", on_select)
- listbox2.pack()
- def on_leave(event):
- x = root.winfo_pointerx()
- geom = root2.geometry()
- x2y2, w, h = geom.split("+")
- x2, y2, w, h = [int(i) for i in x2y2.split("x") + [w, h]]
- if (x < x2 + w - 50 or x > x2 + w):
- root2.destroy()
- # destroys if outside of top or bottom automaticallY? nice
- text_box.bind("<Button-1>", on_enter) # Bind on_enter to left mouse click
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement