Skip to content

What correctly calls Cascade does not call option in CustomDropdownMenu #33

@limafresh

Description

@limafresh
from customtkinter import *
from CTkMenuBar import CTkMenuBar, CustomDropdownMenu
from PIL import Image, ImageDraw

class PaintShape(CTk):
    def __init__(self):
        super().__init__()
        
        menu = CTkMenuBar(self)
        
        add_shape = menu.add_cascade("Add Shape")
        dropdown = CustomDropdownMenu(widget=add_shape)
        dropdown.add_option(option="Rectangle", command=lambda: self.create_shape("rectangle"))
        dropdown.add_option(option="Oval", command=lambda: self.create_shape("oval"))

        rectangle_menu = menu.add_cascade("Rectangle", command=lambda: self.create_shape("rectangle"))
        oval_menu = menu.add_cascade("Oval", command=lambda: self.create_shape("oval"))

        self.canvas = CTkCanvas(self, bg="white")
        self.canvas.pack(fill=BOTH, expand=True)
        
        self.color = "black"
        self.image = Image.new("RGB", (800, 600), "white")
        self.draw = ImageDraw.Draw(self.image)
        self.photo = None
        
    def create_shape(self, shape):
        def start_shape(event):
            self.shape_start_x = event.x
            self.shape_start_y = event.y
            if self.shape == "rectangle":
                self.shape_id = self.canvas.create_rectangle(self.shape_start_x, self.shape_start_y, self.shape_start_x, self.shape_start_y, outline=self.color)
            elif self.shape == "oval":
                self.shape_id = self.canvas.create_oval(self.shape_start_x, self.shape_start_y, self.shape_start_x, self.shape_start_y, outline=self.color)

        def draw_shape(event):
            self.canvas.coords(self.shape_id, self.shape_start_x, self.shape_start_y, event.x, event.y)

        def end_shape(event):
            self.canvas.unbind("<ButtonPress-1>")
            self.canvas.unbind("<B1-Motion>")
            self.canvas.unbind("<ButtonRelease-1>")
            print("Hello world!")
        
        self.shape = shape
        self.canvas.bind("<ButtonPress-1>", start_shape)
        self.canvas.bind("<B1-Motion>", draw_shape)
        self.canvas.bind("<ButtonRelease-1>", end_shape)
        
app = PaintShape()
app.mainloop()

When I click, for example, on Rectangle as a separate Cascade, the rectangle is drawn, but when I click on the Rectangle option in the CustomDropdownMenu called add_shape - "Hello world!" is printed, but the shape cannot be drawn. With oval or other shapes it's the same.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions