Home > Mobile >  Tkinter: how to use a separate Window to ask the user for input via buttons and return that value to
Tkinter: how to use a separate Window to ask the user for input via buttons and return that value to

Time:10-21

I currently have to write a chess game in Python using nothing but Tkinter in order to get my highschool diploma and I'm currently struggeling with the following: When a pawn reaches the backline, it can be promoted. I want to create a Pop-up window with Buttons the user can click in order to select what kind of piece his pawn should be promoted to. It currently looks like this:

def getPromotePawnPiece() -> str:
    optionsWindow = Toplevel()
    selected_piece = "None"
    pawnField = Button(optionsWindow, text="Pawn", anchor=W, command=lambda: 
                               returnPieceSelected("Pawn", optionsWindow, selected_piece))
    rookField = Button(optionsWindow, text="Rook", anchor=W, command=lambda: 
                               returnPieceSelected("Rook", optionsWindow, selected_piece))
    bishopField = Button(optionsWindow, text="Bishop", anchor=W, command=lambda: 
                               returnPieceSelected("Bishop", optionsWindow, selected_piece))
    knightField = Button(optionsWindow, text="Knight", anchor=W, command=lambda: 
                               returnPieceSelected("Knight", optionsWindow, selected_piece))
    queenField = Button(optionsWindow, text="Queen", anchor=W, command=lambda: 
                               returnPieceSelected("Queen", optionsWindow, selected_piece))

pawnField.grid(row=1, column=0, sticky=NSEW)
rookField.grid(row=1, column=1, sticky=NSEW)
bishopField.grid(row=1, column=3, sticky=NSEW)
knightField.grid(row=1, column=2, sticky=NSEW)
queenField.grid(row=1, column=4, sticky=NSEW)

def returnPieceSelected(selected_piece_via_button: str, window: Toplevel, selected_piece_var: 
str):
     window.destroy()
     selected_piece_var = selected_piece_via_button

The Window gets created successfully but I have no way to get the information of which button has been clicked to the original root Window In case it helps: the "main" Window is globally created, getPromotePiece() is called after a Pawn has reached the last row

CodePudding user response:

First use StringVar for selected_piece, so that it can be set inside returnPieceSelected().

Then use optionsWindow.wait_window() inside getPromotePawnPiece() to wait for selection:

def returnPieceSelected(selected_piece_via_button: str, window: Toplevel, selected_piece_var: StringVar):
    window.destroy()
    # save selection
    selected_piece_var.set(selected_piece_via_button)

def getPromotePawnPiece() -> str:

    optionsWindow = Toplevel()
    selected_piece = StringVar(value="None") # use StringVar instead of normal string
    pawnField = Button(optionsWindow, text="Pawn", anchor=W, command=lambda:
                               returnPieceSelected("Pawn", optionsWindow, selected_piece))
    rookField = Button(optionsWindow, text="Rook", anchor=W, command=lambda:
                               returnPieceSelected("Rook", optionsWindow, selected_piece))
    bishopField = Button(optionsWindow, text="Bishop", anchor=W, command=lambda:
                               returnPieceSelected("Bishop", optionsWindow, selected_piece))
    knightField = Button(optionsWindow, text="Knight", anchor=W, command=lambda:
                               returnPieceSelected("Knight", optionsWindow, selected_piece))
    queenField = Button(optionsWindow, text="Queen", anchor=W, command=lambda:
                               returnPieceSelected("Queen", optionsWindow, selected_piece))

    pawnField.grid(row=1, column=0, sticky=NSEW)
    rookField.grid(row=1, column=1, sticky=NSEW)
    bishopField.grid(row=1, column=3, sticky=NSEW)
    knightField.grid(row=1, column=2, sticky=NSEW)
    queenField.grid(row=1, column=4, sticky=NSEW)

    # wait for user selection
    optionsWindow.wait_window()

    # return user selection
    return selected_piece.get()
  • Related