Q:

pyqt5 always on top radio button not working

I made Always on top radio button with pyqt5 python.
But not working and no error. What's the problem?
When clicking radio button, pass string( "ON" or "OFF ) to func
def radioButtonClicked_always executes always on top code as string
"ON" = always on top execute
"OFF" = always on top disable
I add print("ON") for test. It also works fine.
import sys, os
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class Button(QWidget):

    def __init__(self, parent):
        super(Button, self).__init__(parent)
        self.parent = parent
        self.initUI()

    def initUI(self):
        listBox = QVBoxLayout(self)
        self.setLayout(listBox)

        listBox.addStretch(1)

        hbox = QHBoxLayout()
        group_box_2 = QGroupBox("Always on Top")
        hbox.addWidget(group_box_2)
        hbox_in = QHBoxLayout()
        group_box_2.setLayout(hbox_in)

        rb2_1 = QRadioButton("ON")
        rb2_1.clicked.connect((lambda state, x=rb2_1.text(): self.radioButtonClicked_always(x)))
        hbox_in.addWidget(rb2_1)

        rb2_2 = QRadioButton("OFF")
        rb2_2.clicked.connect((lambda state, x=rb2_2.text(): self.radioButtonClicked_always(x)))
        rb2_2.setChecked(True)
        hbox_in.addWidget(rb2_2)
        listBox.addLayout(hbox)

        self.setWindowTitle('Helper')
        self.setGeometry(300, 300, 300, 200)
        # self.show()

    def radioButtonClicked_always(self, s_state):
        if s_state == "ON":
            print("ON")
            self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
        else:
            print("OFF")
            self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)

    def closeEvent(self, event):
        self.deleteLater()

class MyApp(QMainWindow):

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

    def initUI(self):
        self.statusBar().showMessage('Ready')

        bt = Button(self)
        self.setCentralWidget(bt)
        self.setGeometry(300, 300, 300, 200)
        self.show()

    def closeEvent(self, event):
        self.deleteLater()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

A:

You are setting the window flag for the child widget "Button" (I'd suggest you to use another name, by the way), while you have to set it for the window.
Also, as the documentation about windowFlags reports:

Note: This function calls setParent() when changing the flags for a window, causing the widget to be hidden. You must call show() to make the widget visible again..

So, you have to set the flags for the widget's top level window (accessible through QWidget.window()) and also call the window's show() method right after that.

    def radioButtonClicked_always(self, s_state):
        if s_state == "ON":
            print("ON")
            self.window().setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
        else:
            print("OFF")
            self.window().setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)

        self.window().show()

Note that you shouldn't use the widget's label for comparison. For similar cases, use QButtonGroup instead: you can set an ID for each button added, and use that ID to identify which button has been toggled.
Consider that the buttonToggled signal is also sent when a button is unchecked, so you'll have to check for both the checked state and the button Id, and, in your case, set the window flags only when the checked state is True.
class Button(QWidget):
        # ...
    def initUI(self):
        listBox = QVBoxLayout(self)
        self.setLayout(listBox)

        listBox.addStretch(1)

        hbox = QHBoxLayout()
        group_box_2 = QGroupBox("Always on Top")
        hbox.addWidget(group_box_2)
        hbox_in = QHBoxLayout()
        group_box_2.setLayout(hbox_in)

        rb2_1 = QRadioButton("ON")
        hbox_in.addWidget(rb2_1)

        rb2_2 = QRadioButton("OFF")
        rb2_2.setChecked(True)
        hbox_in.addWidget(rb2_2)
        listBox.addLayout(hbox)

        buttonGroup = QButtonGroup(self)
        buttonGroup.addButton(rb2_1, 1)
        buttonGroup.addButton(rb2_2, 0)
        buttonGroup.buttonToggled[int, bool].connect(self.setOnTop)

        self.setWindowTitle('Helper')
        self.setGeometry(300, 300, 300, 200)

    def setOnTop(self, buttonId, status):
        if not status:
            return
        if buttonId:
            self.window().setWindowFlags(
                self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
        else:
            self.window().setWindowFlags(
                self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint)
        self.window().show()

