python - Kivy 游戏中的重启按钮
问题描述
我正在尝试为井字游戏制作重启按钮,但我无法更改 kivy gridlayout
。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()
解决方案
你只是想重置文本Buttons
?如果是这样,请尝试:
def restart(self, instance):
global gl
for w in gl.children:
w.text = ''
推荐阅读
- r - 带有 r-highcharter 的议会图
- sql-server - 有条件地在查询中包含一列?
- java - Bitronix 事务不适用于 JMS 队列
- python - 具有两个数据框列作为参数的 Lambda
- javascript - 填充网格后剑道网格的居中
- c++ - Doxygen 创建指向特定代码行的链接
- python - 用Python中的字符串替换包含子字符串的字符串
- google-bigquery - 在 where 子句 Bigquery 中使用聚合函数更新表
- jenkins - Kubernetes Pod 模板 Pre Pod,Service,Deployment yaml 文件
- javascript - Computer Guess A Number JavaScript