Wednesday, April 27, 2022

Checkbox, Radiobutton and ComboBox in PyQt6

This demo program shows how to create checkbox, radiobutton and combobox and place them at certain location in the window.

The output:



The Code:


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import sys
import os
from PyQt6.QtWidgets import QMainWindow, QApplication,  QWidget, QComboBox, QLineEdit, QLabel, QRadioButton, QButtonGroup, QCheckBox, QHBoxLayout, QVBoxLayout
from PyQt6 import QtCore, QtGui

class Window(QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        
 
        
        self.initUI()

    def initUI(self):
        self.setCentralWidget(QWidget(self))
        self.line_edit = QLineEdit(self)
        self.line_edit.setGeometry(25, 25, 150, 40)
        self.line_edit.setFocusPolicy(QtCore.Qt.FocusPolicy.StrongFocus)
        self.line_edit.setFocus()
        self.line_edit.setPlaceholderText('Enter Text Here')
        self.linelabel = QLabel('Entered Text Here', self)
        self.linelabel.setGeometry(25, 35, 180, 120)
        self.linelabel.setWordWrap(True)
        
        self.langs ={'c':0, 'cpp':0, 'java':0, 'python':0}
        
        self.checkBox_c = QCheckBox(self)
        self.checkBox_c.setGeometry(25, 155, 81, 20)
        self.checkBox_c.stateChanged.connect(self.checkedc)
          
        self.checkBox_cpp = QCheckBox(self)
        self.checkBox_cpp.setGeometry(116, 155, 81, 20)
        self.checkBox_cpp.stateChanged.connect(self.checkedcpp)
          
        self.checkBox_java = QCheckBox(self)
        self.checkBox_java.setGeometry(207, 155, 81, 20)
        self.checkBox_java.stateChanged.connect(self.checkedjava)
          
        self.checkBox_py = QCheckBox(self)
        self.checkBox_py.setGeometry(298, 155, 81, 20)
        self.checkBox_py.stateChanged.connect(self.checkedpy)
        
        self.checkBox_c.setText("C")
        self.checkBox_cpp.setText( "C++")
        self.checkBox_java.setText("Java")
        self.checkBox_py.setText("Python")
        
        self.clabel1 = QLabel(self)
        self.clabel1.setGeometry(25, 130, 200, 20)
        self.clabel1.setText("Select the Language you know:")
        
        
        
        self.radioButton_male = QRadioButton(self) 
        self.radioButton_male.setGeometry(45, 200, 81, 20)
        self.radioButton_male.toggled.connect(self.maleselected)
                
        self.radioButton_female = QRadioButton(self) 
        self.radioButton_female.setGeometry(45, 225, 81, 20)        
        self.radioButton_female.toggled.connect(self.femaleselected)
        
        self.radioButton_small = QRadioButton(self) 
        self.radioButton_small.setGeometry(215, 200, 81, 20)
        self.radioButton_small.toggled.connect(self.smallselected)
        self.radioButton_medium = QRadioButton(self)
        self.radioButton_medium.setGeometry(215, 225, 81, 20)        
        self.radioButton_medium.toggled.connect(self.mediumselected)         
        self.radioButton_large = QRadioButton(self)  
        self.radioButton_large.setGeometry(215, 245, 81, 20)
        self.radioButton_large.toggled.connect(self.largeselected)
        
        self.label = QLabel(self)
        self.label.setGeometry(45, 250, 150, 20)
        self.slabel = QLabel(self)
        self.slabel.setGeometry(215, 270, 150, 20)
        self.radioButton_male.setText("Male")
        self.label.setText("Select your gender:")
        self.radioButton_female.setText("Female")
        
        self.radioButton_small.setText("Small")
        self.radioButton_medium.setText("Medium")
        self.slabel.setText("Select your size:")
        self.radioButton_large.setText("Large")
        gender_group=QButtonGroup(self)
        size_group=QButtonGroup(self)
                      
        
        
        gender_group.addButton(self.radioButton_male)
        gender_group.addButton(self.radioButton_female)
        
        size_group.addButton(self.radioButton_small)
        size_group.addButton(self.radioButton_medium)
        size_group.addButton(self.radioButton_large)
        
        
        self.combo_box = QComboBox(self)
        self.combo_box.setGeometry(125, 315, 120, 20)
        self.combo_box.addItem("Black")
        self.combo_box.addItem("Blue")
        self.combo_box.addItem("Brown")
        self.combo_box.addItem("Red")
        
        self.combo_box_label = QLabel(self)
        self.combo_box_label.setGeometry(25, 315, 100, 20)
        self.combo_box_label.setText("Select color:")
        self.combo_box_label_s = QLabel(self)
        self.combo_box_label_s.setGeometry(255, 315, 100, 20)
        self.line_edit.returnPressed.connect(lambda: self.do_action())
        self.combo_box.activated.connect(self.on_combobox_changed)
        self.setGeometry(25, 45, 400, 360)
        self.setWindowTitle('Post 26')
        self.show()
        
    def on_combobox_changed(self, value):
        print("combobox changed", value)
        self.combo_box_label_s.setText(self.combo_box.currentText())
    def mouseMoveEvent(self, e):

        x = int(e.position().x())
        y = int(e.position().y())

        #text = f'x: {x},  y: {y}'
        #self.linelabel.setText(text)
        self.line_edit.setFocus()
            
    def maleselected(self, selected):
        if selected:
            self.label.setText("You are male")
             
    def femaleselected(self, selected):
        if selected:
            self.label.setText("You are female")
            
    def smallselected(self, selected):
        if selected:
            self.slabel.setText("Your size is small")
             
    def mediumselected(self, selected):
        if selected:
            self.slabel.setText("Your size is medium")
    def largeselected(self, selected):
        if selected:
            self.slabel.setText("Your size is large")       
            
    def checkedc(self, checked):
        if checked:
            self.langs['c']= 1
        else:
            self.langs['c']= 0
        self.show1()
  
    def checkedcpp(self, checked):
        if checked:
            self.langs['cpp']= 1
        else:
            self.langs['cpp']= 0    
        self.show1()
              
    def checkedjava(self, checked):
        if checked:
            self.langs['java']= 1
        else:
            self.langs['java']= 0
        self.show1()
              
    def checkedpy(self, checked):
        if checked:
            self.langs['python']= 1
        else:
            self.langs['python']= 0
        self.show1() 
    
    def show1(self):
        checkedlangs =', '.join([key for key in self.langs.keys()
                                         if self.langs[key]== 1]) 
          
        # Updates message having list of all selected languages. 
        self.clabel1.setText("You know "+checkedlangs)
    def do_action(self):

        value = self.line_edit.text()
        self.linelabel.setText(value)
       
 
        


def main():

    app = QApplication(sys.argv)
    ex = Window()
    ex.show()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()

No comments:

Post a Comment