📜  PyQt5 - 向单选按钮添加动作

📅  最后修改于: 2022-05-13 01:55:30.920000             🧑  作者: Mango

PyQt5 - 向单选按钮添加动作

在本文中,我们将了解如何为单选按钮设置操作。为单选按钮设置操作意味着向其添加一个操作,当单选按钮被选中或取消选中时,该操作被调用并执行某些任务。

为了向单选按钮添加操作,我们将使用toggled.connect方法。

下面是实现。

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
    # method for widgets
    def UiComponents(self):
  
        # creating a radio button
        self.radio_button = QRadioButton(self)
  
        # setting geometry of radio button
        self.radio_button.setGeometry(200, 150, 120, 40)
  
        # setting text to radio button
        self.radio_button.setText("GEEK ?")
  
        # creating label to display if it is checked or not
        self.label = QLabel("", self)
  
        # setting geometry of label
        self.label.setGeometry(200, 200, 150, 40)
  
        # adding action to radio button
        self.radio_button.toggled.connect(self.action)
  
    # method called by radio button
    def action(self):
  
        # changing the content of label
        self.label.setText("Action performed")
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :