2017-06-23 2526 views
0

我想獲取QLabel上放置的文件的路徑。所以我這樣編碼,但標籤不接受文件。問題是什麼..? 這是我的代碼。所以很長的代碼抱歉,謝謝你!如何使用pyqt5從文件路徑中獲取文件路徑?

import random 
import sys 
import pygame 
import time 
from PyQt5.QtGui import QPixmap 
from PyQt5.QtCore import Qt, QTimer 
from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QDesktopWidget 
class Example(QWidget): 
    size=100 
    imgNum=0 
    frameCount=4 
    isXmin = False 
    isXmax = False 
    isYmin = False 
    isYmax = False 
    isLeft= True 
def __init__(self): 
    super().__init__() 
    self.initUI() 
def initUI(self): 
    sizeObject = QDesktopWidget().screenGeometry() 
    # print(" Screen size : " + str(sizeObject.height()) + "x" + str(sizeObject.width())) 
    self.xMax=sizeObject.width()-10 
    self.yMax=sizeObject.height()-10 
    print(self.xMax) 
    print(self.yMax) 
    self.setWindowFlags(Qt.FramelessWindowHint) 
    self.setAttribute(Qt.WA_TranslucentBackground) 
    self.setStyleSheet("background-color:transparent;") 
    self.setGeometry(100, 100, 100, 100) 
    self.setWindowFlags(Qt.SplashScreen | Qt.WindowStaysOnTopHint) 
    self.setAcceptDrops(True) 
    self.label=QLabel(self) 
    self.label.setAcceptDrops(True) 
    self.pixmaps=[QPixmap('left.png'),QPixmap('stand.png'),QPixmap('right.png'),QPixmap('stand.png'),QPixmap('leftR.png'),QPixmap('standR.png'),QPixmap('rightR.png'),QPixmap('standR.png')] 
    for x in range(len(self.pixmaps)): 
     self.pixmaps[x]=self.pixmaps[x].scaled(self.size,self.size,Qt.KeepAspectRatio) 
    self.resize(self.pixmaps[2].width(),self.pixmaps[2].height()) 
    self.label.setPixmap(self.pixmaps[len(self.pixmaps)-1]) 
    self.changeTimer=QTimer(self) 
    self.changeTimer.timeout.connect(self.changeFoot) 
    self.moveTimer=QTimer(self) 
    self.moveTimer.timeout.connect(self.moving) 
    self.setAcceptDrops(True) 
    pygame.init() 
    pygame.mixer.music.load('hoi_imtemmie.mp3') 
    pygame.mixer.music.play() 
    self.show() 

def dragEnterEvent(self, event): 
    if event.mimeData().hasUrls: 
     event.accept() 
    else: 
     event.ingore() 
def dropEvent(self, event): 
    self.path=event.mimeData.urls() 
def moving(self): 
    if self.distance == 0: 
     print(self.distance) 
     print(self.x(),"x",self.y()) 
     self.label.setPixmap(self.pixmaps[1]) 
     self.moveTimer.stop() 
     self.changeTimer.stop() 
     time.sleep(3) 
     self.setMovement() 
     return 0 
    else: 
     self.move(self.x()+self.direct[0],self.y()+self.direct[1]) 
     self.distance-=1 
    if self.x()<=-10 : 
     self.distance=0 
     print("xm") 
     self.isXmin = True 
    if self.y()<=-10 : 
     self.distance=0 
     print("ym") 
     self.isYmin = True 
    if self.x()>=self.xMax: 
     self.distance=0 
     print("xM") 
     self.isXmax=True 
    if self.y()>=self.yMax : 
     self.distance=0 
     print("yM") 
     self.isYmax=True 

def setMovement(self): 
    self.direct=[0,0] 
    while(self.direct[0]==0 and self.direct[1]==0): 
     self.direct=[random.randint(-1,1),random.randint(-1,1)] 
    if self.isXmax: 
     self.direct[0]=-1 
    if self.isXmin: 
     self.direct[0]=1 
    if self.isYmin: 
     self.direct[1]=1 
    if self.isYmax: 
     self.direct[1]=-1 

    if self.direct[0]== -1: 
     self.isLeft=True 
     self.imgNum=0 
    elif self.direct[0]== 1: 
     self.isLeft=False 
     self.imgNum=4 
    self.isXmax = False 
    self.isXmin = False 
    self.isYmin = False 
    self.isYmax = False 
    # if direct[0]*direct[1]==0 : self.delta = QPoint(dX*direct[0],dY*direct[1]) 
    # else: self.delta=QPoint(direct[0]*(dX**(1/2)),direct[1]*(dY**1/2)) 
    self.distance=random.randint(200,400) 
    self.changeTimer.start(300) 
    self.moveTimer.start(30) 

def changeFoot(self): 
    self.label.setPixmap(self.pixmaps[self.imgNum]) 
    if self.isLeft: 
     if self.imgNum<self.frameCount-1: 
      self.imgNum+=1 
     else : 
      self.imgNum=0 
    else: 
     if self.imgNum<2*self.frameCount-1: 
      self.imgNum+=1 
     else : 
      self.imgNum=self.frameCount 

def mousePressEvent(self, QMouseEvent): 
    self.setMovement() 
    pygame.mixer.music.load('hoi_imtemmie.mp3') 
    pygame.mixer.music.play() 
def keyPressEvent(self, QKeyEvent): 
    if QKeyEvent.key() == Qt.Key_Escape: 
     sys.exit() 
    # if QKeyEvent.key() == Qt.Key_G: 
if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    ex = Example() 
    sys.exit(app.exec_()) 

我設置AcceptDrops(真)

self.setAcceptDrops(True) 

和代碼有關獲取文件路徑▼

def dragEnterEvent(self, event): 
    if event.mimeData().hasUrls: 
     event.accept() 
    else: 
     event.ingore() 
def dropEvent(self, event): 
    self.path=event.mimeData.urls() 

回答

0

通過event.mimeData()嘗試循環。網址()

for url in event.mimeData().urls(): 
    self.path = url.toLocalFile()