[英]Nothing happens when running PyQt4 example code
I am trying to run some example code using PyQt4. 我正在尝试使用PyQt4运行一些示例代码。 I am trying to run it from Notepad++ and also from the Enthought Canopy environment. 我正在尝试从Notepad ++以及Enthought Canopy环境中运行它。 The example code is shown below in its entirety. 示例代码完整显示在下面。 From within npp, I am using the plugin nppExec with python -i "$(FULL_CURRENT_PATH)" as the command. 在npp中,我将python -i“ $(FULL_CURRENT_PATH)”作为命令使用插件nppExec。 (i also tried it without interactive -i) (我也没有互动-i尝试过)
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('Simple')
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
When I run it from within npp, The internal console seems to freeze up (no errors) and is recoverable with ctrl-c 当我从npp中运行它时,内部控制台似乎冻结了(没有错误)并且可以通过ctrl-c恢复
When I run it from within Canopy, I get the following error message 当我从Canopy中运行它时,出现以下错误消息
%run "c:\users\brian\appdata\local\temp\tmpfe9mmh.py"
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
C:\Users\Brian\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.0.0.1160.win-x86\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
174 else:
175 filename = fname
--> 176 exec compile(scripttext, filename, 'exec') in glob, loc
177 else:
178 def execfile(fname, *where):
c:\users\brian\appdata\local\temp\tmpfe9mmh.py in <module>()
20
21 import sys
---> 22 from PyQt4 import QtGui
23
24
C:\Users\Brian\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.0.0.1160.win-x86\lib\site-packages\IPython\external\qt_loaders.pyc in load_module(self, mod_name)
43 Importing %s disabled by IPython, which has
44 already imported an Incompatible QT Binding: %s
---> 45 """ % (mod_name, loaded_api()))
46
47 ID = ImportDenier()
ImportError:
Importing PyQt4 disabled by IPython, which has
already imported an Incompatible QT Binding: pyside
I am pretty confused as to why Canopy places its items in a seemingly strange place (C:\\Users\\Brian\\AppData\\Local\\Enthought\\Canopy32), rather than the simple C:/python27 我对为什么Canopy将其项目放置在一个看似奇怪的地方(C:\\ Users \\ Brian \\ AppData \\ Local \\ Enthought \\ Canopy32)而不是简单的C:/ python27感到非常困惑
I am running python 2.7 32 bit, Canopy Version: 1.0.0.1160 32 bit, Win7 64 bit 我正在运行python 2.7 32位,Canopy版本:1.0.0.1160 32位,Win7 64位
It looks like pyside (an alternative python binding for Qt) is somehow being automatically imported when python is run on your machine. 看起来pyside(用于Qt的替代python绑定)在您的计算机上运行python时会以某种方式自动导入。 import both pyside and pyqt is causing some sort of clash that is causing your problems. 导入pyside和pyqt都会引起某种冲突,这会引起您的问题。
I wonder.. do you have a module named sys.py in your path that is using pyside? 我想知道..您在使用pyside的路径中是否有一个名为sys.py的模块? You are importing sys which is a built-in module, but if you have a file named sys.py in your path, that will be imported instead. 您正在导入sys,它是一个内置模块,但是如果您的路径中有一个名为sys.py的文件,则会导入该文件。 If this is the case, then you can run the script: 如果是这种情况,则可以运行脚本:
import sys
print sys.__file__
to find where the file is. 查找文件在哪里。 If the sys module being imported is actually the built-in module, sys will not have a __file__
attribute and an exception will be raised. 如果要导入的sys模块实际上是内置模块,则sys将没有__file__
属性,并且将引发异常。
By the way, I tried your example code on my machine and it ran without any issues. 顺便说一句,我在我的机器上尝试了您的示例代码,该代码没有任何问题。
In NppExec, use the command 在NppExec中,使用以下命令
CMD /C python -u "$(FULL_CURRENT_PATH)"
instead of 代替
python "$(FULL_CURRENT_PATH)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.