Create Folder with browse button and get name of folder from a qlineedit text pyqt

107 Views Asked by At

as a sub-part of my pyqt dashboard project ,i am stuck with a problem, where i use button titled browse to open a dialog to select images, and a qlineedit to give the person name and id , what i would want my application to do is,

  1. if its the first time for a particular name , then it creates a folder with the name from the qlineedit and inside the folder it pastes the images i select from the browse button.

  2. If a folder is already present with the name just add the image in the folder with the name.

I am attaching what i have tried till now .

def person_Details(self):
    self.add_area.setStyleSheet('background-color:white')
    self.v_parent_layout = QVBoxLayout()
    self.v_layout = QVBoxLayout()
    self.H_layout = QHBoxLayout()
    #In here inside the QVBoxlayout
    self.q_form = QFormLayout()
    self.q_form_box  = QGroupBox("Enter Your Details")
    self.nameLineEdit = QLineEdit()
    self.IDLineEdit = QLineEdit()
    self.q_form.addRow(QLabel("Name"), self.nameLineEdit)
    self.q_form.addRow(QLabel("ID"), self.IDLineEdit)
    self.q_form_box.setLayout(self.q_form)
    self.v_layout.addWidget(self.q_form_box)
    #In here inside the QHBoxlayout
    self.save = QPushButton("SAVE")
    self.Browse = QPushButton("BROWSE")
    self.Browse.clicked.connect(self.getImage)
    self.save.clicked.connect(self.show_entries)
    self.checkbox = QCheckBox("CHECK",self)
    self.H_layout.addWidget(self.save)
    self.H_layout.addWidget(self.Browse)
    self.H_layout.addWidget(self.checkbox)
    self.checkbox.stateChanged.connect(self.clickBox)
    #coming Outside
    self.v_parent_layout.layout().addLayout(self.v_layout)
    self.v_parent_layout.layout().addLayout(self.H_layout)
    self.add_area.setLayout(self.v_parent_layout)





def getImage(self):
    import os
    desktop = os.getlogin()
    self.fname = QFileDialog.getOpenFileNames(self, 'Open file', os.getcwd(), "Image files (*.jpg *.gif *.jpeg)")
    try:
        imagePath = self.fname[0][0]
        pixmap = QPixmap(imagePath)
        self.pic_1.setPixmap(QPixmap(pixmap))
        self.pic_2.setPixmap(QPixmap(pixmap))
        self.pic_3.setPixmap(QPixmap(pixmap))
        self.pic_4.setPixmap(QPixmap(pixmap))
        self.resize(pixmap.width(), pixmap.height())
    except IndexError as e:
        print(e)

for creating the folder and saving the image i have done the following:

from PIL import Image
import os
from CONSTANT import *

name_of_folder = "some_name"
absolute_path = os.path.join(path,name_of_folder)
# i=0
if not os.path.exists(absolute_path):
    os.makedirs(absolute_path)
    # i=i+1
image = Image.open("/home/Downloads/image_1.jpg")
image.save(f"{absolute_path}/{name_of_folder}.jpg")


0

There are 0 best solutions below