Tkinter Popup Documentation
Overview
This module creates a simple graphical interface using customtkinter that allows users to click on specific words within a text box to display a popup. The popup contains information corresponding to the clicked word. The popup will close automatically if the mouse moves outside of its area after a short delay, but can be kept open by moving the mouse back inside.
Key Components
Main Window: A CTk window containing a text box with selectable words.
Popup: A CTkToplevel window that shows when a word is clicked.
Mouse Tracking: The program tracks mouse movements to determine whether to close the popup.
Functions Used
- log(message, color=None)
Description: Logs messages to the console, optionally color-coded.
Parameters:
message: The message string to be logged.
color: Optional; color of the text in the console.
Example:
log("This is an informational message", "green")
2. log_cursor_position()
Description: Logs the current position of the mouse cursor every 2 seconds.
Example:
log_cursor_position() # Starts logging cursor positions
3. on_word_click(event, word)
Description: Called when a word is clicked; creates and displays a popup.
Parameters:
event: The event that triggered the function.
word: The word that was clicked.
Example:
text.tag_bind("word", "", lambda e, w="example": on_word_click(e, w))
4. coordinate_popup_events(event)
Description: Handles mouse motion events to control popup visibility.
Parameters:
event: The event indicating mouse movement.
Example: This function is called when the mouse moves within the popup.
5. log_destroy()
Description: Destroys the popup window and logs this action.
Example:
This function is an internal callback used with the after method for scheduling destruction.
6. make_selectable_and_clickable(text_widget, word)
Description: Makes specified words within a text widget selectable and clickable.
Parameters:
text_widget: The CtkTextBox containing the words.
word: The word to be made selectable.
Example:
make_selectable_and_clickable(text, "example_word")
Example Usage
Here’s how to initialize a simple Tkinter application that leverages the above functions:
import customtkinter as ctk
root = ctk.CTk()
root.title("Popup Example")
root.geometry("500x300")
text = ctk.CTkTextbox(root, wrap="word")
text.pack(expand=True, fill="both", padx=10, pady=10)
text.insert("1.0", "Click on this example_word to see the popup.")
text.configure(state=ctk.DISABLED)
make_selectable_and_clickable(text, "example_word")
root.mainloop()
In this example, when "example_word" is clicked, a popup window will appear above it.
Common Errors and Solutions
- Popup Closes Immediately After Creation
Error: The popup closes right after it is created, regardless of mouse position.
Cause: The timer for destruction might be set immediately upon popup creation, without considering mouse position.
Solution:
Ensure that the destroy_id = popup.after(1500, log_destroy) line is only set within the coordinate_popup_events() function after verifying the mouse's current position.
2. coordinate_popup_events Not Executing
Error: The coordinate_popup_events function only runs when the mouse is within the popup frame.
Cause: The binding of mouse movement events is limited to the frame.
Solution:
Change the binding from the frame to the popup window by using popup.bind('', coordinate_popup_events). This will allow the function to execute for any mouse movement within the entire popup area.
3. Mouse Movement Detection Not Working
Error: Mouse movement is not tracked correctly, causing the popup to remain open even when moved outside.
Cause: If the widget_detection_area logic is flawed or improperly implemented, it may not accurately reflect the mouse's position relative to the popup.
Solution:
Review the logic within coordinate_popup_events to ensure that the mouse coordinates are accurately compared to the popup's width and height. Use frame.winfo_rootx(), frame.winfo_rooty(), and frame dimensions to define the detection area correctly.
4. Popup Doesn't Respect Time Out
Error: The popup is not automatically scheduled for destruction after the specified timeout.
Cause: If the logic to set the timer for destruction is not correctly invoked, the popup will remain open indefinitely.
Solution:
Ensure that the timer for destruction is only set when the popup is actually going to be destroyed, using the if destroy_id is None: condition correctly within the else statement of the coordinate_popup_events function.
5. Popup Appears Off-Screen
Error: The popup window appears outside the visible area of the screen after creation.
Cause: The popup's position is calculated based on the mouse cursor's coordinates, and if the coordinates exceed screen boundaries, the popup will not be entirely visible.
Solution:
Add logic to constrain the popup’s position to ensure it remains within screen boundaries, adjusting popup_x and popup_y as necessary.
Conclusion
By following this documentation and being mindful of the common errors listed above, future developers should be able to maintain, extend, or modify the popup functionality efficiently. For any further inquiries or issues not covered in this documentation, refer to the official customtkinter or Tkinter documentation for additional features and customization options.