首页 > 解决方案 > Kivy 游戏中的重启按钮

问题描述

我正在尝试为井字游戏制作重启按钮,但我无法更改 kivy gridlayoutboxlayout如何制作这个 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() 

标签: pythonkivykivy-language

解决方案


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

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

推荐阅读