PyQt4 example working from IDLE, but not when exec

2019-08-28 04:16发布

问题:

I am running a very basic example in PyQt4. It is shown below. I was struggling with the Enthought Canopy installation, struggling with the cygwin Python implementation, and finally just installed Python 2.7, Numpy 1.7.1, MatPlotLib 1.2.0 one at a time.

When I execute the example from IDLE, it works fine. Although when I try to execute it from Notepad++ using nppExec, the console window just hangs. I do not see a little empty window pop up anywhere, nor am I given any error codes.

  • I tried interactive mode and non-interactive mode from nppExec (-i)
    • My nppExec command is python "$(FULL_CURRENT_PATH)"
  • I tried pulling the guts of tho code out of the function definition and running it by itself, same thing.

.

  • Python 2.7.4
  • notepad++ 6.3.2
  • PyQt4 4.10.1

    import sys
    from PyQt4 import QtGui
    
    
    def main():
    
        app = QtGui.QApplication(sys.argv)
    
        w = QtGui.QWidget()
        w.resize(250, 150)
        w.move(300, 300)
        w.setWindowTitle('Brian')
        w.show()
    
    sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

回答1:

In NppExec, use the command

CMD /C python -u "$(FULL_CURRENT_PATH)"

instead of

python "$(FULL_CURRENT_PATH)"


回答2:

I had/have the same issue. Using w.showMaximized() instead of w.show() solved for me the problem of not showing the window in Notepad++. Subsequent opened widgets can be opened with w.show().

However, your code did not work for me, I got a Python traceback. Instead I used (with PySide):

import sys
from PySide import QtGui

app = QtGui.QApplication(sys.argv)

def main():

    w = QtGui.QWidget()
    w.resize(250, 150)
    w.move(300, 300)
    w.setWindowTitle('Brian')
    w.showMaximized()
    app.exec_()


if __name__ == '__main__':
    main()

And, as a work-around, just add something like w.resize(width, height) after you called w.showMaximized() to resize the window to your target size.