Skip to content

Instantly share code, notes, and snippets.

@niftycode
Created July 15, 2021 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niftycode/4691674937e5382b721fc2c1675848e4 to your computer and use it in GitHub Desktop.
Save niftycode/4691674937e5382b721fc2c1675848e4 to your computer and use it in GitHub Desktop.
Create a second window containing QRadioButtons
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Create a second window (QWidget)
Version: 1.0
Python 3.8+
Date created: July 15th, 2021
Date modified: -
"""
import sys
from PyQt5.QtWidgets import (QApplication,
QLabel,
QMainWindow,
QPushButton,
QRadioButton,
QVBoxLayout,
QHBoxLayout,
QWidget)
class AnotherWindow(QWidget):
def __init__(self):
super().__init__()
vbox = QVBoxLayout()
hbox = QHBoxLayout()
rb1 = QRadioButton("1", self)
rb1.toggled.connect(self.updateLabel)
rb2 = QRadioButton("2", self)
rb2.toggled.connect(self.updateLabel)
self.label = QLabel("1 or 2?")
hbox.addWidget(rb1)
hbox.addWidget(rb2)
vbox.addLayout(hbox)
vbox.addWidget(self.label)
self.setLayout(vbox)
self.setGeometry(250, 250, 250, 250)
self.setWindowTitle('QRadioButton')
def updateLabel(self, value):
rbtn = self.sender()
if rbtn.isChecked() == True:
self.label.setText(rbtn.text())
MainWindow.show_value(rbtn.text())
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.w = AnotherWindow()
self.button = QPushButton("Show another window")
self.button.clicked.connect(self.toggle_window)
self.setCentralWidget(self.button)
def toggle_window(self, checked):
if self.w.isVisible():
self.w.hide()
else:
self.w.show()
def show_value(value):
print(f"Value = {value}")
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment