📌  相关文章
📜  PyQt5 – QComboBox

📅  最后修改于: 2022-05-13 01:55:48.528000             🧑  作者: Mango

PyQt5 – QComboBox

QComboBox是 PyQt5 中的一个小部件,用于从列表中进行选择。当我们单击组合框时,它占用的空间最小,一个下拉列表可以从中选择项目。 QComboBox类用于在应用程序中添加组合框。下面是组合的外观表示 -

例子 :
在此我们将在窗口中创建一个组合框,允许我们在“Geek”、“Super Geek”和“Ultra Geek”之间进行选择

下面是代码——

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
    # method for widgets
    def UiComponents(self):
  
        # creating a combo box widget
        combo_box = QComboBox(self)
  
        # setting geometry of combo box
        combo_box.setGeometry(200, 150, 120, 40)
  
        # adding items to combo box
        combo_box.addItem("Geek")
        combo_box.addItem("Super Geek")
        combo_box.addItem("Ultra Geek")
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :