Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想用拖放功能获取并显示图像;pyqt5的滴加方法_Python_Python 3.x_Pyqt_Pyqt5 - Fatal编程技术网

Python 我想用拖放功能获取并显示图像;pyqt5的滴加方法

Python 我想用拖放功能获取并显示图像;pyqt5的滴加方法,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,我想用pyqt5的拖放方法获取和显示图像。比如说像这个画面, 我想让拖放空间和图像显示空间。 我预计如果在左上空间拖放JPG图像,图像会显示在中间空间。 但当我将jpg图像拖到左上角时,“drop enabled mark”不会显示。因此,当我拖放图像时,没有任何反应。当您要从桌面拖动图像时,此图像的路径通过本地url提供图像,以验证它是否使用了mimeData()hasUrls(),和urls()来获取url,然后我们使用方法toLocalFile()获取本地路径,所有这些都在以下代码中

我想用pyqt5的拖放方法获取和显示图像。比如说像这个画面,

我想让拖放空间和图像显示空间。

我预计如果在左上空间拖放JPG图像,图像会显示在中间空间。
但当我将jpg图像拖到左上角时,“drop enabled mark”不会显示。因此,当我拖放图像时,没有任何反应。

当您要从桌面拖动图像时,此图像的路径通过本地url提供图像,以验证它是否使用了
mimeData()
hasUrls()
,和
urls()
来获取url,然后我们使用方法
toLocalFile()
获取本地路径,所有这些都在以下代码中实现:

class Button(QPushButton):
    def __init__(self, title, parent):
        super().__init__(title, parent)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, e):
        m = e.mimeData()
        if m.hasUrls():
            e.accept()
        else:
            e.ignore()

    def dropEvent(self, e):
        m = e.mimeData()
        if m.hasUrls():
            self.parent().label.setPixmap(QPixmap(m.urls()[0].toLocalFile())) 


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

    def initUI(self):
        button = Button("",self)
        button.resize(100,100) 
        button.setIcon(QIcon("gazo1.jpg")) 
        button.setIconSize(QSize(100,100))
        button.move(0, 0)

        self.label = QLabel(self)
        self.label.setPixmap(QPixmap('gazo2.png'))
        self.label.move(150,150)

        self.setWindowTitle('Simple drag & drop')
        self.setGeometry(300, 300, 300, 300)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()

您从哪里获得图像?您确定格式为
image/*
?谢谢您的评论。我通过拖放从桌面获取图像,但无法获取。我试图将“image/*”改为“.jpg”,但没有任何反应。如何更正?执行下一个更改:
def dragEnterEvent(self,e):print(e.mimeData().formats())
Printed next句子。因为太久了,我决定。谢谢你!就像我想的那样。我是毕业生,因为你已经回答了我所有的问题。你是我的恩人。@toma如果你不知道如何做,请不要忘记将我的答案标记为正确,检查以下链接:现在,它标记为正确?如果不是,请评论。是的,就是这样P
class Button(QPushButton):
    def __init__(self, title, parent):
        super().__init__(title, parent)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, e):
        m = e.mimeData()
        if m.hasUrls():
            e.accept()
        else:
            e.ignore()

    def dropEvent(self, e):
        m = e.mimeData()
        if m.hasUrls():
            self.parent().label.setPixmap(QPixmap(m.urls()[0].toLocalFile())) 


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

    def initUI(self):
        button = Button("",self)
        button.resize(100,100) 
        button.setIcon(QIcon("gazo1.jpg")) 
        button.setIconSize(QSize(100,100))
        button.move(0, 0)

        self.label = QLabel(self)
        self.label.setPixmap(QPixmap('gazo2.png'))
        self.label.move(150,150)

        self.setWindowTitle('Simple drag & drop')
        self.setGeometry(300, 300, 300, 300)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()