Best way to debug a GUI application (particularly in Python)?

六眼飞鱼酱① 提交于 2021-01-28 22:16:36

问题


So I'm trying to debug this source code (in Python) that I didn't write that has a GUI (Kivy in particular, and I'm trying to figure out what code/event is being triggered when I click on certain things. I try to put in breakpoints for where I think it's going to be triggered, I sometimes find that I'm wrong, and the breakpoint doesn't seem to ever be encountered for what I want.

Anyone have any advice for GUI debugging, and figuring out what is being triggered by certain events, especially in Python?


回答1:


How about putting print statements in the different functions (or event handlers) that print the name of the function, followed by the arguments the function received? That should make it pretty clear what's going on when you use the GUI. I usually wrap print in a function that I use for this verbose output, e.g.:

def verbose_print(message, *args):
    if VERBOSE:
        print message.format(*args)

So now, in my various functions, I can use the function like this:

def do_something(param1, param2):
    verbose_print('do_something({}, {})', param1, param2)

    # Do some stuff...

Additionally you can now turn the verbose output on and off by setting the module-global constant VERBOSE to True or False instead of removing all the print statements.




回答2:


There's a famous CLI based joke program called xcowsay. It displays a popup(a GUI one) - picture of a cow with a message. You'll need to install xcowsay on your PC first, it is very small.

Instead of print and then searching for the message in your terminal, use this oneliner

# Example
import subprocess; subprocess.Popen(['xcowsay', 'your message here', '-t 1'])

# General syntax
import subprocess; subprocess.Popen(['xcowsay', string1, string2, string3 ..., '-t 1'])

# For printing objects, cast them to str first
# '-t 1' is for displaying the popup for one second, you can change it.

Why I use it:

  1. Simple and short - one line
  2. Works on most systems - Linux, Mac and Windows
  3. Runs asynchronously - won't interfere with your apps' execution
  4. Multilines strings are supported
  5. Unicode support - emojis will work

I found this when I started using Python, and didn't know of pdb etc. In fact, this program can GUIfy your whole CLI.

Screenshot



来源:https://stackoverflow.com/questions/13666911/best-way-to-debug-a-gui-application-particularly-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!