CTkMenuBar icon indicating copy to clipboard operation
CTkMenuBar copied to clipboard

What correctly calls Cascade does not call option in CustomDropdownMenu

Open limafresh opened this issue 1 year ago • 5 comments

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.

limafresh avatar Oct 02 '24 14:10 limafresh

@l1mafresh I tested your example but I am not facing any issue while drawing the rectangle or oval after click on those buttons. See this video below:

https://github.com/user-attachments/assets/54087897-2ea4-497c-860c-973c26497efa

Can you specify your OS? Make sure you are using the latest version of CTkMenubar.

Akascape avatar Oct 02 '24 14:10 Akascape

@l1mafreshЯ перевірив ваш приклад, але не зіткнувся з проблемами під час малювання прямокутника чи овалу після натискання цих кнопок. Перегляньте це відео нижче:

Запис.2024-10-02.200118.mp4 Чи можете ви вказати свою ОС? Переконайтеся, що ви використовуєте останню версію CTkMenubar.

host@antix1:~
$ pip show CTkMenuBar
Name: CTkMenuBar
Version: 0.8
Summary: Customtkinter Menu Widget
Home-page: https://github.com/Akascape/CTkMenuBar
Author: Akash Bora
Author-email: 
License: Creative Commons Zero v1.0 Universal
Location: /home/host/.local/lib/python3.11/site-packages
Requires: customtkinter
Required-by: 

OS: Antix Linux (based on Debian 12)

limafresh avatar Oct 02 '24 15:10 limafresh

@l1mafresh I tested your example but I am not facing any issue while drawing the rectangle or oval after click on those buttons. See this video below:

Recording.2024-10-02.200118.mp4 Can you specify your OS? Make sure you are using the latest version of CTkMenubar.

Repl.It has the same mistake, tried it there too

limafresh avatar Oct 03 '24 05:10 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)

        self.after(100) # SMALL DELAY
        
app = PaintShape()
app.mainloop()

The problem was solved by adding a small delay to the function.

limafresh avatar Oct 04 '24 18:10 limafresh

There are really no problems on Windows, I tried. But there is such a problem on Linux. And the delay is not a solution, but rather a temporary solution. It created some more problems. If someone solves what the problem is, it will be good. Probably something with bind

limafresh avatar Mar 18 '25 16:03 limafresh