Vahe Ohanyan
Vahe Ohanyan

Reputation: 13

Restart button in Kivy Game

I am trying to make restart button for tic-tac-toe game but i cant change kivy gridlayout in boxlayout. How to make this RESTART button?

I have tried with instance.text, but I could change only the text of the Restart Button

from kivy.app import App 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.button import Button 
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget 

symbols = ["x", "o"]; switch = 0


class TicApp(App):

    def choose(self, instance):
        global switch
        if switch % 2 == 0:
            instance.text = symbols[0]
            switch += 1
        else:
            instance.text = symbols[1]
            switch += 1    

    def restart(self, instance):
        """Pleasee help me to make restart button"""


    def build(self):
        global bl
        global gl
        gl = GridLayout(rows=3, cols=3)
        bl = BoxLayout(orientation = "vertical", spacing=5)
    
        #grid 3x3 of tic-tac-toe
        for i in range(9):
            gl.add_widget(Button(text="", font_size=60, on_press=self.choose))

        #making vertical boxlayout to add RESTART button
        bl.add_widget(gl)
    
        bl.add_widget(Button(text="Restart",font_size=35, size_hint=(1, .2), on_press=self.restart))
        return bl

    if __name__ == '__main__':
    TicApp().run() 

Upvotes: 1

Views: 315

Answers (1)

John Anderson
John Anderson

Reputation: 38857

Do you just want to reset the text of the Buttons? If so, try:

def restart(self, instance):
    global gl
    for w in gl.children:
        w.text = ''

Upvotes: 1

Related Questions