Home > Software engineering >  I want to a create a moving label
I want to a create a moving label

Time:01-26

import tkinter as tk
from tkinter import *
import keyboard
import time

a = 120
root = tk.Tk()
root.state('zoomed')
root.configure(bg='white')
root.resizable(False, False)
a = 30
x = 0


def increase():
    global a
    a  = 10


def ar():
    for i in range(50):
        labele1.place(x=120, y=a)
        root.after(1000, increase)


c1 = Canvas(root, width=700, height=700, bg='gray95')
c1.place(x=450, y=60)
labele1 = tk.Label(c1, text='_______', font='Calibri 20')
startbutton = tk.Button(text='Başla', command=ar)
startbutton.pack()

root.mainloop()

I want to create a moving label which moves per second. Everything looks clear but it doesn't work well. I want to make a car race app but first the walls should move back and it exit the project

CodePudding user response:

The idea to use root.after is correct, but the implementation is wrong. Just because you update the value of a inside a function, tkinter will not automatically update that value with its widget unless you manually ask it to. So what you can do is:

  • Make the label a canvas object (recommended since you will be making a game and canvas is closer to give you tools you need):
def ar():
    global a
    a  = 10 # Increase the value of a

    c1.coords('wall', [120, a]) # Change the coord of the tag 'wall'
    root.after(25, ar) # Repeat this function every 25 ms

....

labele1 = tk.Label(c1, text='_______', font='Calibri 20')
c1.create_window(120, a, window=labele1, tags='wall') # Initial coords -> x=120, y=30
  • Change the location of the label using place_configure:
def ar():
    global a
    a  = 10 # Increase the value of a

    labele1.place_configure(y=a)
    root.after(25, ar) # Repeat this function every 25 ms

....

labele1 = tk.Label(c1, text='_______', font='Calibri 20')
labele1.place(x=120, y=a)

CodePudding user response:

Plus checking frame size and decreasing functions. the main problem was a minor bug which you should change in line 37.

import tkinter as tk
from tkinter import *
import keyboard
import time

a = 120
root = tk.Tk()
root.state('zoomed')
root.configure(bg='white')
root.resizable(False, False)
a = 30
x = 0

moving_rate = 10
max_y = 600
min_y = 30

flag_mov_func= 1
def moving_func(): #increase or decrease function
    global a
    global flag_mov_func

    if flag_mov_func :
        a  = moving_rate
        if  a   moving_rate == max_y: #simple logic bouncing if it ends
            flag_mov_func=0
    elif flag_mov_func==0 :
        a -= moving_rate
        if  a - moving_rate == min_y:
            flag_mov_func=1
def ar():

    moving_func() 
    labele1.place(x=120, y=a) 
    root.after(1000, ar)

flag_is_called=1
def button1(): #new update on code to make the button work once.
    global  flag_is_called
    if flag_is_called:
        flag_is_called=0
        ar()

c1 = Canvas(root, width=700, height=700, bg='gray95')
c1.place(x=450, y=60)
labele1 = tk.Label(c1, text='_______', font='Calibri 20')
startbutton = tk.Button(text='Başla', command=button1)
startbutton.pack()

root.mainloop()
  • Related