-1

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

dkol
  • 27
  • 4
  • It's not clear what you're asking. You have complete control of putting widgets above, below, or to the side of other widgets. All of the features to do so are documented in many places. Can you add a [mcve] that illustrates the problem you're having? – Bryan Oakley Jun 30 '21 at 17:35

2 Answers2

1

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)
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • I tried this but I got this error`Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\pedro\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__ return self.func(*args) File "C:\Users\pedro\Documents\jobs\Analista\teste.py", line 26, in AddPlayer info = addPlayer.grid_info('row') AttributeError: 'NoneType' object has no attribute 'grid_info'` – dkol Jul 01 '21 at 11:42
  • 1
    Did you split the line `addPlayer = Button(...).grid(...)` into two lines like in my answer? – acw1668 Jul 01 '21 at 12:01
0

Possible Solution:

def AddPlayer():
    new_entry = Entry(playerFrame).grid(column=1, row=) # Figure out a way to make the row number increase

#Button to add more players / Botão para adicionar mais jogadores

addPlayer = Button(playerFrame, text="Add player / Adicionar jogador", command=AddPlayer).grid(column=1, row=7)