2
複数のボタンに同じコールバックを使用しようとしていますが、ここで行われたようにコールバックを使用するためにブール値を返すものが必要でしたが、ボタンごとに異なる機能をバインドせずにこれを行う方法はありますか?kivyの複数のボタンに同じコールバック
import kivy
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
kivy.require('1.9.2')
class VelhaGame(GridLayout, Screen):
def __init__(self, **kwargs):
super(VelhaGame, self).__init__(**kwargs)
self.cols = 3
self.rows = 3
self.font_size = 100
self.button1 = Button(text='', font_size=self.font_size)
self.add_widget(self.button1)
self.button1.bind(on_press=self.player_turn)
self.button2 = Button(text='', font_size=self.font_size)
self.add_widget(self.button2)
self.button2.bind(on_release=self.player_turn)
self.button3 = Button(text='', font_size=self.font_size)
self.add_widget(self.button3)
self.button3.bind(on_release=self.player_turn)
self.button4 = Button(text='', font_size=self.font_size)
self.add_widget(self.button4)
self.button4.bind(on_release=self.player_turn)
self.button5 = Button(text='', font_size=self.font_size)
self.add_widget(self.button5)
self.button5.bind(on_release=self.player_turn)
self.button6 = Button(text='', font_size=self.font_size)
self.add_widget(self.button6)
self.button6.bind(on_release=self.player_turn)
self.button7 = Button(text='', font_size=self.font_size)
self.add_widget(self.button7)
self.button7.bind(on_release=self.player_turn)
self.button8 = Button(text='', font_size=self.font_size)
self.add_widget(self.button8)
self.button8.bind(on_release=self.player_turn)
self.button9 = Button(text='', font_size=self.font_size)
self.add_widget(self.button9)
self.button9.bind(on_release=self.player_turn)
self.player1 = True
def player_turn(self, *args):
if self.player1:
self.player1 = False
if self.button1:
self.button1.text = 'X'
elif self.button2:
self.button2.text = 'X'
elif self.button3:
self.button3.text = 'X'
elif self.button4:
self.button4.text = 'X'
elif self.button5:
self.button5.text = 'X'
elif self.button6:
self.button6.text = 'X'
elif self.button7:
self.button7.text = 'X'
elif self.button8:
self.button8.text = 'X'
elif self.button9:
self.button9.text = 'X'
elif not self.player1:
self.player1 = True
if self.button1:
self.button1.text = 'O'
elif self.button2:
self.button2.text = 'O'
elif self.button3:
self.button3.text = 'O'
elif self.button4:
self.button4.text = 'O'
elif self.button5:
self.button5.text = 'O'
elif self.button6:
self.button6.text = 'O'
elif self.button7:
self.button7.text = 'O'
elif self.button8:
self.button8.text = 'O'
elif self.button9:
self.button9.text = 'O'
class VelhaGameApp(App):
def build(self):
game = Screen()
table_screen = VelhaGame()
game.add_widget(table_screen)
return game
if __name__ == '__main__':
VelhaGameApp().run()
私は本当に役に立ちましたが、今までこれをやり遂げる方法はわかりませんでした。私はこのようなforループを使うのに慣れていなかったので、あなたの助言に感謝します。 –