Python Kivy编程,如何通过按下另一个按钮来更改按钮的颜色或图像

Python Kivy编程,如何通过按下另一个按钮来更改按钮的颜色或图像,python,python-2.7,python-3.x,kivy,kivy-language,Python,Python 2.7,Python 3.x,Kivy,Kivy Language,所以我和我的朋友们一直在尝试在kivy上编写一个游戏,但是我们被困在了这一点上,我们不知道如何通过按下另一个按钮来改变一个按钮的颜色或图像。 游戏是在一个棋盘上,我们正在尝试做一个狐狸和猎犬游戏,所以我们的想法是当用户按下一只猎犬时,棋盘上的两个方块亮起,指示您可以移动的位置,然后按下其中一个方块,将de Hound的图像更改为按下的那个方块 这是代码,希望你能帮我,谢谢 from kivy.app import App from kivy.graphics import * from kivy

所以我和我的朋友们一直在尝试在kivy上编写一个游戏,但是我们被困在了这一点上,我们不知道如何通过按下另一个按钮来改变一个按钮的颜色或图像。 游戏是在一个棋盘上,我们正在尝试做一个狐狸和猎犬游戏,所以我们的想法是当用户按下一只猎犬时,棋盘上的两个方块亮起,指示您可以移动的位置,然后按下其中一个方块,将de Hound的图像更改为按下的那个方块

这是代码,希望你能帮我,谢谢

from kivy.app import App
from kivy.graphics import *
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.uix.popup import Popup
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.stacklayout import StackLayout
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.config import Config

Window.size = (800, 800)

intento = 0
nroboton= [0,0]

class MainWindow(GridLayout): # main class
    def popbtns(self):
        i = 3
        self.add_widget(Label(text=' '))
        self.add_widget(Btn(id=str(i+3), size=(100, 100), background_color=(0,0,0,1)))
        i = i - 1

class Btn(Button): # button class
    def on_release(self):
        print('self.pos= ' + str(self.id))
    def on_press(self):
        global intento
        if(intento == 1):
            nroboton[1] = self.id
            print('moviendose de ' + str(nroboton[0]) + 'a ' + str(nroboton[1]))
            intento = 0
        else:
            nroboton[0] = self.id
            intento = 1
        print('llamada ssh ' + str(self.id))

class But(Button):
    def new(self):
        self.background_color=(0,250,0,1)

class Zorro(Button): # button class
    def on_release(self):
        print('self.pos= ' + str(self.id))
    def on_press(self):
        global intento
        if(intento == 1):
            nroboton[1] = self.id
            print('moviendose de ' + str(nroboton[0]) + 'a ' + str(nroboton[1]))
            intento = 0
        else:
            nroboton[0] = self.id
            intento = 1
        print('llamada ssh ' + str(self.id)) 

class MainApp(App):

    def build(self):

        main = MainWindow(cols=3, rows=1)
        self.root = main  # don't use global!
        # make background
        with main.canvas:
            Rectangle(pos=main.pos, size=(10000,10000))

        # populate gridlayout with Buttons
        #main.add_widget(Debug(text='debug', background_color=(1, 0, 0, 1)))
        main.popbtns()
        # print position of buttons...
        Clock.schedule_once(self.delayed_function, 0.1)

    def delayed_function(self, dt):
        self.print_buttons_pos()

    def print_buttons_pos(self):
        for child in self.root.children:
            print(str(child) + ' pos is ' + str(child.id))

if __name__ == "__main__":
    MainApp().run()
这是猎犬和狐狸的图像。


好的,这就是我所能减少的,希望能有所帮助。

这里有一些代码,告诉你如何通过按下另一个按钮来改变一个按钮的颜色

我使用父窗口的ids属性,这当然是更好的方法之一。我在这里留下了一些你的代码,但是你会注意到我最终删除了很多,因为那不是MCVE的一部分

from kivy.app import App
from kivy.graphics import *
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.lang import Builder

Window.size = (800, 800)

intento = 0
nroboton= [0,0]

setup = '''
MyGame:
    cols: 3
    rows: 2
    # This could have just been "Button"
    # and it would work just fine, too.
    Btn:
        # This ID is only valid inside the .kv
        # stuff, so we have to pass something
        # else to the pressed/released handlers
        id: hello_btn
        text: 'Hello'
        size: 100,100
        # These lines are how to identify the
        # button doing the pressing
        on_press: root.was_pressed('hello')
        on_release: root.was_released('hello')
    Btn:
        id: world_btn
        text: 'World'
        size: 100,100
        on_press: root.was_pressed('world')
        on_release: root.was_released('world')
'''

# I'm not using any of this code, you may want it
# if you're trying to do something special with the
# buttons you're creating. Otherwise just change
# Btn to Button
class Btn(Button): # button class
    def on_release(self):
        print('self.pos= ' + str(self.id))
    def on_press(self):
        global intento
        self.background_color = [1,1,1,1]

        print('IDs: ', self.parent.ids)
        if(intento == 1):
            nroboton[1] = self.id
            print('moviendose de ' + str(nroboton[0]) + 'a ' + str(nroboton[1])
            intento = 0
        else:
            nroboton[0] = self.id
            intento = 1
        print('llamada ssh ' + str(self.id))


class MyGame(GridLayout):
    def was_pressed(self, name):
        if name == 'hello':
            btn = self.ids['world_btn']
        elif name == 'world':
            btn = self.ids['hello_btn']
        btn.background_color = [255, 0, 0, 1]

    def was_released(self, name):
        if name == 'hello':
            btn = self.ids['world_btn']
        elif name == 'world':
            btn = self.ids['hello_btn']
        btn.background_color = [1, 1, 1, 1]


class MainApp(App):
    def build(self):
        return Builder.load_string(setup)

if __name__ == "__main__":
    MainApp().run()

请把这个减少到一个数字。事实上,还有很多额外的信息。也就是说,你应该有一个kivy应用程序,上面有两个按钮。完成!或者至少我试着减少它。仍然可以非常感谢你,这真的很有帮助。