Search code examples
pythonfunctiontkintertkinter-texttkinter-button

Moving text from a button to another using python tkinter


def move(sp, t):
    if sp == "eightA":
        sps = eightA
    if t == "sixA":
        st = sixA
    if st == " ":
        # target is empty
        sps["text"] = " "
        st["text"] = str(sps["text"])

Hello Everyone, Im trying to make this function to "move" text from a tkinter button to another, lets say sp is what i want to move, and t is the target so i want to move text from the button eightA to sixA, also note that i want to be able to use this function on any 2 buttons, Its hard to explain, but please help if you can, the code above is one i tried out of alot other which didnt work, Thanks


Solution

  • Procedure

    • Store the text on first button in a variable
    • Configure first button to have no text
    • Configure second button to have text stored in variable

    Example

    btn1 = tk.Button(text="Hello")
    btn2 = tk.Button(text="Not Hello")
    
    # store text on btn1 in a variable
    txt = btn1['text']
    # configure btn1 to have no text
    btn1.config(text="")
    # configure btn2 to have text stored in variable
    btn2.config(text=txt)
    

    So your function would look like

    def move(btn1, btn2):
        txt = btn1['text']
        btn1.config(text=" ")
        btn2.config(text=txt)