A ticker for long messages (Python/Tkinter)

vegaseat 1 Tallied Votes 4K Views Share

If you have a long message to display in a large readable font, then this little Tkinter GUI toolkit code might help.

''' Tk_Text_ticker102.py
using Tkinter to create a marquee/ticker
uses a display width of 20 characters
not superbly smooth but good enough to read
tested with Python27 and Python33  by  vegaseat  04oct2013
'''

import time
try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

root = tk.Tk()

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

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

s1 = "We don't really care why the chicken crossed the road.  "
s2 = "We just want to know if the chicken is on our side of the "
s3 = "road or not. The chicken is either for us or against us.  "
s4 = "There is no middle ground here.  (George W. Bush)"
# pad front and end of text with spaces
s5 = ' ' * text_width
# concatenate it all
s = s5 + s1 + s2 + s3 + s4 + s5

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

root.mainloop()
Ismatus3 13 Junior Poster in Training

Hello ,
Thank you for this ticker example , It's realy a useful result of the Tkinter module .

TrustyTony 888 pyMod Team Colleague Featured Poster

This is what I got from theantonio.neal.39's post by tinkering:

from tkinter import *

def move():   
    if message.winfo_x() + message.move >= message.x_limit or message.winfo_x() + message.move < 0:
        message.move = -message.move
    message.place(x=message.winfo_x() + message.move)
    message.after(message.delay, move)

gui = Tk()
gui.geometry('8000x100+1+600')                 
gui.title('ALERTS')
gui.config (bg = 'blue')                       
message = Label(gui, text = 'this is a demo')
message.config (fg = 'white',bg = 'blue', font=('times','60'))
message.x_limit = 2000
message.move = 2
message.delay = 5
message.place(x=1)
message.after(10, move)
gui.mainloop()
Ismatus3 13 Junior Poster in Training

Hello pyTony , i don't understand you about my last reply . This call to tkinter works only with python3.x ?

Ismatus3 13 Junior Poster in Training

with python 2.x , it's just : `import from Tkinter

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Using the Tkinter canvas for the ticker makes it run a little smoother ...

''' Tk_Canvas_ticker1.py
using a Tkinter canvas to create a marquee/ticker via
canvas.create_text(x, y, anchor='nw', text=s, font=font)
canvas.move(object, x_increment, y_increment)
tested with Python27 and Python33  by  vegaseat  04oct2013
'''

import time
try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

root = tk.Tk()

canvas = tk.Canvas(root, height=80, width=600, bg="yellow")
canvas.pack()

font = ('courier', 48, 'bold')

text_width = 15
s1 = "We don't really care why the chicken crossed the road.  "
s2 = "We just want to know if the chicken is on our side of the "
s3 = "road or not. The chicken is either for us or against us.  "
s4 = "There is no middle ground here.  (George W. Bush)"
# pad front and end of text with spaces
s5 = ' ' * text_width
# concatenate it all
s = s5 + s1 + s2 + s3 + s4 + s5

x = 1
y = 2
text = canvas.create_text(x, y, anchor='nw', text=s, font=font)

dx = 1
dy = 0  # use horizontal movement only
# the pixel value depends on dx, font and length of text
pixels = 9000
for p in range(pixels):
    # move text object by increments dx, dy
    # -dx --> right to left
    canvas.move(text, -dx, dy)
    canvas.update()
    # shorter delay --> faster movement
    time.sleep(0.005)
    #print(k)  # test, helps with pixel value

root.mainloop()
Ismatus3 13 Junior Poster in Training

Hello ,
The second example works better , It just stoping a small periods sometimes . My question is how can i put the Tkinter window on the top of the sceen ?

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

To put the tkinter window on top of your screen you can use ...

x = 0
y = 0
# set UpperLeftCorner x, y position of root
root.geometry("+%d+%d" % (x, y))
bobles 0 Newbie Poster

Is there any good way to scale the pixels variable by the the input? Like is there a way to expresses pixels as a function of the text length, the font, and the fount size?

Gribouillis 1,391 Programming Explorer Team Colleague

Once the text item is created, you can obtain a bounding box with canvas.bbox(item=text), or something similar.

Mohamed_65 0 Newbie Poster

Hello,

How to make the canvas moves continuously without stopping?

BR,

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.