Home > database >  I want it to save. it is supposed to be disabled and shouldn't change but it changes
I want it to save. it is supposed to be disabled and shouldn't change but it changes

Time:10-08

I clicked the first one

then i clicked the second one but they are not staying the same second is correct but the first is empty

the widgets were changing when I clicked the other button the first button was resetting to normal

from ipywidgets import *
from IPython.display import *

the Output function was not doing its job

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

I was making tic tac toe but I came across a problem

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

tic tac toe is not done yet

 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)

It shouldn't change but it does

 out

There was no error code the widgets just were changing it was not working the b1 and b2 were changing

CodePudding user response:

using global variable, I could resolve this problem

  • Related