user5760215
user5760215

Reputation:

Tkinter Checkbox issues

Edit: Changed to use TopLevel and the radio buttons

I am working on making a python chess game for the fun of it and I am coming at a slight problem with creating a checkbox.

In the standard game of chess, if the pawn reaches the other side of the chessboard, the player will be able to choose what chess piece they want.

Well, I wanted to give the user a checkbox to check the item they wanted and then the box would disappear(destroyed). Here is my code:

from Tkinter import *

def AddChessPiece():
    CheckBox = TopLevel()

    print("Im in The checkbox function")
    CheckVar1 = IntVar()
    CheckVar2 = IntVar()
    CheckVar3 = IntVar()
    CheckVar4 = IntVar()

    C1 = Radiobutton(CheckBox, text="Rook",variable = CheckVar1, command = lambda: PieceName("Rook"))
    C2 = Radiobutton(CheckBox, text="Knight",variable = CheckVar2,command= lambda: PieceName("Knight"))
    C3 = Radiobutton(CheckBox, text="Bishop",variable = CheckVar3,command= lambda: PieceName("Bishop"))
    C4 = Radiobutton(CheckBox, text="Queen",variable = CheckVar4, command= lambda: PieceName("Queen"))

    C1.pack()
    C2.pack()
    C3.pack()
    C4.pack()


    CheckBox.mainloop()
    print("Im leaving the checkbox function")

What it is doing is that it creates the window, then when checked, it sends the item to the lambda function. The problem is that

  1. I don't know where or how to destroy it immediately after the item has been clicked and

  2. It seems like the program doesn't continue when it goes into the function PieceName. It goes through the complete function but it never prints "I'm Leaving the checkbox function". I'm thinking this might be an error since I destroyed the function.Any help would be wonderful!

Here is the PieceName Method if you're curious. I don't think it will help anything though. What is does is it Adds the new coordinates to new chess piece first(Depending if its player 1 (White Chess Piece) or player 2 (Black chess piece) turn to go) and then removes the pawns coordinates.

def PieceName(name):
    global Player1, CurrentChessPiece, IndexVal,White_Pieces,Black_Pieces

    if(Player1 == True):
        FullName = "White_" + name
        NewPieceIndex = White_Pieces.index(FullName)
        White_Pieces[NewPieceIndex].coordinates.append(CurrentChessPiece.coordinates[IndexVal])
    else:
        FullName = "Black_" + name
        NewPieceIndex = Black_Pieces.index(FullName)
        Black_Pieces[NewPieceIndex].coordinates.append(CurrentChessPiece.coordinates[IndexVal])

    del CurrentChessPiece.coordinates[IndexVal]
    CheckBox.destroy()
    print("Im leaving the PieceName function")

Upvotes: 0

Views: 449

Answers (1)

user5760215
user5760215

Reputation:

Ok, so this is the new revision that is now working. What I did was remove all the lambda parameters and just saved all possible values to "CheckVar1". From there I created a messagebox and gave the user the options to choose from the options and click C5 ("Ok button") to quit.

When the ok button was clicked, it closed the widget but did not destroy it, so I was able to still grab "CheckVar1". From there, I called a getter function "CheckVar1.get()" to return the value as a string variable and pass it into the following method "PieceName"

Once the method "PieceName" was finished, "Checkbox" was then destroyed, removing it from the system.

def AddChessPiece():
    CheckBox = Toplevel()

    print("Im in The checkbox function")
    CheckVar1 = StringVar() 

    C = Message(CheckBox,text="What did you want to\n replace your pawn for?\n\n",width = 300)
    C1 = Radiobutton(CheckBox, text="Rook",variable = CheckVar1,value ="Rook")
    C2 = Radiobutton(CheckBox, text="Knight",variable = CheckVar1,value = "Knight")
    C3 = Radiobutton(CheckBox, text="Bishop",variable = CheckVar1,value = "Bishop")
    C4 = Radiobutton(CheckBox, text="Queen",variable = CheckVar1,value = "Queen")
    C5 = Button(CheckBox, text="Ok", command=CheckBox.quit)

    C.pack()
    C1.pack()
    C2.pack()
    C3.pack()
    C4.pack()
    C5.pack()

    CheckBox.mainloop()

    PieceName(str(CheckVar1.get()))
    print("Im leaving the checkbox function")
    CheckBox.destroy()

This is the second function. The only thing that was changed was that "CheckBox.destroy()" was not used at all in this function. CheckBox wasn't used at all in this function, so this code is now irrelevant to the discussion.

def PieceName(name):
    global Player1, CurrentChessPiece, IndexVal,White_Pieces,Black_Pieces
    if(Player1 == True):

    FullName = "White_" + str(name)

    for n in range(0, len(White_Pieces)):   
        if((FullName in White_Pieces[n].name)== True):
            White_Pieces[n].coordinates.append(CurrentChessPiece.coordinates[IndexVal])
    else:
        FullName = "Black_" + str(name)
        for n in range(0,len(Black_Pieces)):
            if((FullName in Black_Pieces[n].name) == True):
                Black_Pieces[n].coordinates.append(CurrentChessPiece.coordinates[IndexVal])

    del CurrentChessPiece.coordinates[IndexVal]
    print("Name is " + str(FullName))
    print("Im leaving the PieceName function")

Thanks for all the help!

Upvotes: 1

Related Questions