Dynamic adding of textbox in pyqt5
-
hi, guys,
i have a question about a gui project. If the user selects one or more images on the gui, the names of the images should be displayed in text boxes. Currently i have implemented 3 textboxes statically. Therefore only the names of 3 images are displayed. Is there a way to make this more dynamic ? For example, if the user selects more than 3, the gui will be extended by two more textboxes ?
I build the gui with qt designer. In the image, you can see the prototype gui . And this is the code:
def __init__(self): super(Fenster, self).__init__() loadUi(r'C:\Users\NAME\Desktop\xyz\Code_Fr_Forum\test_.ui', self) self.pushButton.clicked.connect(self.load_img) def load_img(self): filename, _ = QFileDialog.getOpenFileNames(self, 'Select Multi File', 'default', 'All Files (*)') count = len(filename) #Myidea: Depend on the number of filenames, add more than 3 lineEdit on #the GUI (dynamic). #![alt text](![image url](![image url](image url)))Now its very static for index, name in enumerate(filename, start=1): # extract file name basename = os.path.basename(name) filename_list.append(basename) if index == 1: self.lineEdit.setText(basename) elif index == 2 self.lineEdit_1.setText(basename) elif index == 3 and idx != -1: self.lineEdit_2.setText(basename) #elif index > 3: #add self.lineEdit_3 under self.lineEdit_2 self.lineEdit_3.setText(basename) ######MAIN#################### app = QApplication(sys.argv) w = Fenster() w.show() sys.exit(app.exec_())```
-
@elias_hh said in Dynamic adding of textbox in pyqt5:
Is there a way to make this more dynamic ?
Of course. You can add a list member variable where you store your line edits.
for index, name in enumerate(filename, start=1): # extract file name basename = os.path.basename(name) filename_list.append(basename) self.lineEdits += QLineEdit(self) # Make sure to set correct parent # Use the line edit you just added
Use a layout to organise all this widgets.
-
@Denni-0 Hey, I thank you for the very detailed and helpful MUC. My later GUI looks much more complex than in the photo, which I showed as an example. Therefore I create the GUI with QT Designer (static). Is there still the possibility to change the GUI I created with QT Designer dynamically and add some textboxes to my main program ?
With your MUC it is possible, but is it also possible to do this with a GUI that was created with Designer ?
-
@elias_hh I think if you want to modify your UI at runtime you will need to change your approach: do not use loadUi but instead use pyuic to generate Python code from ui file (which is XML).
Take a look at https://doc.qt.io/qtforpython/tutorials/basictutorial/uifiles.html -
Hey, guys,
Thanks for your help. I have decided to completely rethink my approach and work without Qt Designer. I will use the code from @Denni-0 as a base because it is almost exactly the way I want it to be. My gui should look like the following picture (designed with qt designer):
My idea is that the program filters the required parameters from the names of the images. I know how to do that and it works. But as you can see in the picture I need two more QLineEdits(). One for Heights and one for Width. These act as security to see if the images are named correctly. If the user has named the images according to the correct nomenclature, the parameters appear there. If the image names were named incorrectly, a question mark appears there.
But my main problem now is, I don't know how to position these textboxes next to the image name. I can only do it below. I tried with xyz.move(x,y) but the textboxes are still below. How can i solve this ?
-
@elias_hh said in Dynamic adding of textbox in pyqt5:
how to position these textboxes next to the image name
You need to put them into the correct layout, don't use move().
Please show your code and where you want to add text boxes.