|
| 1 | +import sys |
| 2 | + |
| 3 | +from PyQt5.QtWidgets import QWidget, QApplication, QCheckBox, QLineEdit, QLabel, QSlider, QLCDNumber, QCalendarWidget, \ |
| 4 | + QGroupBox, QRadioButton, QVBoxLayout |
| 5 | +from PyQt5.QtCore import Qt |
| 6 | + |
| 7 | + |
| 8 | +class Example(QWidget): |
| 9 | + def __init__(self): |
| 10 | + super().__init__() |
| 11 | + self.initUI() |
| 12 | + |
| 13 | + def initUI(self): |
| 14 | + |
| 15 | + self.addCheckBox() |
| 16 | + self.addLineEdit() |
| 17 | + self.addSlider() |
| 18 | + self.addCalendar() |
| 19 | + self.addRadioBoton() |
| 20 | + |
| 21 | + self.resize(640, 320) |
| 22 | + self.setWindowTitle('Controles PyQT 5 by Tutor de Programacion') |
| 23 | + |
| 24 | + def addRadioBoton(self): |
| 25 | + gbx = QGroupBox('Tu Lenguaje Favorito', self) |
| 26 | + gbx.setGeometry(20, 150, 185, 120) |
| 27 | + # crear tres QRadioButton |
| 28 | + radio1 = QRadioButton("C/C++") |
| 29 | + radio2 = QRadioButton("Python") |
| 30 | + radio3 = QRadioButton("Java") |
| 31 | + # agregar los widgets al layout vertical |
| 32 | + vbox = QVBoxLayout(self) |
| 33 | + vbox.addWidget(radio1) |
| 34 | + vbox.addWidget(radio2) |
| 35 | + vbox.addWidget(radio3) |
| 36 | + # establecer el layout del QGroupBox |
| 37 | + gbx.setLayout(vbox) |
| 38 | + |
| 39 | + # radio1.setChecked(True) |
| 40 | + # print(radio1.isChecked()) |
| 41 | + |
| 42 | + |
| 43 | + def addCalendar(self): |
| 44 | + cal = QCalendarWidget(self) |
| 45 | + cal.setGridVisible(True) |
| 46 | + cal.clicked.connect(self.timeSelected) |
| 47 | + cal.move(300, 20) |
| 48 | + |
| 49 | + def timeSelected(self, date): |
| 50 | + print('La Fecha es: ', date.toString()) |
| 51 | + |
| 52 | + def addSlider(self): |
| 53 | + sld = QSlider(Qt.Horizontal, self) |
| 54 | + # establecer posicion (x,y) y dimenciones (ancho, alto) |
| 55 | + sld.setGeometry(20, 80, 150, 30) |
| 56 | + # indicar rango de valores validos |
| 57 | + sld.setRange(50, 250) |
| 58 | + # establecer valor inicial |
| 59 | + sld.setValue(120) |
| 60 | + # evento producido cada vez que cambia el valor |
| 61 | + sld.valueChanged.connect(self.valChange) |
| 62 | + |
| 63 | + num = QLCDNumber(self) |
| 64 | + num.setGeometry(180, 80, 50, 30) |
| 65 | + # mostrar valor inicial |
| 66 | + num.display(sld.value()) |
| 67 | + # cambiar valor QLCDNumber cuando cambiar QSlider |
| 68 | + sld.valueChanged.connect(num.display) |
| 69 | + |
| 70 | + def valChange(self, value): |
| 71 | + print('QSlider value: ', value) |
| 72 | + |
| 73 | + def addLineEdit(self): |
| 74 | + # crea un texto no es editable |
| 75 | + lbl = QLabel('Nombre:', self) |
| 76 | + lbl.move(20, 52) |
| 77 | + |
| 78 | + # crea un widget que permite editar una linea de texto |
| 79 | + txt = QLineEdit(self) |
| 80 | + txt.move(72, 50) |
| 81 | + # establece un texto informativo |
| 82 | + txt.setPlaceholderText('Dime tu nombre') |
| 83 | + # evento producido cada vez que cambia el texto |
| 84 | + txt.textChanged.connect(self.textChange) |
| 85 | + # establece el foco en el widget |
| 86 | + txt.setFocus() |
| 87 | + # obtiene el texto escrito |
| 88 | + nombre = txt.text() |
| 89 | + |
| 90 | + def textChange(self, text): |
| 91 | + print('El nuevo texto es: ', text) |
| 92 | + |
| 93 | + |
| 94 | + def addCheckBox(self): |
| 95 | + # Crear el widget |
| 96 | + cbx = QCheckBox('Mostrar/Ocultar', self) |
| 97 | + # Cambiar estado del CheckBox puede ser: Qt.PartiallyChecked, Qt.Checked, Qt.Unchecked |
| 98 | + cbx.setCheckState(Qt.PartiallyChecked) |
| 99 | + # Responder al evento de cambio de estado |
| 100 | + cbx.stateChanged.connect(self.cbxChange) |
| 101 | + # Ubicar el control en la posicion (x, y) |
| 102 | + cbx.move(20, 20) |
| 103 | + |
| 104 | + def cbxChange(self, state): |
| 105 | + print('CheckBox State: ', state) |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == '__main__': |
| 109 | + app = QApplication(sys.argv) |
| 110 | + win = Example() |
| 111 | + win.show() |
| 112 | + sys.exit(app.exec_()) |
0 commit comments