command=handler #debugger skips tkinter button handler functions
# command=lambda: nseOptionsAnalysis_lbl(btn_nseOptionsAnalysis_label)
)
How to get pycharm to hit the debug points inside the Tkinter Button object?
welcome to the forum. I am reviewing your script and it appears a bit on the unbalanced side.
From what I gather, you are attempting to create a button (i.e., btn_nseOptionsAnalysis) with an accompanying label (i.e., btn_nseOptionsAnalysis_label). You have defined the callback function for the button as handler, that when pressed, further calls the function nseOptionsAnalysis_lbl which then uses the label configuration object as its argument. This is a bit confusing. Perhaps this is some higher level stuff that I haven’t yet considered or come across.
Can you please describe or further elaborate what it is exactly that you are attemping to create with tkinter (describe the GUI as to how it should appear and what it is that it is expected to accomplish when the button is pressed).
The below code is simplfied to highlight the issue of pycharm debugger skipping the breakpoint at the Button’s handler function .How to get the degugger to hit the breakpoint within handler()?
import tkinter as tk
from time import strftime
def nseOptionsAnalysis_lbl(label_target):
print("Executing: nseOptionsAnalysis_lbl")
current_time = strftime('%H:%M:%S')
label_target.config(text=f"Last Click: {current_time} Button clicked! Entered nseOptionsAnalysis_lbl()...")
print("Last Click: {current_time} Button clicked! Entered nseOptionsAnalysis_lbl()...")
def create_main_ui():
root = tk.Tk()
root.title("Debugger Test")
root.geometry("300x200")
# The label we want to update
status_label = tk.Label(root, text="Waiting for click...", font=('Calibri', 12))
status_label.pack(pady=20)
def handler():
"""This is the wrapper function for the button command."""
print("Button clicked! Entering handler...")
nseOptionsAnalysis_lbl(status_label)
# The button that triggers the logic
test_btn = tk.Button(
root,
text="Trigger Analysis",
command=handler
)
test_btn.pack(pady=10)
root.mainloop()
if __name__ == "__main__":
create_main_ui()
I tested the latest script that you provided using PyCharm. It is working as expected. You need to press the Trigger Analysis button in order for the debugger to stop at this line. When stepping through code that includes functions, the debugger does not automatically step through functions and their body of statements. Functions have to be explicitly called in the script. When you assign the handler() function to the button via this line:
command=handler
it doesn’t make a function call to it. It merely makes an assignment that in the event that the button is pressed, a call will be made to this function. To test this, place an explicit function call as shown here during the setup configuration code:
test_btn.pack(pady=10)
handler() # added the function call for testing purposes only
root.mainloop()
Add two print statements as placeholders when stepping through the script as shown here:
nseOptionsAnalysis_lbl(status_label) # place breakpoint here as before
print('TestingOne')
print('TestingTwo')
Now go ahead and re-run the script in debug mode and step through it as before. You will see that it does stop on this line as expected when placing an explicit function call in the script.
As your script is currently written, you need to press the button to create an event to provoke the function call.