【问题标题】:Restart button in Kivy GameKivy 游戏中的重启按钮
【发布时间】:2020-06-28 10:24:40
【问题描述】:

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

【问题讨论】:

标签: python kivy kivy-language


【解决方案1】:

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

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

【讨论】:

  • 非常感谢,你真的帮了我!
猜你喜欢
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多