This repository has been archived on 2024-08-23. You can view files and clone it, but cannot push or open issues or pull requests.
Files
lessons/Python/pyqt6_4/snippets/7.py

64 lines
1.4 KiB
Python
Executable File

import sys
from PyQt6.QtCore import QSize
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import (
QApplication,
QHBoxLayout,
QLabel,
QRadioButton,
QVBoxLayout,
QWidget,
)
ICON_SIZE = 100
class Window(QWidget):
def __init__(self) -> None:
super().__init__()
self.setGeometry(200, 200, 400, 300)
self.setWindowTitle("Окно")
self.create_radio()
def create_radio(self) -> None:
hbox = QHBoxLayout()
rad1 = QRadioButton("Cat 1")
rad1.setIcon(QIcon("cat1.jpg"))
rad1.setIconSize(QSize(ICON_SIZE, ICON_SIZE))
rad1.toggled.connect(self.radio_selected)
rad2 = QRadioButton("Cat 2")
rad2.setIcon(QIcon("cat2.jpg"))
rad2.setIconSize(QSize(ICON_SIZE, ICON_SIZE))
rad2.toggled.connect(self.radio_selected)
hbox.addWidget(rad1)
hbox.addWidget(rad2)
vbox = QVBoxLayout()
self.label = QLabel("")
vbox.addWidget(self.label)
vbox.addLayout(hbox)
self.setLayout(vbox)
def radio_selected(self) -> None:
radio_btn = self.sender()
if radio_btn.isEnabled():
self.label.setText(f"You have chosen {radio_btn.text().lower()}")
def main() -> None:
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()