这个最小的例子展示了如何通过切换单选按钮来激活/停用旋转框。
随着时间的推移,代码会增长并且需要分解。
帮助将方法移动spinBoxSetEnabled_1()到spinBoxSetEnabled_2()单独的文件中。
主程序
import sys
from PyQt6.QtWidgets import QMainWindow, QApplication
from PyQt6.uic import loadUi
class New(QMainWindow):
def __init__(self):
super(New, self).__init__()
loadUi("Test.ui", self)
self.RB_1.setChecked(True)
self.RB_1.clicked.connect(self.spinBoxSetEnabled_1)
self.SB_1.setEnabled(True)
self.RB_2.setChecked(False)
self.RB_2.clicked.connect(self.spinBoxSetEnabled_2)
def spinBoxSetEnabled_1(self):
self.SB_1.setEnabled(True)
self.SB_2.setEnabled(False)
def spinBoxSetEnabled_2(self):
self.SB_1.setEnabled(False)
self.SB_2.setEnabled(True)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = New()
window.show()
sys.exit(app.exec())
测试.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>732</width>
<height>539</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QGroupBox" name="groupBox">
<property name="geometry">
<rect>
<x>10</x>
<y>0</y>
<width>701</width>
<height>491</height>
</rect>
</property>
<property name="title">
<string>GroupBox</string>
</property>
<widget class="QSpinBox" name="SB_1">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>40</x>
<y>40</y>
<width>131</width>
<height>61</height>
</rect>
</property>
</widget>
<widget class="QSpinBox" name="SB_2">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>200</x>
<y>40</y>
<width>131</width>
<height>61</height>
</rect>
</property>
</widget>
<widget class="QRadioButton" name="RB_1">
<property name="geometry">
<rect>
<x>50</x>
<y>160</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>1</string>
</property>
</widget>
<widget class="QRadioButton" name="RB_2">
<property name="geometry">
<rect>
<x>210</x>
<y>160</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>2</string>
</property>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>732</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
作为一个选项:
主程序
q1514951_spinBox.py
q1514951_test.ui