Как в PyQt5 решить проблему с использованием radioButton
Программа имеет радио кнопки RB_1 и RB_2. При выборе любой из них можно, используя кнопки плюс и минус, выводить в label_1 и label_2 число.
Каждое нажатие кнопок плюс и минус прибавляет или вычитает единицу.
Можно заметить, что если несколько раз просто переключиться между радио кнопками, то счет меняется не на единицу, а больше. Т.е. радио кнопки вмешиваются в счет, чего быть не должно. И радио кнопка NONE тоже ведет себя не нормально. Что надо исправить, чтобы решить проблему?
main.py
import sys
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUi
from PyQt5.QtCore import QSettings
CONFIG_FILE_NAME = 'config.ini'
class New(QMainWindow):
def __init__(self):
super(New, self).__init__()
loadUi("clickB_save.ui", self)
self.setWindowTitle('New')
self.EXIT.clicked.connect(self.exit_action)
self.RB_1.clicked.connect(self.vetka_1)
self.RB_2.clicked.connect(self.vetka_2)
self.spinBox_1.setRange(0, 100)
self.spinBox_1.setValue(50)
self.counter_1 = 0
self.spinBox_2.setRange(0, 100)
self.spinBox_2.setValue(50)
self.counter_2 = 0
self.load_settings()
def clickme_ON(self):
self.CLICK_ME_1.clicked.connect(lambda: self.plus_Clicked())
self.CLICK_ME_2.clicked.connect(lambda: self.minus_Clicked())
def plus_Clicked(self):
if self.index == 1:
self.counter_1 += 1
self.label_1.setText(str(self.counter_1))
print(f'плюс 1 слева:', self.counter_1)
elif self.index == 2:
self.counter_2 += 1
self.label_2.setText(str(self.counter_2))
print(f'плюс 1 справа:', self.counter_2)
def minus_Clicked(self):
if self.index == 1:
self.counter_1 -= 1
self.label_1.setText(str(self.counter_1))
print(f'минус 1 слева:', self.counter_1)
elif self.index == 2:
self.counter_2 -= 1
self.label_2.setText(str(self.counter_2))
print(f'минус 1 справа:', self.counter_2)
def vetka_1(self):
self.index = 1
self.clickme_ON()
print('vetka_1')
def vetka_2(self):
self.index = 2
self.clickme_ON()
print('vetka_2')
def save_settings(self):
settings = QSettings(CONFIG_FILE_NAME, QSettings.IniFormat)
settings.setValue('SpinBoxValue', self.spinBox_1.value())
settings.setValue('Counter', self.counter_1)
settings.setValue('SpinBoxValue_2', self.spinBox_2.value())
settings.setValue('Counter_2', self.counter_2)
def load_settings(self):
settings = QSettings(CONFIG_FILE_NAME, QSettings.IniFormat)
self.spinBox_1.setValue(
int(settings.value('SpinBoxValue', self.spinBox_1.value())))
self.counter_1 = int(settings.value('Counter', self.counter_1))
self.spinBox_2.setValue(
int(settings.value('SpinBoxValue_2', self.spinBox_2.value())))
self.counter_2 = int(settings.value('Counter_2', self.counter_2))
def exit_action(self):
self.save_settings()
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = New()
window.show()
sys.exit(app.exec_())
clickB_save.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>Deqart</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="CLICK_ME_1">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>250</x>
<y>80</y>
<width>125</width>
<height>91</height>
</rect>
</property>
<property name="font">
<font>
<family>MS Shell Dlg 2</family>
<pointsize>12</pointsize>
<weight>75</weight>
<italic>false</italic>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>+</string>
</property>
</widget>
<widget class="QLabel" name="label_1">
<property name="geometry">
<rect>
<x>180</x>
<y>20</y>
<width>121</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>14</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);</string>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QSpinBox" name="spinBox_1">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>420</y>
<width>121</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>14</pointsize>
</font>
</property>
</widget>
<widget class="QPushButton" name="EXIT">
<property name="geometry">
<rect>
<x>530</x>
<y>20</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>Выход</string>
</property>
</widget>
<widget class="QSpinBox" name="spinBox_2">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>510</x>
<y>420</y>
<width>121</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>14</pointsize>
</font>
</property>
</widget>
<widget class="QPushButton" name="CLICK_ME_2">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>250</x>
<y>180</y>
<width>125</width>
<height>91</height>
</rect>
</property>
<property name="font">
<font>
<family>MS Shell Dlg 2</family>
<pointsize>12</pointsize>
<weight>75</weight>
<italic>false</italic>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>-</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>320</x>
<y>20</y>
<width>121</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>14</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);</string>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QGroupBox" name="groupBox">
<property name="geometry">
<rect>
<x>140</x>
<y>290</y>
<width>351</width>
<height>80</height>
</rect>
</property>
<property name="title">
<string>GroupBox</string>
</property>
<widget class="QRadioButton" name="RB_1">
<property name="geometry">
<rect>
<x>20</x>
<y>30</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>RB 1</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
<widget class="QRadioButton" name="RB_2">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>130</x>
<y>30</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>RB 2</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
<widget class="QRadioButton" name="RB_3">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>250</x>
<y>30</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>NONE</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</widget>
</widget>
<action name="action">
<property name="text">
<string>Выход</string>
</property>
</action>
<action name="action_2">
<property name="text">
<string>Настройки</string>
</property>
</action>
<action name="action_4">
<property name="text">
<string>Диалоговое окно</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Я упорядочил ваш код и прокомментировал.
import sys
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUi
from PyQt5.QtCore import QSettings
CONFIG_FILE_NAME = 'config.ini'
class New(QMainWindow):
def __init__(self):
super(New, self).__init__()
loadUi("clickB_save.ui", self)
self.setWindowTitle('New')
self.EXIT.clicked.connect(self.exit_action)
# ? self.RB_1.clicked.connect(self.vetka_1)
# ? self.RB_2.clicked.connect(self.vetka_2)
# ? self.index = 0
self.spinBox_1.setRange(0, 100)
self.spinBox_1.setValue(50)
self.counter_1 = 0
self.spinBox_2.setRange(0, 100)
self.spinBox_2.setValue(50)
self.counter_2 = 0
self.load_settings()
self.label_1.setNum(self.counter_1) # +++
self.label_2.setNum(self.counter_2) # +++
# ? def clickme_ON(self):
# Неправильно вставлять в метод, который вызывается многократно
self.CLICK_ME_1.clicked.connect(self.plus_Clicked)
self.CLICK_ME_2.clicked.connect(self.minus_Clicked)
def plus_Clicked(self):
# if self.index == 1:
if self.RB_1.isChecked(): # +++
self.counter_1 += 1
self.label_1.setText(str(self.counter_1))
print(f'плюс 1 слева:', self.counter_1)
elif self.RB_2.isChecked(): #self.index == 2:
self.counter_2 += 1
self.label_2.setText(str(self.counter_2))
print(f'плюс 1 справа:', self.counter_2)
def minus_Clicked(self):
if self.RB_1.isChecked():
self.counter_1 -= 1
self.label_1.setText(str(self.counter_1))
print(f'минус 1 слева:', self.counter_1)
elif self.RB_2.isChecked():
self.counter_2 -= 1
self.label_2.setText(str(self.counter_2))
print(f'минус 1 справа:', self.counter_2)
'''
def vetka_1(self):
self.index = 1
# ? self.clickme_ON()
print('vetka_1')
def vetka_2(self):
self.index = 2
# ? self.clickme_ON()
print('vetka_2')
'''
def save_settings(self):
settings = QSettings(CONFIG_FILE_NAME, QSettings.IniFormat)
settings.setValue('SpinBoxValue', self.spinBox_1.value())
settings.setValue('Counter', self.counter_1)
settings.setValue('SpinBoxValue_2', self.spinBox_2.value())
settings.setValue('Counter_2', self.counter_2)
def load_settings(self):
settings = QSettings(CONFIG_FILE_NAME, QSettings.IniFormat)
self.spinBox_1.setValue(
int(settings.value('SpinBoxValue', self.spinBox_1.value())))
self.counter_1 = int(settings.value('Counter', self.counter_1))
self.spinBox_2.setValue(
int(settings.value('SpinBoxValue_2', self.spinBox_2.value())))
self.counter_2 = int(settings.value('Counter_2', self.counter_2))
def exit_action(self, e):
self.save_settings()
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = New()
window.show()
sys.exit(app.exec_())
