CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

Implement Toggleable Focus-Loss for CTkEntry and CTkTextbox on External Click

Open JanPanthera opened this issue 1 year ago • 4 comments

This commit introduces a new feature to the CTkEntry widget that allows it to lose focus when the user clicks outside of it. The functionality is designed to be toggleable, giving developers the flexibility to enable or disable this behavior based on their application's requirements.

Key Changes:

  • Added a focus_loss_outside_click parameter to the __init__ method of the CTkEntry class, enabling the focus-loss feature by default.
  • Implemented _get_root_window method to traverse the widget hierarchy and find the root window, ensuring compatibility with various application structures.
  • Implemented _on_root_click method, which checks if a mouse click occurred outside the CTkEntry widget and shifts focus to the root window to effectively remove focus from the CTkEntry.
  • Implemented _is_click_inside method to determine if a given click event is within the bounds of the CTkEntry widget, aiding the focus management logic.
  • Adjusted _create_bindings method to include binding for root window clicks if not disabled by focus_loss_outside_click.

JanPanthera avatar Feb 02 '24 12:02 JanPanthera

It will be great idea

Anonymous6598 avatar Feb 02 '24 16:02 Anonymous6598

Sometimes it can be really annoying, when entry widget always stays active. You should also add it to CTkTexbox.

Anonymous6598 avatar Feb 02 '24 18:02 Anonymous6598

i will take a look :-)

JanPanthera avatar Feb 02 '24 18:02 JanPanthera

@JanPanthera this does not work for toplevel windows.

VillainousSsnake avatar May 13 '24 00:05 VillainousSsnake