-1

我对 tkinter 还很陌生,我需要制作一个按钮,在同一个按钮上方的行中添加一个条目小部件,这可能吗?

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()

我需要按钮向下并在其上方添加输入行

4

2 回答 2

1

您可以使用grid_info()获取addPlayer按钮的网格信息并使用此信息将按钮移动到下一行并在按钮的当前行中创建Label和:Entry

#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)
于 2021-07-01T03:45:19.700 回答
0

可能的解决方案:

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)
于 2021-06-30T18:07:31.180 回答