在sys.exit(app.exec_())之前不显示任何元素
我尝试在窗口上显示图片(最多6张)。在此之前,您必须在第一个窗口中输入一个名称(在下面的代码中没有给出)。图片会自动从我的相机传输到电脑中,所以一开始文件夹中没有图片,但一段时间后可能直到6点。通过运行代码没有出现错误,但当所有6张图片都在正确的文件夹中时,窗口才会完全加载。因此,在sys.exit(app.exec_())
运行之前不会显示任何内容。
我试着在ui.update_picture(MainWindow)
之后调用sys.exit(app.exec_())
,但是在这一点上就停止了。
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 and transfer them in a new folder
if number > 0 and number < 7:
ui.update_picture(MainWindow)
print(number)
else:
recording = False
recording = False
sys.exit(app.exec_())
转载请注明出处:http://www.cospi.net/article/20230401/2117474.html