2022-04-05 23:55:22 +03:00
|
|
|
import customtkinter
|
|
|
|
import tkinter
|
2022-05-25 19:37:55 +03:00
|
|
|
import sys
|
2022-04-05 23:55:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
class CTkMenu(tkinter.Toplevel):
|
|
|
|
def __init__(self, master, x, y, options):
|
2022-05-24 14:50:34 +03:00
|
|
|
super().__init__()
|
2022-04-05 23:55:22 +03:00
|
|
|
|
2022-05-24 14:50:34 +03:00
|
|
|
self.overrideredirect(True)
|
|
|
|
self.geometry(f"120x{len(options) * (25 + 3) + 3}+{x}+{y}")
|
|
|
|
|
2022-05-25 19:37:55 +03:00
|
|
|
if sys.platform.startswith("darwin"):
|
|
|
|
self.overrideredirect(False)
|
|
|
|
self.wm_attributes("-transparent", True) # turn off shadow
|
|
|
|
self.config(bg='systemTransparent') # transparent bg
|
|
|
|
self.frame = customtkinter.CTkFrame(self, border_width=0, width=120, corner_radius=6, border_color="gray4", fg_color="#333740")
|
|
|
|
elif sys.platform.startswith("win"):
|
|
|
|
self.configure(bg="#FFFFF1")
|
|
|
|
self.wm_attributes("-transparent", "#FFFFF1")
|
|
|
|
self.focus()
|
|
|
|
self.frame = customtkinter.CTkFrame(self, border_width=0, width=120, corner_radius=6, border_color="gray4", fg_color="#333740",
|
|
|
|
overwrite_preferred_drawing_method="circle_shapes")
|
|
|
|
else:
|
|
|
|
self.configure(bg="#FFFFF1")
|
|
|
|
self.wm_attributes("-transparent", "#FFFFF1")
|
|
|
|
self.frame = customtkinter.CTkFrame(self, border_width=0, width=120, corner_radius=6, border_color="gray4", fg_color="#333740",
|
|
|
|
overwrite_preferred_drawing_method="circle_shapes")
|
|
|
|
|
2022-05-24 14:50:34 +03:00
|
|
|
self.frame.grid(row=0, column=0, sticky="nsew", rowspan=len(options) + 1, columnspan=1, ipadx=0, ipady=0)
|
|
|
|
|
|
|
|
self.frame.grid_rowconfigure(len(options) + 1, minsize=3)
|
|
|
|
self.frame.grid_columnconfigure(0, weight=1)
|
2022-04-05 23:55:22 +03:00
|
|
|
self.grid_columnconfigure(0, weight=1)
|
|
|
|
|
|
|
|
self.buttons = []
|
|
|
|
for index, option in enumerate(options):
|
2022-05-24 14:50:34 +03:00
|
|
|
button = customtkinter.CTkButton(self.frame, text=option, height=25, width=108, fg_color="#333740", text_color="gray74",
|
|
|
|
hover_color="gray28", corner_radius=4, command=self.button_click)
|
2022-04-05 23:55:22 +03:00
|
|
|
button.text_label.grid(row=0, column=0, rowspan=2, columnspan=2, sticky="w")
|
2022-05-24 14:50:34 +03:00
|
|
|
button.grid(row=index, column=0, padx=(3, 3), pady=(3, 0), columnspan=1, rowspan=1, sticky="ew")
|
2022-04-05 23:55:22 +03:00
|
|
|
self.buttons.append(button)
|
|
|
|
|
2022-05-25 19:37:55 +03:00
|
|
|
self.bind("<FocusOut>", self.focus_loss_event)
|
|
|
|
self.frame.canvas.bind("<Button-1>", self.focus_loss_event)
|
|
|
|
|
2022-05-24 14:50:34 +03:00
|
|
|
def focus_loss_event(self, event):
|
|
|
|
print("focus loss")
|
|
|
|
self.destroy()
|
2022-05-25 19:37:55 +03:00
|
|
|
# self.update()
|
2022-05-24 14:50:34 +03:00
|
|
|
|
|
|
|
def button_click(self):
|
2022-05-25 19:37:55 +03:00
|
|
|
print("button press")
|
2022-05-24 14:50:34 +03:00
|
|
|
self.destroy()
|
2022-05-25 19:37:55 +03:00
|
|
|
# self.update()
|
2022-04-05 23:55:22 +03:00
|
|
|
|
|
|
|
|
2022-05-24 14:50:34 +03:00
|
|
|
app = customtkinter.CTk()
|
|
|
|
app.geometry("600x500")
|
2022-04-05 23:55:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
def open_menu():
|
2022-05-22 21:26:31 +03:00
|
|
|
menu = CTkMenu(app, button.winfo_rootx(), button.winfo_rooty() + button.winfo_height() + 4, ["Option 1", "Option 2", "Point 3"])
|
2022-04-05 23:55:22 +03:00
|
|
|
|
2022-05-24 14:50:34 +03:00
|
|
|
button = customtkinter.CTkButton(command=open_menu, height=30, corner_radius=6)
|
2022-04-05 23:55:22 +03:00
|
|
|
button.pack(pady=20)
|
|
|
|
|
2022-05-25 19:37:55 +03:00
|
|
|
button_2 = customtkinter.CTkButton(command=open_menu, height=30, corner_radius=6)
|
|
|
|
button_2.pack(pady=60)
|
|
|
|
|
2022-05-22 21:26:31 +03:00
|
|
|
app.mainloop()
|