python - Pycharm 调试器在 Ubuntu 上运行,但在 Windows 上运行相同的代码

标签 python debugging pyqt pycharm signals-slots

更新:正在跟踪的问题 here

编辑:我可能不是唯一遇到此问题的人。我错过了今天也发布的这个,它看起来相关:PyCharm debugger doesn't work

下面是一个使用 Pycharm Professional 时在 Windows 7 和 Ubuntu 14.04 上运行良好的最小示例

PyCharm 2016.1
Build #PY-145.260, built on March 22, 2016
JRE: 1.8.0_60-b27 x86
JVM: Java HotSpot(TM) Server VM by Oracle Corporation

现在是 super 奇怪的部分:在 Ubuntu 的 Pycharm 调试器上我没有遇到任何问题。但是,在 Window 的 Pycharm 调试器中,我得到以下回溯(即使代码在不调试时运行良好!):

Traceback (most recent call last):
  File "D:\Program Files (x86)\JetBrains\PyCharm 2016.1\helpers\pydev\pydevd.py", line 1530, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "D:\Program Files (x86)\JetBrains\PyCharm 2016.1\helpers\pydev\pydevd.py", line 937, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:/Users/Cornelis/PycharmProjects/SO-Help/test.py", line 11, in <module>
    class MainWindow(QtGui.QMainWindow):
  File "C:/Users/Cornelis/PycharmProjects/SO-Help/test.py", line 58, in MainWindow
    @QtCore.pyqtSlot(int, int)
AttributeError: 'module' object has no attribute 'pyqtSlot'
Backend TkAgg is interactive backend. Turning interactive mode on.

我什至不知道我应该如何开始调试,因为我以前从未见过这样的事情发生!如果您的程序在您运行时正常运行,但在您尝试调试它时却不正常,而且它在另一个操作系统上调试得很好,这到底意味着什么?这只能是 Pycharm 的错误吗?上面的回溯与我在主程序中得到的并不完全相同,但它是我能得到的最接近的。以下是我在完整程序中看到的回溯:

Backend TkAgg is interactive backend. Turning interactive mode on.
Traceback (most recent call last):
  File "D:\Program Files (x86)\JetBrains\PyCharm 2016.1\helpers\pydev\pydevd.py", line 1530, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "D:\Program Files (x86)\JetBrains\PyCharm 2016.1\helpers\pydev\pydevd.py", line 937, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "D:/Home/Documents/PycharmProjects/BMDanalyse/BMDanalyse/SPCanalyse.py", line 22, in <module>
    from ViewBoxCustom import MultiRoiViewBox, ImageAnalysisViewBox
  File "D:/Home/Documents/PycharmProjects/BMDanalyse/BMDanalyse\ViewBoxCustom.py", line 68, in <module>
    class MultiRoiViewBox(pg.ViewBox):
  File "D:/Home/Documents/PycharmProjects/BMDanalyse/BMDanalyse\ViewBoxCustom.py", line 71, in MultiRoiViewBox
    clicked = QtCore.pyqtSignal(int, int)
AttributeError: 'module' object has no attribute 'pyqtSignal'

这是您的最小示例。只需复制和粘贴并单击 ViewBox。如果你运行它,应该会出现一个随机图像。但是如果你调试它,它就会爆炸。

import os, sys, matplotlib, matplotlib.pyplot
import numpy as np
from pyqtgraph.Qt import QtCore, QtGui
from pyqtgraph.widgets.GraphicsLayoutWidget import GraphicsLayoutWidget
import pyqtgraph as pg
import pyqtgraph.functions as fn

N = 256
ARR = np.random.random((N,N))*255

class MainWindow(QtGui.QMainWindow):

    def __init__(self, parent=None):

        QtGui.QMainWindow.__init__(self, parent)
        self.setupUserInterface()
        self.setupSignals()

    def setupUserInterface(self):
        """ Initialise the User Interface """
        # Left frame
        leftFrame = QtGui.QFrame()
        leftFrameLayout = QtGui.QHBoxLayout()
        leftFrame.setLayout(leftFrameLayout)
        leftFrame.setLineWidth(0)
        leftFrame.setFrameStyle(QtGui.QFrame.Panel)
        leftFrameLayout.setContentsMargins(0,0,5,0)

        # Left frame contents
        self.viewMain = GraphicsLayoutWidget()  # A GraphicsLayout within a GraphicsView
        leftFrameLayout.addWidget(self.viewMain)
        self.viewMain.setMinimumSize(200,200)
        self.vb = MultiRoiViewBox(lockAspect=True,enableMenu=True)
        self.viewMain.addItem(self.vb)
        self.vb.enableAutoRange()

        # Right frame
        self.sidePanel = SidePanel(self)

        # UI window (containing left and right frames)
        UIwindow         = QtGui.QWidget(self)
        UIwindowLayout   = QtGui.QHBoxLayout()
        UIwindowSplitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
        UIwindowLayout.addWidget(UIwindowSplitter)
        UIwindow.setLayout(UIwindowLayout)
        self.setCentralWidget(UIwindow)
        UIwindowSplitter.addWidget(leftFrame)
        UIwindowSplitter.addWidget(self.sidePanel)

        self.setMinimumSize(600,500)
        self.resize(self.minimumSize())

    def setupSignals(self):
        """ Setup signals """
        # self.sidePanel.buttImageAdd.clicked.connect(self.showImage)
        self.vb.clicked.connect(self.on_vbc_clicked)

    @QtCore.pyqtSlot(int, int)
    def on_vbc_clicked(self, x, y):
        print(x)
        print(y)
        self.showImage()

    def showImage(self):
        """ Shows image in main view """
        self.vb.showImage(ARR)

class ViewMode():
    def __init__(self,id,cmap):
        self.id   = id
        self.cmap = cmap
        self.getLookupTable()
    def getLookupTable(self):
        lut = [ [ int(255*val) for val in self.cmap(i)[:3] ] for i in xrange(256) ]
        lut = np.array(lut,dtype=np.ubyte)
        self.lut = lut

class MultiRoiViewBox(pg.ViewBox):

    sigROIchanged = QtCore.Signal(object)
    clicked = QtCore.pyqtSignal(int, int)

    def __init__(self,parent=None,border=None,lockAspect=False,enableMouse=True,invertY=False,enableMenu=True,name=None):
        pg.ViewBox.__init__(self,parent,border,lockAspect,enableMouse,invertY,enableMenu,name)
        self.img      = None
        self.NORMAL   = ViewMode(0,matplotlib.cm.gray)
        self.DEXA     = ViewMode(1,matplotlib.cm.jet)
        self.viewMode = self.NORMAL

    def showImage(self,arr):
        if arr==None:
            self.img = None
            return
        if self.img==None:
            self.img = pg.ImageItem(arr,autoRange=False,autoLevels=False)
            self.addItem(self.img)
        self.img.setImage(arr,autoLevels=False)
        self.updateView()

    def updateView(self):
        self.background.setBrush(fn.mkBrush(self.viewMode.lut[0]))
        self.background.show()
        if    self.img==None: return
        else: self.img.setLookupTable(self.viewMode.lut)

    def mouseClickEvent(self, ev):
        if ev.button() == QtCore.Qt.RightButton and self.menuEnabled():
            ev.accept()
            self.raiseContextMenu(ev)
        elif ev.button() == QtCore.Qt.LeftButton:
            ev.accept()
            pos = self.mapToItem(self.img, ev.pos())
            self.clicked.emit(pos.x(), pos.y())

from pyqtgraph.Qt import QtCore,QtGui

class SidePanel(QtGui.QWidget):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.setMinimumWidth(250)
        self.buttMinimumSize = QtCore.QSize(36,36)
        self.setupImageToolbox()
        sidePanelLayout = QtGui.QVBoxLayout()
        sidePanelLayout.addWidget(self.imageToolbox)
        sidePanelLayout.setContentsMargins(0,0,0,0)
        self.setLayout(sidePanelLayout)

    def setupImageToolbox(self):
        # Image buttons
        self.buttImageAdd  = QtGui.QPushButton()
        imageButtons       = [self.buttImageAdd]
        for i in xrange(len(imageButtons)):
            image = imageButtons[i]
            image.setMinimumSize(self.buttMinimumSize)

        self.imageFileTools  = QtGui.QFrame()
        imageFileToolsLayout = QtGui.QHBoxLayout()
        self.imageFileTools.setLayout(imageFileToolsLayout)
        self.imageFileTools.setLineWidth(1)
        self.imageFileTools.setFrameStyle(QtGui.QFrame.StyledPanel)
        imageFileToolsLayout.addWidget(self.buttImageAdd)

        # Image Toolbox (containing imageFileList + imageFileList buttons)
        self.imageToolbox = QtGui.QFrame()
        self.imageToolbox.setLineWidth(2)
        self.imageToolbox.setFrameStyle(QtGui.QFrame.Panel | QtGui.QFrame.Raised)
        imageToolboxLayout = QtGui.QVBoxLayout()
        self.imageToolbox.setLayout(imageToolboxLayout)
        imageToolboxLayout.addWidget(self.imageFileTools)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

最佳答案

重新安装 Pycharm 并从头开始重新设置 python 包后问题已解决,除了这次使用 python 64 位(之前使用 32 位)。此外,我使用的是 Continuum Analytics 的 python 包管理器。我不再使用它,所以这也可能是罪魁祸首。

关于python - Pycharm 调试器在 Ubuntu 上运行,但在 Windows 上运行相同的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36413623/

相关文章:

python - 如何让 pipenv 基于符号链接(symbolic link)路径而不是真实路径工作

python - 如何使用 _winreg Python 将本地时间 (REG_BINARY) 写入注册表

c++ - 在 gdb 中打印整个链表?

c++ - 我可以更改 glui 面板背景颜色吗?

python - 如何修改 QDataWidgetMapper 的值

python - 将项目文本从 QTreeView 拖放到 QLineEdit

python - 从 Shapely 中的多边形中提取点/坐标

在查询中使用 Python 变量时,Python MySQL 查询没有给出结果

c# - 如何轻松地从编译中排除某些代码行?

delphi - Delphi 6 调试在 Windows 10 上能否正常工作?