PyQt5 combobox QComboBox

by Atakan

As you know, combobox is a very popular ui component.
It is a component that allows us to show the options of a category collectively and select one of them.

Let’s move on to its use.

        self.combobox1  = QComboBox(self)
        self.combobox1.resize(100, 25)
        self.combobox1.addItem("Select One")
        self.combobox1.addItem("Blue")
        self.combobox1.addItem("Black")
        self.combobox1.addItem("Yellow")
        self.combobox1.move(10, 10)
        self.combobox1.currentTextChanged.connect(self.handleCombobox1)
        self.combobox2  = QComboBox(self)
        self.combobox2.resize(100, 25)
        self.combobox2.addItem("Select One")
        self.combobox2.addItem("Blue")
        self.combobox2.addItem("Black")
        self.combobox2.addItem("Yellow")
        self.combobox2.move(10, 40)
    def handleCombobox1(self,value):
        self.label1_combobox1.setText(value)

    def handleCombobox2btnClick(self):
        self.label2_combobox2.setText(self.combobox2.currentText())

Result:

Full source code:

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
import sys

W_WIDTH = 400
W_HEIGHT = 250
W_X = 400
W_Y = 150


class CreateButton:
    def __init__(self, window, text, width, height, x, y):
        self.button = QtWidgets.QPushButton(window)
        self.button.setText(text)
        self.button.resize(width, height)
        self.button.move(x, y)

    def get_instance(self):
        return self.button


class MainFrame(QMainWindow):
    def __init__(self):
        super(MainFrame, self).__init__()
        self.init()
        self.show()


    def init(self):
        self.resize(W_WIDTH, W_HEIGHT)
        self.move(W_X, W_Y)
        self.setWindowTitle("www.langpy.com | Python GUI Tutorial")

        self.combobox1  = QComboBox(self)
        self.combobox1.resize(100, 25)
        self.combobox1.addItem("Select One")
        self.combobox1.addItem("Blue")
        self.combobox1.addItem("Black")
        self.combobox1.addItem("Yellow")
        self.combobox1.move(10, 10)
        self.combobox1.currentTextChanged.connect(self.handleCombobox1)

        self.label1_combobox1 = QLabel(self)
        self.label1_combobox1.setText("")
        self.label1_combobox1.move(125, 10)


        self.combobox2  = QComboBox(self)
        self.combobox2.resize(100, 25)
        self.combobox2.addItem("Select One")
        self.combobox2.addItem("Blue")
        self.combobox2.addItem("Black")
        self.combobox2.addItem("Yellow")
        self.combobox2.move(10, 40)

        self.label2_combobox2= QLabel(self)
        self.label2_combobox2.setText("")
        self.label2_combobox2.move(260, 40)


        self.button_question = CreateButton(self, "Show Value", 120, 25, 120, 40).get_instance()
        self.button_question.clicked.connect(self.handleCombobox2btnClick)

    def handleCombobox1(self,value):
        self.label1_combobox1.setText(value)

    def handleCombobox2btnClick(self):
        self.label2_combobox2.setText(self.combobox2.currentText())

app = QApplication(sys.argv)
win = MainFrame()
sys.exit(app.exec_())

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. OK Read More