Variables across classes to scale plot in PyQt GUI

后端 未结 1 2014
迷失自我
迷失自我 2021-01-21 11:12

I\'m making a GUI which is to have a couple user input boxes and a plot which will use factors in the input boxes to scale data. The GUI will need an apply button and an export

1条回答
  •  礼貌的吻别
    2021-01-21 11:56

    There are several options here:

    (a) Peform the work in the main class: Instead of connecting the button in the AppForm to a method in AppForm, do the same in the ApplicationWindow.

    self.form = AppForm()
    self.sc = MyStaticMplCanvas(....)
    self.form.apply_button.clicked.connect(self.applyButton)
    
    def applyButton(self):
        tx = self.form.aFactor.text()
        # do something with tx
        # e.g. call a method from MplCanvas
        self.sc.someMethod(tx)
    

    (b) Use signals and slots: Create a signla in AppForm. Once the button is clicked, applyButton may emit this signal with the relevant content. In ApplicationWindow connect that signal to a method which can use the supplied data in some way. You can also connect it directly to a method of MplCanvas.

    class AppForm(QWidget):
        mysignal = pyqtSignal(object)
        def __init__(self):
            ....
            self.apply_button.clicked.connect(self.applyButton)
    
        def applyButton(self):
            self.mysignalemit((self.aFactor.text(),self.mFactor.text(), self.cZone()) )
    
    class ApplicationWindow(QMainWindow):
        def __init__(self):
            ....
            self.form = AppForm()
            self.sc = MyStaticMplCanvas(....)
            self.form.mysignal.connect(self.useButtonResults)
            self.form.mysignal.connect(self.sc.someMethod)
    
        def useButtonResults(self, obj):
            a,b,c = obj
            # do something with a, b, c
    

    (c) Use a single class to do everything: Just put everything in a single class and do whatever you like.

    0 讨论(0)
提交回复
热议问题