Home >Backend Development >Python Tutorial >Combo box that you must learn every day in PyQt5
This article mainly introduces the combo box that you must learn every day in PyQt5. It has a certain reference value. Interested friends can refer to it.
QComboBox is a tool that allows users to select from a list of options. A control that selects an item.
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ PyQt5 教程 这个例子展示了如何使用QComboBox部件。 作者:我的世界你曾经来过 博客:http://blog.csdn.net/weiaitaowang 最后编辑:2016年8月4日 """ import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QComboBox class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.lb1 = QLabel('黑客帝国', self) combo = QComboBox(self) combo.addItem('黑客帝国') combo.addItem('指环王') combo.addItem('复仇车联盟') combo.addItem('阿凡达') combo.addItem('X战警') combo.move(50, 50) self.lb1.move(50, 150) combo.activated[str].connect(self.onActivated) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('组合框') 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 selected option from the combo box.
combo = QComboBox(self) combo.addItem('黑客帝国') combo.addItem('指环王') combo.addItem('复仇车联盟') combo.addItem('阿凡达') combo.addItem('X战警')
We create a QComboBox widget containing five options.
combo.activated[str].connect(self.onActivated)
When the item 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() adjusts the size of the label.
After the program is executed
Related recommendations:
##PyQt5 The slider control QSlider_python## that must be learned every day
#PyQt4 implements a drop-down menu to select and print outPyQt5 must learn the switch button every day_pythonThe above is the detailed content of Combo box that you must learn every day in PyQt5. For more information, please follow other related articles on the PHP Chinese website!