基于此代码,我们可以radioButton_2
添加另一个daL_2
句柄,并通过稍微更改代码,可以根据所选的句柄对左右句柄执行算术运算radioButton
。
但是如果项目中有很多这样的句柄呢?原来代码很大,窗口里有一堆。
是否可以用一支笔代替两支笔,并根据激活的一支笔获得结果radioButton
?或者这样做是错误的?
主文件
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QSettings
from PyQt5.uic import loadUi
CONFIG_FILE_NAME = 'config.ini'
class New(QMainWindow):
def __init__(self):
super(New, self).__init__()
loadUi("RButton_Dial_save.ui", self)
self.Exit.clicked.connect(self.close)
self.dial.setMinimum(0)
self.dial.setMaximum(10)
self.dial.setValue(5)
self.spinbox.setRange(5, 100)
self.spinbox.setValue(5)
self.load_settings()
self.dial.setMinimum(self.spinbox.value() - 5)
self.dial.setMaximum(self.spinbox.value() + 5)
self.dial.setValue(self.spinbox.value())
self.dial.valueChanged.connect(self.dial_changed)
self.spinbox.valueChanged.connect(self.spinbox_changed)
self.statusbar = self.statusBar()
self.statusbar.showMessage('Hello World', msecs=2000)
self.dial.setValue(self._dial)
self.radioButton.toggled.connect(self.rb_checked)
self.spinbox.setEnabled(False)
def save_settings(self):
settings = QSettings(CONFIG_FILE_NAME, QSettings.IniFormat)
settings.setValue('DialValue', self.dial.value())
settings.setValue('SpinBoxValue', self.spinbox.value())
def load_settings(self):
settings = QSettings(CONFIG_FILE_NAME, QSettings.IniFormat)
self.dial.setValue(
int(settings.value('DialValue', self.spinbox.value())))
self.spinbox.setValue(
int(settings.value('SpinBoxValue', self.spinbox.value())))
self._dial = int(settings.value('DialValue', self.spinbox.value()))
def closeEvent(self, event):
self.save_settings()
def spinbox_changed(self, value):
self.dial.setMinimum(value - 5)
self.dial.setMaximum(value + 5)
self.dial.setValue(value)
def dial_changed(self, value):
self.statusbar.showMessage(f'dial.value = {value}')
def rb_checked(self, checked):
if checked:
self.dial.setEnabled(True)
self.spinbox.setEnabled(True)
else:
self.dial.setEnabled(False)
self.spinbox.setEnabled(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setFont(QFont("Times", 12, QFont.Bold))
window = New()
window.show()
sys.exit(app.exec_())
RButton_Dial_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>800</width>
<height>600</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>800</width>
<height>600</height>
</size>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<widget class="QWidget" name="centralwidget">
<property name="minimumSize">
<size>
<width>800</width>
<height>600</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>800</width>
<height>600</height>
</size>
</property>
<widget class="QGroupBox" name="groupBox">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>761</width>
<height>211</height>
</rect>
</property>
<property name="title">
<string>GroupBox</string>
</property>
<widget class="QDial" name="dial">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>200</x>
<y>130</y>
<width>100</width>
<height>55</height>
</rect>
</property>
<property name="wrapping">
<bool>false</bool>
</property>
<property name="notchesVisible">
<bool>true</bool>
</property>
</widget>
<widget class="QPushButton" name="Exit">
<property name="geometry">
<rect>
<x>650</x>
<y>30</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>Выход</string>
</property>
</widget>
<widget class="QSpinBox" name="spinbox">
<property name="geometry">
<rect>
<x>300</x>
<y>20</y>
<width>111</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QRadioButton" name="radioButton">
<property name="geometry">
<rect>
<x>230</x>
<y>90</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>RButton 1</string>
</property>
</widget>
<widget class="QRadioButton" name="radioButton_2">
<property name="geometry">
<rect>
<x>420</x>
<y>90</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>RButton 2</string>
</property>
</widget>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
一切皆有可能,您需要了解的主要内容是如何与这么多单选按钮、一个旋钮进行交互,同时执行算术运算。
作为一种选择: