Python Forum
[PyQt] How to access a label in an other def ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] How to access a label in an other def ?
#1
Hi,

I try to show pictures (max.6) on a window. Before that you have to enter a name in the first Window (not given in the following code)
The pictures are transferred live by my camera so at the beginning no picture will be in the folder but after some time maybe until 6.

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon, QPixmap
import sys
import os
import time


path = "C:/Users/benni/Desktop/"                   # given from the first Window
folder_name = "exit_folder_test"                   # given from the first Window
source = "test"                                    # given
os.makedirs(path + folder_name)



class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.resize(1920, 1080)
        self.frame = QtWidgets.QWidget(MainWindow)
        start_x = 20                                # origin from the grid
        start_y = 80                                
        distance = 40                               # distance between the pictures
        global image_width
        image_width = 600                                                       
        global image_height
        image_height = 400

        self.picture1 = QtWidgets.QLabel(self.frame)                                    # all the different fields for the pictures, grid-layout
        self.picture1.setGeometry(QtCore.QRect(start_x, start_y, image_width, image_height))
        
        self.picture2 = QtWidgets.QLabel(self.frame)
        self.picture2.setGeometry(QtCore.QRect((start_x + image_width + distance), start_y, image_width, image_height))
        
        self.picture3 = QtWidgets.QLabel(self.frame)
        self.picture3.setGeometry(QtCore.QRect((start_x + 2*image_width + 2*distance), start_y, image_width, image_height))
        
        self.picture4 = QtWidgets.QLabel(self.frame)
        self.picture4.setGeometry(QtCore.QRect((start_x), (start_y + image_height + distance), image_width, image_height))
        
        self.picture5 = QtWidgets.QLabel(self.frame)
        self.picture5.setGeometry(QtCore.QRect((start_x + image_width + distance), (start_y + image_height + distance), image_width, image_height))
        
        self.picture6 = QtWidgets.QLabel(self.frame)
        self.picture6.setGeometry(QtCore.QRect((start_x + 2*image_width + 2*distance), (start_y + image_height + distance), image_width, image_height))
        
        self.print_button = QtWidgets.QPushButton(self.frame)       
        self.print_button.setGeometry(QtCore.QRect((start_x + 3*image_width + 2*distance) - 74, (start_y + 2*image_height + 2*distance), 75, 24)) 
        MainWindow.setCentralWidget(self.frame)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def update_picture(self, MainWindow):                               # funktions to add pictures to the fields in setupUi
        pixmap1 = QPixmap(path + folder_name + "/image1.jpg").scaled(image_width, image_height)
        self.picture1.setPixmap(pixmap1)
        pixmap2 = QPixmap(path + folder_name + "/image2.jpg").scaled(image_width, image_height)
        self.picture2.setPixmap(pixmap2)
        pixmap3 = QPixmap(path + folder_name + "/image3.jpg").scaled(image_width, image_height)
        self.picture3.setPixmap(pixmap3)
        pixmap4 = QPixmap(path + folder_name + "/image4.jpg").scaled(image_width, image_height)
        self.picture4.setPixmap(pixmap4)
        pixmap5 = QPixmap(path + folder_name + "/image5.jpg").scaled(image_width, image_height)
        self.picture5.setPixmap(pixmap5)
        pixmap6 = QPixmap(path + folder_name + "/image6.jpg").scaled(image_width, image_height)
        self.picture6.setPixmap(pixmap6)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Fotobox"))
        self.print_button.setText(_translate("MainWindow", "print"))



if __name__ == "__main__":
    
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    
    recording = True
    amount = 0
    number = 0
    while recording == True:
        pictures = os.listdir(path + source)         # reads the content of the given folder
        amount_update = len(pictures)         
        if amount != amount_update:                  # checks if the amount of files in the folder has changed
            amount = amount_update 
            for picture_name in pictures:                 
                number += 1
                time.sleep(1)                                   # wait until the picture is transferred
                picture_new_name = "image" + str(number) + ".jpg"
                os.rename(path + source + "/" + picture_name, path + folder_name + "/" + picture_new_name)   # renames all the given pictures
                if number > 0 and number < 7:           
                    ui.update_picture(MainWindow)
                else:
                    recording = False


        sys.exit(app.exec_())
The debugger shows me no error but the window does not load and no pictures are shown (they are renamed and transferred in the correct folder)
So I think the problem is that I can not access the picture1 ... labels in the update_picture function?
When this is the case, how can I fix this and when not, what is the problem?
Reply
#2
sys.exit(app.exec_()) is in your while loop so i guess it just exits as soon as you went through the first iteration. move it out of the while loop.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020