我一直在研究這個問題很長時間,但我無法找到解決方案。我正在制作井字游戲,并且創建了 9 個按鈕的網格布局。當按下按鈕時,我希望它從占位符影像更改為 x 或 o 的影像。我已將按鈕影像源分配為 python 類屬性,我可以呼叫一個函式,以便在按下按鈕時更新屬性。我知道它正在作業,因為每次按下按鈕時我都會從函式中獲得輸出。問題是影像沒有更新。我到處尋找解決方案,但找不到。
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.gridlayout import GridLayout
class Game(GridLayout):
empty = str('C:/Users/codin/Desktop/tictactoe/walpaper.png')
states = ['button1', 'button2', 'button3', 'button4', 'button5',
'button6', 'button7', 'button8', 'button9']
button1 = empty
button2 = empty
button3 = empty
button4 = empty
button5 = empty
button6 = empty
button7 = empty
button8 = empty
button9 = empty
def change_state(self, state):
O = str('C:/Users/codin/Desktop/tictactoe/just_o.png')
X = str('C:/Users/codin/Desktop/tictactoe/just_x.png')
empty = str('C:/Users/codin/Desktop/tictactoe/walpaper.png')
if getattr(self, state) == empty:
setattr(self, state, X)
elif getattr(self, state) == X:
setattr(self, state, O)
elif getattr(self, state) == O:
setattr(self, state, X)
else:
setattr(self, state, empty)
print(self.__dict__[state])
pass
# Create an app class to do the handling
class TicTacToe(App):
def build(self):
return Game()
if __name__ == "__main__":
TicTacToe().run()
.kv 檔案:
<Game>
cols: 3
rows: 4
Button:
on_press: root.change_state(root.states[0])
Image:
id: button1
source: root.button1
center_x: self.parent.center_x
center_y: self.parent.center_y
Button:
on_press: root.change_state(root.states[1])
Image:
id: button2
source: root.button2
center_x: self.parent.center_x
center_y: self.parent.center_y
more buttons below
我完全知道我可以為每個按鈕撰寫一個函式,并通過 python 檔案中的 self.ids.image_id.source 更改影像源,但我試圖保持簡短。任何幫助是極大的贊賞。
uj5u.com熱心網友回復:
我能夠結合使用 ApuCoder 的答案和來自 kivy.lang 模塊大綱的資訊來解決這個問題:https ://kivy.org/doc/stable/api-kivy.lang.html
我的 python 代碼很短,分配給 str() 檔案路徑的簡單變數用于游戲類中的影像,以及要編譯的應用程式類:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.gridlayout import GridLayout
# class for image sources and layout
class Game(GridLayout):
O = str('just_o.png')
X = str('just_x.png')
empty = str('wallpaper.png')
# App class to do the handling
class TicTacToe(App):
def build(self):
return Game()
if __name__ == "__main__":
TicTacToe().run()
在 kv 中,您可以撰寫條件陳述句。要求是每個陳述句都在自己的行上。if/elif/else 以及 for 和 while 回圈都是允許的。
<Game>
cols: 3
rows: 3
Button:
on_press:
if button1.source == root.empty: button1.source = root.X
elif button1.source == root.X: button1.source = root.O
else: button1.source = root.X
Image:
id: button1
source: root.empty
center_x: self.parent.center_x
center_y: self.parent.center_y
這段代碼基本上是說,“在開始時分配的影像源,如果影像是這個,則將其更改為那個,反之亦然”
這可能是完成此任務所需的最少代碼量。
uj5u.com熱心網友回復:
像這樣的東西,
在你的Game課堂上,
img_O = StringProperty('C:/Users/codin/Desktop/tictactoe/just_o.png')
img_X = StringProperty('C:/Users/codin/Desktop/tictactoe/just_x.png')
img_empty = StringProperty('C:/Users/codin/Desktop/tictactoe/walpaper.png')
那么在 kv 中,
Button:
on_press: button1.source = root.img_X if button1.source == root.img_empty else ...
Image:
id: button1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/405429.html
標籤:
