Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

JanPanthera
Copy link

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.

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`.
@Anonymous6598
Copy link

It will be great idea

This commit addresses a bug where the CTkEntry widget was unable to lose focus when clicked outside. The `_on_root_click` method has been updated to only set the focus to the root window if the CTkEntry widget currently has the focus. This prevents the focus from being set to the root window when clicking on another widget that should receive the focus. Exception handling has also been improved for better error resilience.
This commit addresses a bug where the CTkEntry widget was unable to lose focus when clicked outside. The `_on_root_click` method has been updated to only set the focus to the root window if the CTkEntry widget currently has the focus. This prevents the focus from being set to the root window when clicking on another widget that should receive the focus. Exception handling has also been improved for better error resilience.
@Anonymous6598
Copy link

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

@JanPanthera
Copy link
Author

i will take a look :-)

This commit introduces a new feature to the CTkTextbox 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 CTkTextbox 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 CTkTextbox widget and shifts focus to the root window to effectively remove focus from the CTkTextbox .
- Implemented `_is_click_inside` method to determine if a given click event is within the bounds of the CTkTextbox 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 JanPanthera changed the title Implement Toggleable Focus-Loss for CTkEntry on External Click Implement Toggleable Focus-Loss for CTkEntry and CTkTextbox on External Click Feb 2, 2024
- Created a standalone function `handle_root_click` that encapsulates the logic for handling root window and click events.
- This function takes a widget and an event as parameters, and an optional specific widget to check for focus.
- Implemented this function in the `CTkEntry` and 'CtkTextbox' class, creating a new method `on_root_click` that calls `handle_root_click`.
- Bound the root click event to the `on_root_click` method in the `CTkEntry` and 'CtkTextbox' class.
- This change allows for better code reusability and separation of concerns.
return widget


def handle_root_click(widget, event, specific_widget=None):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That method was needed a long time ago. Since the beginning.

@@ -20,3 +20,39 @@ def check_kwargs_empty(kwargs_dict, raise_error=False) -> bool:
return True
else:
return False


def get_root_window(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same goes for this method

… method in the `CTkTabview` class and the addition of code to update the `_current_name` attribute. These changes ensure that the tab renaming process is more efficient and that the connection to the frame is maintained even when a tab is renamed.

Changes:
1. The renaming method in the `CTkTabview` class has been altered. Instead of removing the old name from the `_name_list` and appending the new name at the end, the new name now replaces the old name at the same index in the `_name_list`. This change makes the renaming process more efficient.
2. Code has been added to update the `_current_name` attribute if the old name matches the current name. This is a crucial change as it ensures that the connection to the frame is not lost when a tab is renamed.
@VillainousSsnake
Copy link

@JanPanthera this does not work for toplevel windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants