Search code examples
python-3.xmatplotlibkivykivy-languagematplotlib-widget

Can I use buttons from one screen to make changes to other screen?


I have a skeleton of an app I want to make. I'm trying to understand how a button from the "values screen" can interfere with the plot in the "direction screen".

Is it even possible ? I couldn't figure out the syntax of such an "update" function nor where to put it (though I think it must be under the MyFigure class).

main.py is

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg
import matplotlib.pyplot as plt


class AppScreen(BoxLayout):
    pass

class MagneticFieldApp(App):
    def build(self):
        return AppScreen()

class MyFigure(FigureCanvasKivyAgg):
    def __init__(self, **kwargs):
        plt.plot([1, 23, 2, 25])
        plt.ylabel('some numbers')
        super().__init__(plt.gcf(), **kwargs)

    #def plotx2(self):

    #def plotx3(self):

if __name__ == "__main__":
    MagneticFieldApp().run()

magneticfield.kv is

<ValuesScreen@Screen>:

<DirectionScreen@Screen>:

<IntensityScreen@Screen>:
    Label:
        text: 'intensity'
        font_size:50

<AppScreen>:
    spacing: 5
    orientation: 'vertical'
    manager: manager

    ScreenManager:
        size_hint: 1 , .9
        pos_hint: {'top': 1}
        id: manager

        ValuesScreen:
            name: 'valuesscreen'
            GridLayout:
                cols: 1
                Button:
                    text: 'x**2'
                    #on_press:

                Button:
                    text: 'x**3'
                    #on_press:

        DirectionScreen:
            name: 'directionscreen'
            MyFigure:

        IntensityScreen:
            name: 'intensityscreen'

    GridLayout:
        size_hint: 1 , .1
        pos_hint: {'y': 0}
        rows:1

        Button:
            text: 'values'
            on_press:
                root.manager.current = 'valuesscreen'

        Button:
            text: 'direction'
            on_press:
                root.manager.current = 'directionscreen'

        Button:
            text: 'intensity'
            on_press:
                root.manager.current = 'intensityscreen'

Solution

  • You can do using app.root.get_screen('directionscreen').plotx2() but I can't be more precise because I don't understand what you want to do :)