Example Analysis of combo Box in PyQt5
This article is about the sample analysis of combo boxes in PyQt5. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
QComboBox is a control that allows the user to select an item from a list of options.
#! / usr/bin/python3#-*-coding: utf-8-*-"" PyQt5 tutorial this example shows how to use QComboBox parts. Last editor: import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QLabel, QComboBoxclass Example (QWidget): def _ _ init__ (self): super (). _ init__ () self.initUI () def initUI (self): self.lb1 = QLabel ('Matrix' Self) combo = QComboBox (self) combo.addItem ('Matrix') combo.addItem ('Lord of the Rings') combo.addItem ('Avengers') combo.addItem ('Avatar') combo.addItem ('X-Men') combo.move (50,50) self.lb1.move (50150) combo.activated.connect (self.onActivated) self.setGeometry (300,300,300) Self.setWindowTitle ('combo box') self.show () def onActivated (self, text): self.lb1.setText (text) self.lb1.adjustSize () if _ _ name__ ='_ main__': app = QApplication (sys.argv) ex = Example () sys.exit (app.exec_ ())
There are five options in the combo box. The label control is used to display the options selected from the combo box.
Combo = QComboBox (self) combo.addItem ('Matrix') combo.addItem ('Lord of the Rings') combo.addItem ('Avengers') combo.addItem ('Avatar') combo.addItem ('X-Men')
We create a QComboBox part with five options.
Combo.activate [str] .connect (self.onActivated)
When the project is selected in QComboBox, we call the onActivated () method.
Def onActivated (self, text): self.lb1.setText (text) self.lb1.adjustSize ()
In the onActivated () method, we set the label control to display the text of the selected item. AdjustSize () resizes the label.
After the program is executed
Thank you for reading! This is the end of this article on "sample analysis of combo boxes in PyQt5". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!