我點了第一個
然后我點擊了第二個,但他們沒有保持相同第二個是正確的,但第一個是空的
當我單擊另一個按鈕時,小部件正在更改第一個按鈕正在重置為正常
from ipywidgets import *
from IPython.display import *
輸出功能沒有完成它的作業
out = Output()
b1 = Button()
b1.style.button_color = 'black'
b2 = Button()
b2.style.button_color = 'black'
b3 = Button()
b3.style.button_color = 'black'
b4 = Button()
b4.style.button_color = 'black'
b5 = Button()
b5.style.button_color = 'black'
b6 = Button()
b6.style.button_color = 'black'
b7 = Button()
b7.style.button_color = 'black'
b8 = Button()
b8.style.button_color = 'black'
b9 = Button()
b9.style.button_color = 'black'
board = HBox([VBox([b1,b2,b3]),VBox([b4,b5,b6]),VBox([b7,b8,b9])])
user1name = Text()
user2name = Text()
user1turn = widgets.Text(value="turn",description=user1name.value ':',disabled=False)
user2turn = widgets.Text(value="",description=user1name.value ':',disabled=False)
user1 = "turn"
user2 = ""
userturn = 1
我正在做井字游戲,但遇到了一個問題
def u1nc(self):
out.clear_output()
with out:
print("enter ? player's name:")
display(user1name)
def onsumbitu1(self):
out.clear_output()
user1name.disabled = True
with out:
print("enter ? player's name:")
display(user2name)
def onsubmitu2(self):
out.clear_output()
user2name.disabled = True
with out:
display(board)
def turnchooser(self):
global userturn
if userturn == 1:
user2 = "turn"
user1 = ""
userturn = 2
elif userturn == 2:
user1 = "turn"
user2 = ""
userturn = 1
else:
out.clear_output
print("game over")
def b1func(self):
if userturn == 1:
out.clear_output()
b1 = Button(description='?',disabled=True)
b1.style.button_color = "black"
board =HBox([VBox([b1,b2,b3]),VBox([b4,b5,b6]),VBox([b7,b8,b9])])
with out:
display(board)
out
elif userturn == 2:
b1 = Button(description='?',disabled=True)
b1.style.button_color = 'black'
board = HBox([VBox([b1,b2,b3]),VBox([b4,b5,b6]),VBox([b7,b8,b9])])
with out:
display(board)
out
def b2func(self):
if userturn == 1:
out.clear_output()
b2 = Button(description='?',disabled=True)
b2.style.button_color = "black"
board = HBox([VBox([b1,b2,b3]),VBox([b4,b5,b6]),VBox([b7,b8,b9])])
with out:
display(board)
out
elif userturn == 2:
out.clear_output()
b2 = Button(description='?',disabled=True)
b2.style.button_color = 'black'
board = HBox([VBox([b1,b2,b3]),VBox([b4,b5,b6]),VBox([b7,b8,b9])])
with out:
display(board)
out
井字游戲還沒有完成
user1name.on_submit(onsumbitu1)
user2name.on_submit(onsubmitu2)
out.on_displayed(u1nc)
b1.on_click(b1func)
b1.on_click(turnchooser)
b2.on_click(b2func)
b2.on_click(turnchooser)
它不應該改變,但它確實
out
沒有錯誤代碼只是小部件正在更改它不起作用 b1 和 b2 正在更改
uj5u.com熱心網友回復:
使用全域變數,我可以解決這個問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/511680.html
