我正在尝试在我自己的PyCharm项目中复制/合并this tutorial中的代码。
我有两个文件tempconv.py
和mainwindow.py
,其中包含相同的代码。
前一个文件位于Users/some_user/Documents/Coding/Qt/firstGUI
后一个文件位于/Users/some_user/Documents/Coding/Python/CV-generator
代码中引用的文件tempconv.ui
存在于两个文件夹中。
这是文件中的代码。这些文件是相同的,但mainwindow.py
包含在我的PyCharm项目中,而tempconv.py
则不包括在内。
import sys
from PyQt4 import QtCore, QtGui, uic
form_class = uic.loadUiType("tempconv.ui")[0] # Load the UI
class MyWindowClass(QtGui.QMainWindow, form_class):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.btn_CtoF.clicked.connect(self.btn_CtoF_clicked) # Bind the event handlers
self.btn_FtoC.clicked.connect(self.btn_FtoC_clicked) # to the buttons
def btn_CtoF_clicked(self): # CtoF button event handler
cel = float(self.editCel.text()) #
fahr = cel * 9 / 5.0 + 32 #
self.spinFahr.setValue(int(fahr + 0.5)) #
def btn_FtoC_clicked(self): # FtoC button event handler
fahr = self.spinFahr.value() #
cel = (fahr - 32) * 5.0 / 9 #
self.editCel.setText(str(cel)) #
app = QtGui.QApplication(sys.argv)
myWindow = MyWindowClass(None)
myWindow.show()
app.exec_()
有4种情况,其中一种情况失败。
Run in PyCharm Run in Terminal
mainwindow.py Error Succes
tempconv.py Succes Succes
错误是:
Traceback (most recent call last):
File "/Users/some_user/Documents/Coding/Python/CV-generator/mainwindow.py", line 4, in <module>
form_class = uic.loadUiType("tempconv.ui")[0] # Load the UI
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyQt4/uic/__init__.py", line 208, in loadUiType
winfo = compiler.UICompiler().compileUi(uifile, code_string, from_imports, resource_suffix)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyQt4/uic/Compiler/compiler.py", line 140, in compileUi
w = self.parse(input_stream, resource_suffix)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyQt4/uic/uiparser.py", line 974, in parse
document = parse(filename)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1182, in parse
tree.parse(source, parser)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 647, in parse
source = open(source, "rb")
IOError: [Errno 2] No such file or directory: 'tempconv.ui'
如果我在终端中运行python /Users/some_user/Documents/Coding/Python/CV-generator/mainwindow.py
则没有问题。
另外,这个:
Machine:~ some_user$ ls /Users/some_user/Documents/Coding/Python/CV-generator
README.md functions.pyc mainwindow.py scratch.json tex
functions.py input.json read-in.py tempconv.ui
显示文件在那里。
如何找到问题的原因并解决问题?