简体   繁体   中英

How can I get the position of button in kivy?

from kivy.app import App
from kivy.lang.builder import Builder
from kivy.core.window import Window 
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button



KV = """
FloatLayout:
    BoxLayout:
        id: chess_board
        orientation: "vertical"
"""

class MyApp(App):
    def build(self):
        Window.size = [800,800]
        return Builder.load_string(KV)

    def on_start(self):
        board = self.root.ids.chess_board
        for row in range(8):
            board_row = BoxLayout(orientation="horizontal")
            for column in range(8):
                board_row.add_widget(Button(background_normal=self.place_pieces(row,column),
                    background_color=self.get_color(row,column)))
            board.add_widget(board_row)


    def get_color(self, row, column):
        is_light_square = (row+column)%2 != 0
        if is_light_square:
            return [1,1,1,1]
        else:
            return [0,0,0,1]

    def place_pieces(self,row,column):
    
        if (row+column)%2 !=0 and row <3:
            return "red.png"
        if (row+column)%2 !=0 and row >4:
            return "blue.png"
        else:
            return ""



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

So I want to create Checkers Board Strategy Game but I don't know how to get the position of the button. I want to know where the selected piece is to make it move or you may have another method to move the piece.

Create a separate button class and in that give it a row and col attribute. As you add the button to the layout, allocate it's row and col attribute. Also, it might be helpful to add the button to a dict with the key row,col. This way you can access the instance of each button using, for example, location["2,3"]....

from kivy.app import App
from kivy.lang.builder import Builder
from kivy.core.window import Window 
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button



KV = """
FloatLayout:
    BoxLayout:
        id: chess_board
        orientation: "vertical"
    

<Square>:
    on_press: print(self.Row, self.Col)
"""
class Square(Button):
    Col = 0
    Row = 0


class MyApp(App):
    location = {}
    def build(self):
        Window.size = [800,800]
        return Builder.load_string(KV)

    def on_start(self):
        board = self.root.ids.chess_board
        for row in range(8):
            board_row = BoxLayout(orientation="horizontal")
            for column in range(8):
                btn = Square(background_normal=self.place_pieces(row,column),background_color=self.get_color(row,column))
                btn.Row = row
                btn.Col = column
                self.location[f'{row},{column}'] = btn
                board_row.add_widget(btn)
            board.add_widget(board_row)


    def get_color(self, row, column):
        is_light_square = (row+column)%2 != 0
        if is_light_square:
            return [1,1,1,1]
        else:
            return [0,0,0,1]

    def place_pieces(self,row,column):

        if (row+column)%2 !=0 and row <3:
            return "red.png"
        if (row+column)%2 !=0 and row >4:
            return "blue.png"
        else:
            return ""



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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM