0

I have an app that has a main window made with Eel. From it, with a button, you can call another script ("open_dragAndDrop()" function), where PyQt5 was used, which opens a new window where you can drag and drop files inside, showing a list of all the paths of the files you have dragged on it. The issue is that whenever I closed the second window (the drag & drop window) it would finish the process, even though the main window was still opened, so I added "self.destroy()" inside the function that gets called when all the files have been dragged ("getSelectedItem"), but now I have the opposite problem. The process keeps running even after the main windows has been closed. "self.close()" doesn't work either. And once I closed the second window, I can't open it again from the button in the main window. If I close the main window without having opened the Drag&Drop window, the process finishes correctly.

I simplified the code. This would be the main window:

import eel,sys,socket
from PyQt5.QtWidgets import QApplication, QMainWindow

from dragAndDrop import open_dragAndDrop


class IndexEel(QMainWindow):
    
    @eel.expose
    def get_data():
        open_dragAndDrop()

    eel.init("web")
    
def find_open_ports():
    PORT = 8000
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
            res = sock.connect_ex(('localhost', PORT))
            if res != 0:
                print("Using port: ",PORT)
                return PORT
            else:
                for port in range(8001, 8080):
                    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
                        res = sock.connect_ex(('localhost', port))
                        if res != 0:
                            print("Using port: ",port)
                            return port

eel.start("index.html", host="localhost", port=find_open_ports(), size=(900,700))



if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = IndexEel()
    demo.show()
    sys.exit(app.exec_())   

And this would be the Drag & Drop window:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QListWidget, QListWidgetItem, QPushButton, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont

links = []
paths = []


class ListBoxWidget(QListWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setAcceptDrops(True)
        self.resize(1150, 400)

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls:
            event.accept()
        else:
            event.ignore()

    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(Qt.CopyAction)
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):

        if event.mimeData().hasUrls():
            event.setDropAction(Qt.CopyAction)
            event.accept()

            links = []

            for url in event.mimeData().urls():
                # https://doc.qt.io/qt-5/qurl.html
                if url.isLocalFile():
                    archivoLocalFile = str(url.toLocalFile())
                    paths.append(archivoLocalFile)
                    print(paths)
                    links.append(archivoLocalFile.split('/')[-1])
                    
                else:
                    archivoToString = str(url.toString())
                    links.append(archivoToString.split('/')[-1])
                    
            
            self.addItems(links)
        else:
            event.ignore()
        return links

class AppDemo(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(1200, 600)
        self.setWindowFlag(Qt.WindowCloseButtonHint, False)


        global listbox_view
        listbox_view = ListBoxWidget(self)
        listbox_view.setGeometry(25, 25, 1150, 450)
        listbox_view.setFont(QFont('Arial', 30, QFont.Bold ))

        self.btnProcesar = QPushButton('Continue', self)
        self.btnProcesar.setGeometry(975, 500, 200, 50)
        self.btnProcesar.setStyleSheet('background-color : lightgreen')
        self.btnProcesar.setFont(QFont('Arial', 15, QFont.Bold))
        self.btnProcesar.clicked.connect(lambda: print(self.getSelectedItem()))

    def getSelectedItem(self):
        item = QListWidgetItem(listbox_view.currentItem())
        split = item.text().split('/')
        self.destroy()
        return split[-1]
        
    
def open_dragAndDrop():

    app = QApplication(sys.argv)

    demo = AppDemo()
    demo.setWindowFlags(demo.windowFlags() |Qt.WindowStaysOnTopHint)  
    demo.show() 
    demo.setWindowFlags(demo.windowFlags() & ~Qt.WindowStaysOnTopHint)
    demo.show() 

    sys.exit(app.exec_())

        

I tried to explain myself as best as I could, but If there is something missing please let me know!

musicamante
  • 41,230
  • 6
  • 33
  • 58
Agusms
  • 17
  • 4

0 Answers0