Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to call a function of a pyqt5 file in another pyqt5 file function



  • Hi everyone, My project is almost complete. I'm facing one more problem. I have three python files. Now I want that if I clicked on the image it goes to ImageViewer file to display the image. the code is below. when I call the function like File1.item_images_clicked. it does not work.

    
    import Image_viewer_class
    def item_images_clicked(self):
    
            path1 = 'C:/Users/OB/Desktop/DevoMech Project/Python/data/actual_data/'
            path2 = ''
            # print('hi')
            test = self.dlistWidget.currentItem().text()
            test1 , act_name = test.split('thumdnail')
            # print(act_name)
    
            if (test == ('thumdnail'+ act_name)):
                path2 = path1 + act_name
                return path2
    

    other file code:

    import File1 
    image_view1 = File1.item_images_clicked()
    def open(self):
            image_from_main = image_view1
            options = image_from_main.path2
            # fileName = QFileDialog.getOpenFileName(self, "Open File", QDir.currentPath())
            fileName, _ = QFileDialog.getOpenFileName(self, 'QFileDialog.getOpenFileName()', '',
                                                      'Images (*.png *.jpeg *.jpg *.bmp *.gif)', options=options)
            if fileName:
                image = QImage(fileName)
                if image.isNull():
                    QMessageBox.information(self, "Image Viewer", "Cannot load %s." % fileName)
                    return
    
                self.imageLabel.setPixmap(QPixmap.fromImage(image))
                self.scaleFactor = 1.0
    
                self.scrollArea.setVisible(True)
                self.printAct.setEnabled(True)
                self.fitToWindowAct.setEnabled(True)
                self.updateActions()
    

    and when I use this code it show the other window but the path2 is not moving:

                self.window = QtWidgets.QMainWindow()
                self.ui = Ui_MainWindow()
                self.ui.setupUi(self.window)
                self.window.show()
                OsamaWindow.hide()
    

  • Lifetime Qt Champion

    Hi,

    Can you even run that code ?

    Your logic is strange, if you want to call a function as a result of a click why do you put it away like that in an unrelated file ?



  • @SGaist din't get your point



  • @Osama_Billah
    Like @SGaist, I struggle to understand whatever logic you are using in your code. His obvious question is: if you have code you want executed on a click in a given file/module, why don't you define the click handler in that file instead of in some other file, which can only lead to complications?

    Let's start with:

    image_view1 = File1.item_images_clicked()
    

    What is this line doing on its own in a file outside of any class/def?? This means it will execute, and therefore try to access self.dlistWidget.currentItem(), at whatever instant this file/module happens to first get imported in some other file....

    Also

    import Image_viewer_class
    import File1 
    

    You say you have three Python files, you show us snippets apparently from two files, but do not tell us what these files are named. We (I) do not know what those imports refer to. We (I) are not mind-readers, which files are named what?


Log in to reply