Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_power_listbox.py
- from tkinter import *
- from random import shuffle
- root = Tk()
- root.title("tk_power_listbox")
- menu_options = list(range(300))
- frame = Frame(root)
- frame.pack()
- entry = Entry(frame)
- entry.pack(side=LEFT)
- button = Button(frame, text="Click Me")
- button.pack(side=LEFT)
- label = Label(frame, text="Hello Tkinter")
- label.pack(side=LEFT)
- listbox = Listbox(frame, height=1, bg='yellow')
- for option in menu_options:
- listbox.insert(END, option)
- listbox.pack(side=LEFT)
- listbox.activate(10)
- listbox.selection_set(10)
- selection = listbox.curselection()[0]
- 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):
- global selection
- try:
- selection = listbox2.curselection()[0]
- listbox2.activate(selection)
- listbox2.selection_set(selection)
- except: 0
- srceen_h = 564 # root.winfo_screenheight():
- def on_enter(event):
- global root2, listbox2, selected_index
- try:
- root2.destroy()
- except: 0
- selected_index = 0
- root2 = Toplevel()
- root2.overrideredirect(True)
- Lmenu = min(40, len(menu_options))
- Lb2 = Lmenu * 14
- listbox2 = Listbox(root2, height=Lmenu)
- yyy = root.winfo_y() + 20
- if yyy + Lb2 > srceen_h:
- yyy = srceen_h - Lb2
- root2.geometry("+{}+{}".format(listbox.winfo_x()+root.winfo_x()+8, yyy))
- for option in menu_options:
- listbox2.insert(END, option)
- listbox2.activate(selection)
- listbox2.selection_set(selection)
- 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):
- global root2
- x = root.winfo_pointerx()
- geom = root2.geometry()
- w = int(geom.split("x")[0])
- x2 = int(geom.split("+")[1])
- if x > x2 + w - 50 and x < x2 + w:
- return
- listbox.activate(selection)
- listbox.selection_set(selection)
- root2.destroy()
- listbox.bind("<Enter>", on_enter)
- checkbox = Checkbutton(frame, text="Check Me")
- checkbox.pack(side=LEFT)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement