community
community copied to clipboard
Reaching a breakpoint from a button with a on_press event disables mouse click on Linux
Software Versions
- Python: 3.11
- OS: Linux Manjaro with KDE or Linux in ChromeOS
- Kivy: 2.2.0
- Kivy installation method: pip
Describe the bug When I have a Button defined in a .kv file that has an on_press event. When I stop on a breakpoint in the method called in the on_press event. The IDE's UI will not accept any click events or highlight any buttons in the IDE that I mouse over. I have tested it in Pycharm and VSCode with the same result. Keyboard commands still work. Ie. When a breakpoint is reached I cannot press the skip over button to go to the next line. Pressing F8 still works. Pressing Shift-Alt-9 to continue running the program will cause the mouse click to start working again.
Workaround Change the on_press to on_release and debugging will work correctly.
Expected behavior Program stops at breakpoint and mouse clicks and mouse hover works correctly.
To Reproduce Run the code in debug mode in VSCode or Pycharm. Mouse click will stop working after clicking the button. Press F5 to continue and the mouse click will start working again.
Minimal code showing the problem.
main.py
import kivy
from kivymd.app import MDApp
from kivy.lang.builder import Builder
class MainApp(MDApp):
def build(self):
return Builder.load_file('main.kv')
def button_press(self):
print('Button Pressed') # Put a breakpoint on this line
if __name__=="__main__":
MainApp().run()
main.kv
BoxLayout:
Button:
text: "Press Me!"
on_press: app.button_press()
try to implement on another thread
@givver Perhaps I misunderstand your request. When the program is at a breakpoint, the code is stopped, the UI will not respond. This is not specific to a button press or any other callback. Any breakpoint on the main thread will cause the UI to become non-responsive.
@givver Perhaps I misunderstand your request. When the program is at a breakpoint, the code is stopped, the UI will not respond. This is not specific to a button press or any other callback. Any breakpoint on the main thread will cause the UI to become non-responsive.
I think OP misunderstands how the debugger works. This issue should be closed.
Normally, I would recommend looking at kivy/ncis, but it is still in Alpha and doesn't seem to work at the moment.
I know how the debugger works. As I stated in the OP, when I reach a breakpoint from a on_press event in kivy. The IDE (PyCharm) as well as the rest of my desktop (KDE) will stop responding to mouse clicks. I know the Kivy program will not respond to mouse click as it is stopped at the breakpoint. But PyCharm, as will as the rest of my system should still respond to mouse clicks. My Keyboard still works and I can use the hotkeys to resume or step over the breakpoints. When I press F9 to resume the program, PyCharm and the rest of my system will start responding again. if I change on_press to on_release then everything will work as intended. I have tested this on my main development system as well as in a Virtual Machine. I have only tested this on Manjaro Linux with the KDE desktop. It might be something specific to this configuration?
I have never experienced this issue on Window's on Mac. It must be something specific to your configuration. That is really strange.
@givver would you mind recording this happening?
@FilipeMarch here is a video showing what happens when I try to debug a on_press event on Manjaro Linux. https://youtu.be/-gioE1DINag
Is there any update on this issue?
@Julian-O Can you please have alook at the video I made about this issue?
@givver: Who me? Nah. I have no expertise in how PyCharm run Python apps. I won't be working on this.
I am having the exact same issue. I was originally developing the program on Win 11 and no issue. I migrated to Ubuntu 22.4 and have the same issue. Any update on this or a workaround?
my suggestion for a work around would be to use the on_release event for a button press rather than on_press.
That is what I have been doing. But this bug still needs to be fixed.
What is the workaround for cases where I have on_press do one action and on_release of the same button doing a second action? @FilipeMarch @Julian-O - Why is this a closed issue because someone has not taken the time to understand the problem, but assuming the user does not understand the program? When an on_press event is triggered and the debug hits a breakpoint, the mouse stops working, not just in pycharm, but everywhere. Keyboard commands continue work, the program is not frozen, just paused at a breakpoint. This is not how a debugger should work, a debugger should pause at the breakpoint and allow you to view the state of the program in the debugger window using your mouse. This is how the debugger works in every other case, why not in this one?
edit: further thought...
My assumption is kivy is capturing the left mouse button in the down state, pycharm breaks the program at the breakpoint, and when focus is lost on the app being debugged, it is not releasing the left mouse button. Seems like an easy fix.
@InfinityCliff This is still an open issue. I cannot reproduce this issue on Windows or Mac,
What windowing backend is being used. Please share your kivy log? This might not be a kivy issue but a lower level issue with the window provider under kivy.
Doing a quick search I found a similar issue: https://discourse.libsdl.org/t/debugging-with-sdl-capturemouse/31014 Are you using the SDL backend? It appears imgui has a similar issue: https://github.com/ocornut/imgui/issues/3650
A possible (yet awkward) workaround could be to use a Clock.schedule_once to defer the handling of the press to the next clock tick. Example below:
from kivy.app import App
from kivy.lang import Builder
kv = """
#:import Clock kivy.clock.Clock
AnchorLayout:
Button:
size_hint: None, None
size: dp(150), dp(48)
text: 'Try Me'
on_press: Clock.schedule_once(lambda dt: app.button('pressed'))
on_release: app.button('released')
"""
class PressReleaseApp(App):
def build(self):
return Builder.load_string(kv)
def button(self, message):
print(message) # set a breakpoint here
PressReleaseApp().run()
Why is this a closed issue because someone has not taken the time to understand the problem, but assuming the user does not understand the program?
Firstly, it isn't a closed issue. It remains open.
Secondly, this is an open source project staffed entirely by volunteers. It is likely that no-one has taken the time to understand this problem because it is low priority. It doesn't affect many people, it isn't easy to reproduce and it is easy to work around (don't put a breakpoint there). Meanwhile, there are hundreds and hundreds of issues that are higher priority.
However, we appreciate this is important to you. All the source is available to you and you have a machine that demonstrates the problem, so please consider bringing your own resources into solving this. We would welcome a PR request that fixes the problem so others can benefit from your efforts. I do wish you luck.
I would be glad to take a look at it. Can you provide me some guidance on where in the kivy backend are the events dispatched and where are they processed/handled? I had looked at that prior to posting here, but was unable to find it, not really sure what I am looking for. Any guidance would be appreciated.
I have done so testing and I think I narrowed down the problem. I tested it using the X11 and Wayland compositors. Both had the same issue. While testing, I noticed the KIVY_WINDOW environment variable. I tried all the options and only sdl2 and x11 was available on my system. Setting KIVY_WINDOW=x11 fixed the problem for me. So I think this issues is caused by the way Kivy uses sdl2 or in sdl2 itself.
I don't have any experience with that part of the Kivy backend (and none with Kivy on x11), but I would suggest the #dev channel on the Kivy Discord as a place to ask these questions with more eyeballs on it.
Thanks @Julian-O, I will ask on the ask on the discord.