Mit einer QComboBox.
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QComboBox, QLabel, QVBoxLayout, QWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Dropdown mit PyQt")
self.setGeometry(100, 100, 400, 200)
layout = QVBoxLayout()
self.label = QLabel("Waehle eine Option:")
layout.addWidget(self.label)
self.combo_box = QComboBox()
self.combo_box.addItem("Option 1")
self.combo_box.addItem("Option 2")
self.combo_box.addItem("Option 3")
self.combo_box.addItem("Option 4")
self.combo_box.currentIndexChanged.connect(self.on_combobox_changed)
layout.addWidget(self.combo_box)
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
def on_combobox_changed(self, index):
selected_option = self.combo_box.currentText()
self.label.setText(f"Ausgewaehlte Option: {selected_option}")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())