同样回调kivy中的多个按钮

时间:2016-07-04 18:06:23

标签: python kivy python-3.5

我试图对多个按钮使用相同的回调,我想要一些返回布尔值的东西来使用回调就像在这里完成的那样,但我找不到像这样的东西。有没有办法在不为每个按钮绑定不同功能的情况下执行此操作?

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

1 个答案:

答案 0 :(得分:4)

如果您发现自己重复了代码,那么通常意味着您需要循环。您绑定的方法也会作为调用它的按钮的第一个参数实例接收。

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


class VelhaGame(GridLayout, Screen):
    def __init__(self, **kwargs):
        super(VelhaGame, self).__init__(**kwargs)
        self.cols = 3
        self.rows = 3
        self.font_size = 100

        self.buttons = []

        for i in range(1, 10):
            button = Button(text='', font_size=self.font_size)
            button.bind(on_press=self.player_turn)
            self.add_widget(button)
            self.buttons.append(button)

        self.player1 = True

    def player_turn(self, instance, *args):
        if self.player1:
            self.player1 = False
            instance.text = 'X'
        elif not self.player1:
            self.player1 = True
            instance.text = '0'

class VelhaGameApp(App):
    def build(self):
        game = Screen()
        table_screen = VelhaGame()
        game.add_widget(table_screen)
        return game

if __name__ == '__main__':
    VelhaGameApp().run()

你也可以创建一个Button类的子类,但在这里会有点过分。