class SelectorFechaVentana(QMainWindow): def __init__(self): super().__init__() self.inicializarGui() def inicializarGui(self): self.setWindowTitle('Selector Fecha') self.setFixedSize(300, 300) self.calendario = QCalendarWidget(self) self.calendario.setGridVisible(True) self.calendario.move(30, 20) self.calendario.setFixedWidth(200) self.calendario.setFixedHeight(200) self.calendario.clicked[QDate].connect(self.mostrar_fecha_seleccionada) self.lbl_fecha_seleccionada = QLabel('', self) self.lbl_fecha_seleccionada.move(30, 220) self.lbl_fecha_seleccionada.setFixedWidth(250) def mostrar_fecha_seleccionada(self, fecha): self.lbl_fecha_seleccionada.setText(fecha.toString())
class DecisionMakerControls(QWidget): # DecisionMakerControls class constructor def __init__(self, parent): # Call superclass constructor super().__init__() # Save parent instance self.parent = parent # Slider box slider_box = QVBoxLayout() slider_box.addWidget( DecisionMakerSlider('How hungry are you?', parent.setHungerLevel)) slider_box.addWidget( DecisionMakerSlider('Thirsty?', parent.setThirstLevel)) slider_box.addWidget( DecisionMakerSlider('Energy level?', parent.setEnergyLevel)) slider_box.addWidget( DecisionMakerSlider('Introversion level?', parent.setIntrovertLevel)) slider_box.addWidget( DecisionMakerSlider('Stress level?', parent.setStressLevel)) # Box containing calendar control, time control, weather control, alone toggle and retrograde toggle right_box = QVBoxLayout() # Box containing all of the above minus calendar #lowerright_box = QHBoxLayout() # Box containing labels for contorl in lower right box #lowerright_label_box = QVBoxLayout() # Box containing controls in lower right box #lowerright_control_box = QVBoxLayout() # Calendar control self.calendar = QCalendarWidget() self.calendar.setHorizontalHeaderFormat( QCalendarWidget.SingleLetterDayNames) self.calendar.setVerticalHeaderFormat(QCalendarWidget.NoVerticalHeader) self.calendar.selectionChanged.connect(self.calendarChanged) self.calendar.setFixedWidth(325) right_box.addWidget(self.calendar) # Save today's date self.today = self.calendar.selectedDate() # Initialize day and year self.parent.setDay(self.today.dayOfWeek() - 1) # Limit year to +- 100 form current year = self.today.year() if (year > 100): year = 100 elif (year < -100): year = -100 self.parent.setYear(year) # Time control time_box = QHBoxLayout() time_label = QLabel('What time is it?', self) self.time_combo = QComboBox() self.time_combo.addItems( ['1:00', '2:00', '3:00', '4:00', '5:00', '6:00', \ '7:00', '8:00', '9:00', '10:00', '11:00', '12:00' ] ) hour = QTime.currentTime().hour() - 1 hour_convert = hour if (hour_convert < 0): hour_convert = 23 if (hour_convert > 12): hour_convert -= 12 self.time_combo.setCurrentIndex(hour_convert) self.time_combo.currentIndexChanged.connect(self.comboChanged) self.ampm_combo = QComboBox() self.ampm_combo.addItems(['AM', 'PM']) if (hour > 12): self.ampm_combo.setCurrentIndex(1) else: self.ampm_combo.setCurrentIndex(0) self.ampm_combo.currentIndexChanged.connect(self.comboChanged) self.parent.setTime(hour) time_box.addWidget(time_label) time_box.addWidget(self.time_combo) time_box.addWidget(self.ampm_combo) right_box.addLayout(time_box) # Weather control weather_box = QHBoxLayout() weather_label = QLabel('What\'s the weather like?', self) self.weather_combo = QComboBox() self.weather_combo.addItems( ['Sunny', 'Cloudy', 'Raining', 'Snowing', 'Natural Disaster']) self.weather_combo.currentIndexChanged.connect(self.comboChanged) weather_box.addWidget(weather_label) weather_box.addWidget(self.weather_combo) right_box.addLayout(weather_box) # Alone toggle button alone_box = QHBoxLayout() alone_label = QLabel('Are you alone, or with someone else?', self) self.button_alone = QPushButton('Alone') self.button_alone.setCheckable(True) self.button_alone.clicked.connect(self.buttonClicked) alone_box.addWidget(alone_label) alone_box.addWidget(self.button_alone) right_box.addLayout(alone_box) # Retrograde toggle button retrograde_box = QHBoxLayout() retrograde_label = QLabel('Is Mercury in retrograde?', self) self.button_retrograde = QPushButton('Nope') self.button_retrograde.setCheckable(True) self.button_retrograde.clicked.connect(self.buttonClicked) retrograde_box.addWidget(retrograde_label) retrograde_box.addWidget(self.button_retrograde) right_box.addLayout(retrograde_box) # Color palette self.setAutoFillBackground(True) palette = self.palette() palette.setColor(self.backgroundRole(), Qt.white) self.setPalette(palette) # Set up layout hbox = QHBoxLayout() hbox.addLayout(slider_box) hbox.insertSpacing(1, 25) hbox.addLayout(right_box) hbox.setSizeConstraint(QLayout.SetFixedSize) self.setLayout(hbox) # Button event handler def buttonClicked(self): sender = self.sender() if (sender == self.button_alone): if (self.button_alone.isChecked()): self.button_alone.setText('With someone else') self.parent.setAlone(1) else: self.button_alone.setText('Alone') self.parent.setAlone(0) elif (sender == self.button_retrograde): if (self.button_retrograde.isChecked()): self.button_retrograde.setText('Maybe?') self.parent.setRetrograde(1) else: self.button_retrograde.setText('Nope') self.parent.setRetrograde(0) # Cobobox event handler def comboChanged(self): sender = self.sender() if ((sender == self.time_combo) or (sender == self.ampm_combo)): hour = self.time_combo.currentIndex() + 1 if (hour == 13): hour = 0 time = (hour + 12 * self.ampm_combo.currentIndex()) self.parent.setTime(time) if (sender == self.weather_combo): self.parent.setWeather(self.weather_combo.currentIndex()) # Calendar event handler def calendarChanged(self): sender = self.sender() if (sender == self.calendar): date = self.calendar.selectedDate() self.parent.setDay(date.dayOfWeek() - 1) year = date.year() # Limit year to +- 100 form current year = year - self.today.year() if (year > 100): year = 100 elif (year < -100): year = -100 self.parent.setYear(year)