Kivy游戏中的重新启动按钮

时间:2020-06-27 19:01:34

标签: python kivy kivy-language

我正在尝试为井字游戏设置重启按钮,但是我无法在gridlayout中更改kivy boxlayout。 如何制作此RESTART按钮?

我尝试使用instance.text,但是我只能更改“重启按钮”的文本

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() 

1 个答案:

答案 0 :(得分:1)

您是否只想重置Buttons的文本?如果是这样,请尝试:

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