Python Forum
[Tkinter] moving label position
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] moving label position
#1
I am trying to animate a label by moving it little by little. I have the following code but it is not doing anything.

global status_name
status_name = Label(root, text='',bd=0, relief=GROOVE)
status_name.pack(fill=X, side="right",padx=90,pady=20)

status_name.after(1000,testing)

# testing function
global xnum
def testing():
    global xnum
    xnum = 90
    xnum = xnum - 10
    status_name.config(padx=xnum)
from my understanding it should run the function and change pady -10 every second but nothing happened, why?
Reply
#2
You could use place instead of pack. Make a frame to hold the label and change the x coordinate of the label using place. You'll want to put the label inside a frame so you can still use pack for the other widgets in your top level window.
import tkinter as tk

def testing():
    """Bounce text label inside frame"""
    global dx

    max_x = frame.winfo_width() - text.winfo_width()
    x = text.winfo_x() + dx
    if not 0 < x < max_x:
        dx = -dx
    text.place(x=x, y=0)
    root.after(5, testing)

dx = 1
root = tk.Tk()
frame = tk.Frame(root, width=300, height=20)
frame.pack(expand=True, fill=tk.X)
text = tk.Label(frame, text='Hello')
text.place(x=0, y=0)
testing()
root.mainloop()
Notice that I set the size of the frame. pack() does more than place a widget in the frame, it also uses the widget size when computing the frame size. This does not happen when using place. If I do not set the frame size explicitly it will default to 0x0.

A few comments about your code. This
Quote:global status_name
in line 1 does nothing. global tells Python to look for a name in the global scope instead of the local scope. Line 1 is in the global scope. global is only useful when it is used inside a function or a class method.

.after() only calls the function once. If you want to repeat something over and over you need to call .after() multiple times. Check out how I do this in my testing() function.

Don't use # for commenting a function.
# testing function
def testing():
Instead use a docstring. If you comment your code using doc strings you can use the python "help" to get information about the function and you can use automatic documentation tools.
def testing():
    """Moving label test function"""
Output:
>>> help(testing) Help on function testing in module __main__: testing() Moving label test function
The way xnum is used doesn't make any sense.
global xnum
def testing():
    global xnum
    xnum = 90
    xnum = xnum - 10
    status_name.config(padx=xnum)
xnum is initialized and calculated inside the testing() function. It will always be 80. That would explain why your program doesn't appear to do anything. Maybe you wanted to do this?
xnum = 90
def testing():
    global xnum
    xnum = xnum - 10
    status_name.config(padx=xnum)
This code initialize xnum once and adjusts xnum each time testing() is called.

But you really, really don't want to use pad to move the label. pad will change how much padding is added to EACH SIDE of the label. Changing pad will change the size of the widget, not just the location of the text.
import tkinter as tk

def testing():
    """Adjust padding.  Probably not what you want"""
    global pad, dx

    pad += dx
    if not 0 < pad < 100:
        dx = -dx
    text.pack(padx=pad)
    root.after(5, testing)

pad = 0
dx = 1
root = tk.Tk()
frame = tk.Frame(root)
frame.pack(expand=True, fill=tk.X)
text = tk.Label(frame, text='Hello')
text.pack(padx=0)
testing()
root.mainloop()
rwahdan likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020