PyQt5 - 更改单选按钮的状态
在本文中,我们将看到如何更改单选按钮的状态。虽然借助setChecked
方法我们可以使单选按钮处于选中状态,但是当我们在选中的单选按钮上使用此方法时,单选按钮的状态不会发生变化。
In order to change the state of radio button when push button is pressed we have to do the following –
1. Create a radio button
2. Create a push button
3. Connect a method to push button such that when push button get pressed method get called
4. In the calling method with the help of nextCheckState
method change the state of radio button.
下面是工具。
# 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("Radio Button")
# creating push button
push = QPushButton("Press", self)
# setting geometry of push button
push.setGeometry(200, 200, 100, 40)
# connect method to push button
push.clicked.connect(self.method)
def method(self):
# changing the state of radio button
self.radio_button.nextCheckState()
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :