Hi,

I was wondering how someone would go about making a scrolling ticker or marquee using Tkinter in such a way that allows you to easily add on text and remove.. kinda like a news scroller on the bottom of the tv?

thanks all
a1eio

Recommended Answers

All 5 Replies

A simple way to make a ticker ...

# using Tkinter to create a marquee/ticker
# uses a display width of 20 characters
# not superly smooth but good enough to read

import Tkinter as tk
import time

root = tk.Tk()

# width=width chars, height=lines text
text = tk.Text(root, width=20, height=1, bg='yellow')
text.pack()

# use a proportional font to handle spaces correctly
text.config(font=('courier', 24, 'bold'))

s1 = "I was wondering how someone would go about making a scrolling ticker"

# pad front and end with 20 spaces
s2 = ' ' * 20
s = s2 + s1 + s2

for k in range(len(s)):
    # use string slicing to do the trick
    ticker_text = s[k:k+20]
    text.insert("1.1", ticker_text)
    root.update()
    # delay by 0.15 seconds
    time.sleep(0.15)

root.mainloop()

hi vega,

thats brilliant, my method involved updating a label with the text all shuffled 1 place to the left each time.

I'm having some trouble understand your slicing method. I can't quite work out what the +20 is for, is that just a number 'that works' or is it to compensate for the 20 spaces padding the text, or is it for the width of the text wiget.. or are all those numbers infact linked.

thanks in advance.
a1eio

ticker_text = s[k:k+20] simply takes a slice of text 20 characters wide moving to the right as k increments.

There are more involved schemes that use canvas text. They can move one pixel at time and are somewhat smoother.

thanks.

Yea i'm using the text version for now, for simplicity and so i can get the rest of the program running, but i'll probably try to work out a canvas method later. i'll post it if it works, cause i had a hard time finding anything on google

Thank you and may God bless you for this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.