0

I am trying to arrange a sentence which have a few words onto grid button. When a word of that sentence is clicked, I want to remove that word of grid button after that is moved to another row of grid button. How to remove only that grid button which is clicked ? Clicked over and over again, those words will be moved to other position and disappeared one by one from original position of grid buttons.

shuffle_btn.grid_forget(row=r, column=c)
shuffle_btn.grid_remove(row=r, column=c)

does not work. It gives this error "TypeError: grid_forget() got an unexpected keyword argument 'row'

How do I solve this problem? Thanks in advance.

The code is as follows, which does work moving but does not work removing.

from tkinter import *
root = Tk()
root.title('words clicking')
root.geometry("1000x600")
btn_lst = ["Do", "you", "have", "a", "book", "?"]
w_cnt = 0
# Create answer buttons
def move_to_ans(row, column):
    global cnt, w_cnt
    r = row
    c = column
    if w_cnt < cnt:
        ans_btn = Button(root, text=btn_lst[column],  font=(
        "Times", 20), fg="black")
        ans_btn.grid(row=1, column=w_cnt, sticky=N+E+W+S, pady=100)
        # shuffle_btn.grid_forget(row=r, column=c)
        w_cnt += 1
 # Create buttons of each word in a sentence
 cnt = len(btn_lst)
 for btn in range(cnt):
     shuffle_btn = Button(root, text=btn_lst[btn], command=lambda row=0, column=btn: move_to_ans(row, 
     column), font=("Times", 20), fg="blue")
 shuffle_btn.grid(row=0, column=btn, sticky=N+E+W+S, pady=100)
 root.mainloop()

Expected result : shuffle words before clicked : (have Do book ? a you) --> How to delete when clicked

answer words after moving from clicked : (Do you have a book ?)

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

1 Answers1

0

You can use root.grid_slaves(row=r, column=c)[0].destroy() inside move_to_ans():

def move_to_ans(row, column):
    global cnt, w_cnt
    r = row
    c = column
    if w_cnt < cnt:
        ans_btn = Button(root, text=btn_lst[column], font=("Times", 20), fg="black")
        ans_btn.grid(row=1, column=w_cnt, sticky=N+E+W+S, pady=100)
        root.grid_slaves(row=r, column=c)[0].destroy() # destroy the clicked button
        w_cnt += 1

Another way is to pass the button instance to move_to_ans().

acw1668
  • 40,144
  • 5
  • 22
  • 34