Button motion animation in tkinter
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
add a comment |
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
Because tkinter is event based you end up freezing the main loop when usingsleep()
in it. Theafter()
method works with the event manager so this does not happen.
– Mike - SMT
Nov 21 '18 at 20:15
add a comment |
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
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
python button tkinter jquery-animate motion
asked Nov 21 '18 at 19:02
TomdzsóTomdzsó
82
82
Because tkinter is event based you end up freezing the main loop when usingsleep()
in it. Theafter()
method works with the event manager so this does not happen.
– Mike - SMT
Nov 21 '18 at 20:15
add a comment |
Because tkinter is event based you end up freezing the main loop when usingsleep()
in it. Theafter()
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
add a comment |
1 Answer
1
active
oldest
votes
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()
Change thisroot.after(0, lambda: move(0))
tomove(0)
. There is no point to doing alambda
orafter()
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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()
Change thisroot.after(0, lambda: move(0))
tomove(0)
. There is no point to doing alambda
orafter()
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
add a comment |
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()
Change thisroot.after(0, lambda: move(0))
tomove(0)
. There is no point to doing alambda
orafter()
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
add a comment |
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()
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()
edited Nov 21 '18 at 21:11
answered Nov 21 '18 at 19:23
Miraj50Miraj50
2,6001824
2,6001824
Change thisroot.after(0, lambda: move(0))
tomove(0)
. There is no point to doing alambda
orafter()
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
add a comment |
Change thisroot.after(0, lambda: move(0))
tomove(0)
. There is no point to doing alambda
orafter()
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Because tkinter is event based you end up freezing the main loop when using
sleep()
in it. Theafter()
method works with the event manager so this does not happen.– Mike - SMT
Nov 21 '18 at 20:15