I'm fairly new to tkinter and I need to make a button that adds a entry widget in the row above the same button, is it possible?
from tkinter import *
root = Tk()
playerFrame = Frame(root)
#Players' numbers and names / Numeros e nomes dos jogadores
pnum1 = Label(playerFrame, text="1").grid(column=0, row=0)
pnum2 = Label(playerFrame, text="2").grid(column=0, row=1)
pnum3 = Label(playerFrame, text="3").grid(column=0, row=2)
pnum4 = Label(playerFrame, text="4").grid(column=0, row=3)
pnum5 = Label(playerFrame, text="5").grid(column=0, row=4)
pnum6 = Label(playerFrame, text="6").grid(column=0, row=5)
pnum7 = Label(playerFrame, text="7").grid(column=0, row=6)
player1 = Entry(playerFrame).grid(column=1, row=0)
player2 = Entry(playerFrame).grid(column=1, row=1)
player3 = Entry(playerFrame).grid(column=1, row=2)
player4 = Entry(playerFrame).grid(column=1, row=3)
player5 = Entry(playerFrame).grid(column=1, row=4)
player6 = Entry(playerFrame).grid(column=1, row=5)
player7 = Entry(playerFrame).grid(column=1, row=6)
#Function for the addPlayer button / Função para o botão addPlayer
def AddPlayer():
pass
#Button to add more players / Botão para adicionar mais jogadores
addPlayer = Button(playerFrame, text="Add player / Adicionar jogador").grid(column=1, row=7)
playerFrame.pack(side=TOP, anchor=NW)
root.mainloop()
I need the button to go down and add the entry line above it
You can use grid_info()
to get the grid information of addPlayer
button and use this information to move the button one row below and create Label
and Entry
in the current row of the button:
#Function for the addPlayer button / Função para o botão addPlayer
def AddPlayer():
# get the row from grid information of the "addPlayer" button
info = addPlayer.grid_info()
row = info['row']
# move "addPlayer" button to next row
info['row'] = row + 1
addPlayer.grid(**info)
# create label and entry in current row
Label(playerFrame, text=row+1).grid(row=row, column=0)
Entry(playerFrame).grid(row=row, column=1)
#Button to add more players / Botão para adicionar mais jogadores
addPlayer = Button(playerFrame, text="Add player / Adicionar jogador", command=AddPlayer)
addPlayer.grid(column=1, row=7)