Как в PyQt6 получить доступ к функции, которая находится в другом модуле
Данный минимальный пример показывает, как с помощью переключения радио кнопок делать активацию/деактивацию спинбоксов.
Со временем код разрастается и требуется декомпозиция.
Помогите перенести методы spinBoxSetEnabled_1()
и spinBoxSetEnabled_2()
в отдельный файл.
main.py
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())
Test.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>
Источник: Stack Overflow на русском