Button motion animation in tkinter












1















I want to move a button animated. For example it starts from x=0 and y=0, after 0.1 second x=1 and y=1 ... x=50 and y=50.



I tried this:



import tkinter
import time

b=tkinter.Button(text="Example")
for i in range(50):
i+=1
b.place(x=i, y=i)
time.sleep(0.1)


The window opened after all place commands were executed.










share|improve this question























  • Because tkinter is event based you end up freezing the main loop when using sleep() in it. The after() method works with the event manager so this does not happen.

    – Mike - SMT
    Nov 21 '18 at 20:15
















1















I want to move a button animated. For example it starts from x=0 and y=0, after 0.1 second x=1 and y=1 ... x=50 and y=50.



I tried this:



import tkinter
import time

b=tkinter.Button(text="Example")
for i in range(50):
i+=1
b.place(x=i, y=i)
time.sleep(0.1)


The window opened after all place commands were executed.










share|improve this question























  • Because tkinter is event based you end up freezing the main loop when using sleep() in it. The after() method works with the event manager so this does not happen.

    – Mike - SMT
    Nov 21 '18 at 20:15














1












1








1








I want to move a button animated. For example it starts from x=0 and y=0, after 0.1 second x=1 and y=1 ... x=50 and y=50.



I tried this:



import tkinter
import time

b=tkinter.Button(text="Example")
for i in range(50):
i+=1
b.place(x=i, y=i)
time.sleep(0.1)


The window opened after all place commands were executed.










share|improve this question














I want to move a button animated. For example it starts from x=0 and y=0, after 0.1 second x=1 and y=1 ... x=50 and y=50.



I tried this:



import tkinter
import time

b=tkinter.Button(text="Example")
for i in range(50):
i+=1
b.place(x=i, y=i)
time.sleep(0.1)


The window opened after all place commands were executed.







python button tkinter jquery-animate motion






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 19:02









TomdzsóTomdzsó

82




82













  • Because tkinter is event based you end up freezing the main loop when using sleep() in it. The after() method works with the event manager so this does not happen.

    – Mike - SMT
    Nov 21 '18 at 20:15



















  • Because tkinter is event based you end up freezing the main loop when using sleep() in it. The after() method works with the event manager so this does not happen.

    – Mike - SMT
    Nov 21 '18 at 20:15

















Because tkinter is event based you end up freezing the main loop when using sleep() in it. The after() method works with the event manager so this does not happen.

– Mike - SMT
Nov 21 '18 at 20:15





Because tkinter is event based you end up freezing the main loop when using sleep() in it. The after() method works with the event manager so this does not happen.

– Mike - SMT
Nov 21 '18 at 20:15












1 Answer
1






active

oldest

votes


















3














Do not pause/sleep your python program. As pointed out by @Mike-SMT, it may end up freezing up your mainloop. If you want to do animation, after is the way to go.



import tkinter as tk

root = tk.Tk()
b = tk.Button(root, text="Example")

def move(i):
if i<=50:
b.place(x=i, y=i)
b.after(100, lambda: move(i)) #after every 100ms
i = i+1

move(0) #Start animation instantly
root.mainloop()


enter image description here






share|improve this answer


























  • Change this root.after(0, lambda: move(0)) to move(0). There is no point to doing a lambda or after() here.

    – Mike - SMT
    Nov 21 '18 at 20:13













  • @Mike-SMT: oh, I'm sorry. My mistake. You are correct that the use of after for the first step isn't necessary. I'll remove my comment.

    – Bryan Oakley
    Nov 21 '18 at 20:17











  • @BryanOakley Thanks. I was worried for a second. I was like "I am sure that after/lambda is overkill here." lol.

    – Mike - SMT
    Nov 21 '18 at 20:21











  • @Mike-SMT Yes, Sorry didn't realise that. Initially I wanted to start the animation after some time, so I had an after there as well. But it is not needed.

    – Miraj50
    Nov 21 '18 at 21:13











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418926%2fbutton-motion-animation-in-tkinter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Do not pause/sleep your python program. As pointed out by @Mike-SMT, it may end up freezing up your mainloop. If you want to do animation, after is the way to go.



import tkinter as tk

root = tk.Tk()
b = tk.Button(root, text="Example")

def move(i):
if i<=50:
b.place(x=i, y=i)
b.after(100, lambda: move(i)) #after every 100ms
i = i+1

move(0) #Start animation instantly
root.mainloop()


enter image description here






share|improve this answer


























  • Change this root.after(0, lambda: move(0)) to move(0). There is no point to doing a lambda or after() here.

    – Mike - SMT
    Nov 21 '18 at 20:13













  • @Mike-SMT: oh, I'm sorry. My mistake. You are correct that the use of after for the first step isn't necessary. I'll remove my comment.

    – Bryan Oakley
    Nov 21 '18 at 20:17











  • @BryanOakley Thanks. I was worried for a second. I was like "I am sure that after/lambda is overkill here." lol.

    – Mike - SMT
    Nov 21 '18 at 20:21











  • @Mike-SMT Yes, Sorry didn't realise that. Initially I wanted to start the animation after some time, so I had an after there as well. But it is not needed.

    – Miraj50
    Nov 21 '18 at 21:13
















3














Do not pause/sleep your python program. As pointed out by @Mike-SMT, it may end up freezing up your mainloop. If you want to do animation, after is the way to go.



import tkinter as tk

root = tk.Tk()
b = tk.Button(root, text="Example")

def move(i):
if i<=50:
b.place(x=i, y=i)
b.after(100, lambda: move(i)) #after every 100ms
i = i+1

move(0) #Start animation instantly
root.mainloop()


enter image description here






share|improve this answer


























  • Change this root.after(0, lambda: move(0)) to move(0). There is no point to doing a lambda or after() here.

    – Mike - SMT
    Nov 21 '18 at 20:13













  • @Mike-SMT: oh, I'm sorry. My mistake. You are correct that the use of after for the first step isn't necessary. I'll remove my comment.

    – Bryan Oakley
    Nov 21 '18 at 20:17











  • @BryanOakley Thanks. I was worried for a second. I was like "I am sure that after/lambda is overkill here." lol.

    – Mike - SMT
    Nov 21 '18 at 20:21











  • @Mike-SMT Yes, Sorry didn't realise that. Initially I wanted to start the animation after some time, so I had an after there as well. But it is not needed.

    – Miraj50
    Nov 21 '18 at 21:13














3












3








3







Do not pause/sleep your python program. As pointed out by @Mike-SMT, it may end up freezing up your mainloop. If you want to do animation, after is the way to go.



import tkinter as tk

root = tk.Tk()
b = tk.Button(root, text="Example")

def move(i):
if i<=50:
b.place(x=i, y=i)
b.after(100, lambda: move(i)) #after every 100ms
i = i+1

move(0) #Start animation instantly
root.mainloop()


enter image description here






share|improve this answer















Do not pause/sleep your python program. As pointed out by @Mike-SMT, it may end up freezing up your mainloop. If you want to do animation, after is the way to go.



import tkinter as tk

root = tk.Tk()
b = tk.Button(root, text="Example")

def move(i):
if i<=50:
b.place(x=i, y=i)
b.after(100, lambda: move(i)) #after every 100ms
i = i+1

move(0) #Start animation instantly
root.mainloop()


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 21:11

























answered Nov 21 '18 at 19:23









Miraj50Miraj50

2,6001824




2,6001824













  • Change this root.after(0, lambda: move(0)) to move(0). There is no point to doing a lambda or after() here.

    – Mike - SMT
    Nov 21 '18 at 20:13













  • @Mike-SMT: oh, I'm sorry. My mistake. You are correct that the use of after for the first step isn't necessary. I'll remove my comment.

    – Bryan Oakley
    Nov 21 '18 at 20:17











  • @BryanOakley Thanks. I was worried for a second. I was like "I am sure that after/lambda is overkill here." lol.

    – Mike - SMT
    Nov 21 '18 at 20:21











  • @Mike-SMT Yes, Sorry didn't realise that. Initially I wanted to start the animation after some time, so I had an after there as well. But it is not needed.

    – Miraj50
    Nov 21 '18 at 21:13



















  • Change this root.after(0, lambda: move(0)) to move(0). There is no point to doing a lambda or after() here.

    – Mike - SMT
    Nov 21 '18 at 20:13













  • @Mike-SMT: oh, I'm sorry. My mistake. You are correct that the use of after for the first step isn't necessary. I'll remove my comment.

    – Bryan Oakley
    Nov 21 '18 at 20:17











  • @BryanOakley Thanks. I was worried for a second. I was like "I am sure that after/lambda is overkill here." lol.

    – Mike - SMT
    Nov 21 '18 at 20:21











  • @Mike-SMT Yes, Sorry didn't realise that. Initially I wanted to start the animation after some time, so I had an after there as well. But it is not needed.

    – Miraj50
    Nov 21 '18 at 21:13

















Change this root.after(0, lambda: move(0)) to move(0). There is no point to doing a lambda or after() here.

– Mike - SMT
Nov 21 '18 at 20:13







Change this root.after(0, lambda: move(0)) to move(0). There is no point to doing a lambda or after() here.

– Mike - SMT
Nov 21 '18 at 20:13















@Mike-SMT: oh, I'm sorry. My mistake. You are correct that the use of after for the first step isn't necessary. I'll remove my comment.

– Bryan Oakley
Nov 21 '18 at 20:17





@Mike-SMT: oh, I'm sorry. My mistake. You are correct that the use of after for the first step isn't necessary. I'll remove my comment.

– Bryan Oakley
Nov 21 '18 at 20:17













@BryanOakley Thanks. I was worried for a second. I was like "I am sure that after/lambda is overkill here." lol.

– Mike - SMT
Nov 21 '18 at 20:21





@BryanOakley Thanks. I was worried for a second. I was like "I am sure that after/lambda is overkill here." lol.

– Mike - SMT
Nov 21 '18 at 20:21













@Mike-SMT Yes, Sorry didn't realise that. Initially I wanted to start the animation after some time, so I had an after there as well. But it is not needed.

– Miraj50
Nov 21 '18 at 21:13





@Mike-SMT Yes, Sorry didn't realise that. Initially I wanted to start the animation after some time, so I had an after there as well. But it is not needed.

– Miraj50
Nov 21 '18 at 21:13


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418926%2fbutton-motion-animation-in-tkinter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen