首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyQt -不显示FigureCanvas (导航工具栏显示)

PyQt -不显示FigureCanvas (导航工具栏显示)
EN

Stack Overflow用户
提问于 2017-03-23 00:51:07
回答 2查看 2.6K关注 0票数 0

我正在尝试在FigureCanvas上实现拖放。这意味着,我希望将文本从左侧布局拖放到右侧的画布上。不幸的是,即使显示了导航工具栏,画布也没有显示。

在创建实现拖放的Create_Canvas类之前,我可以毫无问题地显示所有内容。我是编程新手,我不确定我写的Create_Canvas类是否正确。

我还发了一张照片:

以防您没有安装matplotlib。

提前感谢!

代码语言:javascript
复制
import sys, time

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors as mcolors
import matplotlib.colorbar as mcolorbar

import numpy as np
import pylab as pl

import random


class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        self.editLayout = QWidget()
        self.edit = QLineEdit('chfghfghf', self)
        self.edit.setDragEnabled(True)

        self.left_layout = QVBoxLayout()
        self.left_layout.addWidget(self.edit)

        self.editLayout.setLayout(self.left_layout)

        #Create the right layout that contains the plot canvas.    
        self.plotLayout = QWidget();

        canvas = Create_Canvas(self)       

        self.button = QPushButton('Plot')

        # set the layout
        self.right_layout = QVBoxLayout()
        self.right_layout.addWidget(canvas)
        self.right_layout.addWidget(self.button)

        self.plotLayout.setLayout(self.right_layout)

        splitter_filebrowser = QSplitter(Qt.Horizontal)
        splitter_filebrowser.addWidget(self.editLayout)
        splitter_filebrowser.addWidget(self.plotLayout)
        splitter_filebrowser.setStretchFactor(1, 1)

        hbox = QHBoxLayout(self)
        hbox.addWidget(splitter_filebrowser)

        self.centralWidget().setLayout(hbox)

        self.setWindowTitle('Simple drag & drop')
        self.setGeometry(750, 100, 600, 500)


class Create_Canvas(QWidget):
    def __init__(self, parent):
        super().__init__(parent)
        self.setAcceptDrops(True)

        figure = plt.figure()
        canvas = FigureCanvas(figure)
        toolbar = NavigationToolbar(canvas, self)

        self.right_layout = QVBoxLayout()
        self.right_layout.addWidget(canvas)
        self.right_layout.addWidget(toolbar)

    def dragEnterEvent(self, e):
        print('entering')
        if e.mimeData().hasFormat('text/plain'):
            e.accept()
        else:
            e.ignore()

    def dragMoveEvent(self, e):
        print('drag moving')

    def dropEvent(self, e):
        print("dropped")


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()      
EN

回答 2

Stack Overflow用户

发布于 2017-03-24 00:32:25

要点是您需要将画布添加到布局中,并将此布局添加到小部件中。最后,你也不应该忘记画画布。

代码语言:javascript
复制
class Create_Canvas(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self,parent)
        self.setAcceptDrops(True)

        figure = plt.figure()
        self.canvas = FigureCanvas(figure)
        toolbar = NavigationToolbar(self.canvas, self)

        self.right_layout = QVBoxLayout()
        self.right_layout.addWidget(self.canvas)
        self.right_layout.addWidget(toolbar)
        # set the layout of this widget, otherwise the elements will not be seen.
        self.setLayout(self.right_layout)
        # plot some stuff
        self.ax = figure.add_subplot(111)
        self.ax.plot([1,2,5])        
        # finally draw the canvas
        self.canvas.draw()

(这是它在PyQt4中的外观,在PyQt5中应该类似)。

票数 1
EN

Stack Overflow用户

发布于 2020-10-02 08:45:00

在PyQt5中,你需要在主窗口类上使用SUPER键来接受拖放。dropevent函数应如下所示:

代码语言:javascript
复制
def dropEvent(self,e):
        """
        This function will enable the drop file directly on to the 
        main window. The file location will be stored in the self.filename
        """
        if e.mimeData().hasUrls:
            e.setDropAction(QtCore.Qt.CopyAction)
            e.accept()
            for url in e.mimeData().urls():
                if op_sys == 'Darwin':
                    fname = str(NSURL.URLWithString_(str(url.toString())).filePathURL().path())
                else:
                    fname = str(url.toLocalFile())
            self.filename = fname
            print("GOT ADDRESS:",self.filename)
            self.readData()
        else:
            e.ignore() # just like above functions  

完整的code输出如下:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42957995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档