Mohd Arslaan
Mohd Arslaan

Reputation: 39

How to store the button id on which we clicked for another class in kivy

I want to use the id of the button I clicked on screen1 to open some content related to it on screen2. Suppose i have created 5 buttons with different names on them. If i click button 1 it should open-content related to button 1 and vice versa.

Here is the code for my main .py file:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen,ScreenManager


class MainWindow(Screen):
    def category(self):
        carsquiz=self.ids  #it should contain that button id which was clicked
        self.manager.current='quizgo'

class QuizWindow(MainWindow,Screen):
    def something(self):
        # i want to access that button id in this screen
        pass

class WindowManager(ScreenManager):
    pass

class StandAlone(App):
    def build(self):
        return Builder.load_file('StandAloneQuiz.kv')

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

Here is the code for my .kv file:

#:kivy 2.1.0
WindowManager:
    MainWindow:
    QuizWindow:

<MainWindow>:
    name:"index"
    BoxLayout:
        orientation:'vertical'
        Label:
            text:'Choose your category...'
            font_size:25
        Button:
            text:'category 1'
            id:category1
            on_press:
                root.category()
                root.manager.transition.direction='left'

        Button:
            text:'category 2'
            id:category2
            on_release:
                root.category()
                root.manager.transition.direction='left'
        Button:
            text:'category 3'
            id:category3
            on_release:
                root.category()
                root.manager.transition.direction='left'
        Button:
            text:'category 4'
            id:category4
            on_release:
                root.category()
                root.manager.transition.direction='left'
        Button:
            text:'category 5'
            id:category5
            on_release:
                root.category()
                root.manager.transition.direction='left'

<QuizWindow>:
    name:"quizgo"
    Button:
        text:'go Back'
        on_release:
            root.something()
            app.root.current="index"
            root.manager.transition.direction='right'

Upvotes: 0

Views: 420

Answers (1)

John Anderson
John Anderson

Reputation: 39107

If you add self to the self.category() in the kv:

    Button:
        text:'category 5'
        id: category5
        on_release:
            root.category(self)  # pass the Button instance to the method
            root.manager.transition.direction='left'

Of course, this change must be made for all those Buttons.

The modify the python code to use it:

class MainWindow(Screen):
    def category(self, butt_instance):
        the_id = None
        for butt_id, button in self.ids.items():
            if button == butt_instance:
                the_id = butt_id
                break
        quizgo = self.manager.get_screen('quizgo')
        quizgo.the_butt_id = the_id
        self.manager.current='quizgo'

class QuizWindow(Screen):
    the_butt_id: ''

    def something(self):
        print('in something the id is', self.the_butt_id)

Upvotes: 1

Related Questions