Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to call a function of a pyqt5 file in another pyqt5 file function
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Osama_Billah
    wrote on last edited by Osama_Billah
    #1

    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()
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      O 1 Reply Last reply
      1
      • SGaistS SGaist

        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 ?

        O Offline
        O Offline
        Osama_Billah
        wrote on last edited by
        #3

        @SGaist din't get your point

        JonBJ 1 Reply Last reply
        0
        • O Osama_Billah

          @SGaist din't get your point

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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?

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved