python 调试 pyQT4 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1736015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debugging a pyQT4 app?
提问by Paul Wicks
I have a fairly simple app built with pyqt4. I wanted to debug one of the functions connected to one of the buttons in my app. However, when I do the following
我有一个用 pyqt4 构建的相当简单的应用程序。我想调试连接到我的应用程序中的按钮之一的功能之一。但是,当我执行以下操作时
python -m pdb app.pyw
> break app.pyw:55 # This is where the signal handling function starts.
things don't quite work like I'd hope. Instead of breaking in the function where I've set the breakpoint and letting me step through it, the debugger enters an infinite loop printing out QCoreApplication::exec: The event loop is already running
and I am unable to input anything. Is there a better way to do this?
事情并不像我希望的那样工作。调试器没有进入我设置断点的函数并让我单步执行,而是进入无限循环打印输出,QCoreApplication::exec: The event loop is already running
而我无法输入任何内容。有一个更好的方法吗?
回答by quark
You need to call QtCore.pyqtRemoveInputHook. I wrap it in my own version of set_trace
:
您需要调用QtCore.pyqtRemoveInputHook。我将它包装在我自己的版本中set_trace
:
def debug_trace():
'''Set a tracepoint in the Python debugger that works with Qt'''
from PyQt4.QtCore import pyqtRemoveInputHook
# Or for Qt5
#from PyQt5.QtCore import pyqtRemoveInputHook
from pdb import set_trace
pyqtRemoveInputHook()
set_trace()
And when you are done debugging, you can call QtCore.pyqtRestoreInputHook()
, probably best when you are still in pdb, and then after you hit enter, and the console spam is happening, keep hitting 'c' (for continue) until the app resumes properly. (I had to hit 'c' several times for some reason, it kept going back into pdb, but after hitting it a few times it resumed normally)
当您完成调试后,您可以调用QtCore.pyqtRestoreInputHook()
,可能最好是在您仍在 pdb 中时,然后在您按 Enter 之后,并且控制台垃圾邮件正在发生,继续按“c”(继续)直到应用程序正常恢复。(由于某种原因,我不得不多次点击'c',它一直回到pdb,但在点击几次后它恢复正常)
For further info Google "pyqtRemoveInputHook pdb". (Really obvious isn't it? ;P)
有关更多信息,请使用谷歌“pyqtRemoveInputHook pdb”。(真的很明显不是吗?;P)
回答by jamk
I had to use a "next" command at the trace point to get outside of that function first. For that I made a modification of the code from mgrandi:
我必须在跟踪点使用“next”命令才能首先退出该函数。为此,我对 mgrandi 的代码进行了修改:
def pyqt_set_trace():
'''Set a tracepoint in the Python debugger that works with Qt'''
from PyQt4.QtCore import pyqtRemoveInputHook
import pdb
import sys
pyqtRemoveInputHook()
# set up the debugger
debugger = pdb.Pdb()
debugger.reset()
# custom next to get outside of function scope
debugger.do_next(None) # run the next command
users_frame = sys._getframe().f_back # frame where the user invoked `pyqt_set_trace()`
debugger.interaction(users_frame, None)
This worked for me. I found the solution from here : Python (pdb) - Queueing up commands to execute
这对我有用。我从这里找到了解决方案:Python (pdb) - 排队执行命令
回答by Phillip M. Feldman
In my tests, jamk's solution works, while the previous one, although simpler, does not.
在我的测试中,jamk 的解决方案有效,而前一个解决方案虽然更简单,但无效。
In some situations, for reasons that are unclear to me, I've been able to debug Qt without doing any of this.
在某些情况下,由于我不清楚的原因,我已经能够在不执行任何这些操作的情况下调试 Qt。