2

I was wondering if there's a way to move the text within a text box in Tkinter. For example, a user enters in an incorrect value, and the text "shakes" back and forth (like shaking your head to signal "no").

Thanks!

1 Answer 1

2

Here's a rough example using create text and move method on a canvas widget. You can adjust values and the code could be cleaned up some / adjusted to taste. I think this what you meant by "shake":

import tkinter as tk

CORRECT_ANSWER = 'Some answer'

def callback():

    if var.get() != CORRECT_ANSWER:
        for i in range(10, 50):
            canvas.move(text, -i if i% 2 == 0 else i, 0)
            canvas.update()
            canvas.move(text, i if i % 2 == 0 else -i, 0)                     
            canvas.update()

if __name__ == '__main__':

    root = tk.Tk()
    var = tk.StringVar()
    canvas = tk.Canvas(root, bg="black")
    canvas.pack(fill=tk.BOTH, expand=1)
    text = canvas.create_text(200, 100, text='Enter the answer to this question.',
        fill='white')
    entry = tk.Entry(root, textvariable = var)
    entry.pack(side=tk.LEFT, fill=tk.X, expand=1)
    submit = tk.Button(root, text='Submit', command=callback)
    submit.pack(side=tk.LEFT, fill=tk.X, expand=1)
    root.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

Yes! Thank you for this, exactly what I was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.