0

The problem I am having is that using customtkinter the event <ButtonPress-1> doesn't seem to work in customtkinter Frames

This can be re-produced with (doesn't work):

from customtkinter import CTk, CTkFrame

root = CTk()
root.geometry('300x300')

frame = CTkFrame(root)
frame.bind('<ButtonPress-1>', lambda _ : print('clicked'))
frame.place(x=100, y=100, width=50, height=50)
root.mainloop()

But this event works

from customtkinter import CTk, CTkFrame

root = CTk()
root.geometry('300x300')

frame = CTkFrame(root)
frame.bind('<Enter>', lambda _ : print('entered'))
frame.place(x=100, y=100, width=50, height=50)
root.mainloop()
  • Are you sure you have clicked the frame? Use a `background`-color and try again. It works fine, at least in `tkinter` and the last time I looked into the sourcecode I did not find a difference between frames in `ctk` and `tkinter`. – Thingamabobs Oct 22 '22 at 12:45
  • I have tried the exact setup with just tkinter and the background option so it can be visible (as you said) before posting the question, this problem doesnt appear with just tkinter just the module – martinosspa Oct 22 '22 at 12:53

3 Answers3

0

- is not allowed in a function in python(3), so you need to change it to ButtonPress1, and not ButtonPress-1.
That's because a function name in python only allows characters a-z, A-Z (uppercase), numbers, and an underscore.

Apart from these restrictions, Python allows Identifiers to be a combination of lowercase letters (a to z) or uppercase letters (A to Z) or digits (0 to 9) or an underscore (_). But variable name must not be started with digits.

- from https://www.geeksforgeeks.org/what-are-the-allowed-characters-in-python-function-names


For more information go to this post:
https://www.stackoverflow.com/questions/19482730/allowed-characters-in-python-function-names
Thijzert
  • 11
  • 4
  • `''` is a string and not a function. – Thingamabobs Oct 22 '22 at 12:37
  • @Thingamabobs but it calles a function – Thijzert Oct 22 '22 at 12:54
  • The string represents an event sequence and is valid for the underling tcl interpreter. – Thingamabobs Oct 22 '22 at 12:56
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – user11717481 Oct 25 '22 at 07:31
0

ctk just overlays your Frame with a canvas to draw the rounded corners. It seems that the developer did not paid attention to the event handling. To solve your issue, you can do:

frame.canvas.bind('<ButtonPress-1>', lambda _ : print('clicked'))

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
0

The event bindings will be added in the next CustomTkinter version 5.0.0. At the moment they are always called on the underlying tkinter.Frame object, which is always covered by a canvas. So click events will not work at the moment.

Tom
  • 723
  • 1
  • 7
  • 16