Python Forum
[Tkinter] scrolling text in tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] scrolling text in tkinter
#1
Hi all,
I am writing some python to play songs among other things.
That is working without problems but a friend has seen an MS Windows program which will also show the lyrics and slowly scroll them up the screen.
I have been looking for some method of doing this in python on Linux but can only find code which shows a scrollbar. Manual control of scrolling is not what I want. Something more like karaoke ( without the dancing ball :) ) is what I am trying to find. I have been using Python 2.7.12.
Can anyone give me some pointers please?
Reply
#2
I tossed this together to give you an example of one way it can be done.  Basically, you find out the current index, and repeatedly increment that until the index points somewhere that isn't currently visible, and then move the widget to display that index.
from tkinter import scrolledtext as st
from tkinter import constants as const

def scroll_textbox(elem):
    # get the current index
    current = float(elem.index(const.CURRENT))
    new = current
    # keep incrementing the index until it's not visible
    while elem.bbox(new):
        new += 1
    # make sure the new index is visible
    elem.see(new)
    # move the index again in 250ms
    elem.after(250, lambda: scroll_textbox(elem))

if __name__ == "__main__":
    # a little setup to demonstrate...
    stext = st.ScrolledText(bg='white', height=10)
    # kick off our callback
    stext.after(1000, lambda: scroll_textbox(stext))
    # shove a large amount of text in there
    stext.insert(const.END, ''' here's a very large block of text.
    
    it just keeps going...
    ''' + ('...and going...\n\n'*100))
    stext.pack(fill=const.BOTH, side=const.LEFT, expand=True)
    stext.focus_set()
    stext.mainloop()
Reply
#3
Are you set on tkinter? Or can it be any library? 
Often use this as a credit screen for resources, developers, etc. for games. Thus the reason why its in pygame.

import pygame as pg
 
pg.init()
 
text_list = '''I'm Henry the eighth, I am
Henry the eighth, I am, I am
I got married to the widow next door
She's been married seven times before
 
And every one was an Henry (Henry)
She wouldn't have a Willy or a Sam (No Sam)
I'm her eighth old man, I'm Henry
Henry the eighth I am
 
Second verse, same as the first
 
I'm Henry the eighth, I am
Henry the eighth, I am, I am
I got married to the widow next door
She's been married seven times before
 
And every one was an Henry (Henry)
She wouldn't have a Willy or a Sam (No Sam)
I'm her eighth old man, I'm Henry
Henry the eighth I am
 
I'm Henry the eighth, I am
Henry the eighth, I am, I am
I got married to the widow next door
She's been married seven times before
 
And every one was an Henry (Henry)
She wouldn't have a Willy or a Sam (No Sam)
I'm her eighth old man, I'm Henry
Henry the eighth I am
 
H-E-N-R-Y
Henry (Henry)
Henry (Henry)
Henry the eighth I am, I am
Henry the eighth I am
Yeah!
'''.split('\n')
 
class Credits:
    def __init__(self, screen_rect, lst):
        self.srect = screen_rect
        self.lst = lst
        self.size = 16
        self.color = (255,0,0)
        self.buff_centery = self.srect.height/2 + 5
        self.buff_lines = 50
        self.timer = 0.0
        self.delay = 0
        self.make_surfaces()
 
 
    def make_text(self,message):
        font = pg.font.SysFont('Arial', self.size)
        text = font.render(message,True,self.color)
        rect = text.get_rect(center = (self.srect.centerx, self.srect.centery + self.buff_centery) )
        return text,rect
 
    def make_surfaces(self):
        self.text = []
        for i, line in enumerate(self.lst):
            l = self.make_text(line)
            l[1].y += i*self.buff_lines
            self.text.append(l)
 
    def update(self):
        if pg.time.get_ticks()-self.timer > self.delay:
            self.timer = pg.time.get_ticks()
            for text, rect in self.text:
                rect.y -= 1
 
    def render(self, surf):
        for text, rect in self.text:
            surf.blit(text, rect)
 
screen = pg.display.set_mode((800,600))
screen_rect = screen.get_rect()
clock = pg.time.Clock()
done = False
cred = Credits(screen_rect, text_list)
pg.mixer.music.load("filename.mp3")
pg.mixer.music.play()
 
while not done:
    for event in pg.event.get(): 
        if event.type == pg.QUIT:
            done = True
    screen.fill((0,0,0))
    cred.update()
    cred.render(screen)
    pg.display.update()
    clock.tick(60)
Recommended Tutorials:
Reply
#4
@nilamo,
Thanks for your code. I thought it does what I wanted  but when my friend looked at it he said that it was not scrolling as smoothly as what he had seen. Also scrolledtext doesn't seem to be available in python 2.7.12 but is in 3.5.2
I expect I could convert my current code although I am using urllib2 which doesn't seem to be available in 3.5.2 and maybe eyeD3 isn't either.

@metulburr,
My friend says the scrolling is very much like the code you provided but i am quite new to gui programming in python and have no knowledge of pygame so i would have to re-write some 400 lines of code assuming the other parts are also available.

What I have written so for includes sections which allow playing videos which I can display on my TV rather than the laptop, display web pages, play songs and some other bits and pieces.

i guess I will have to look into both further to see what I can do.

If I can use tkinter and pygame together perhaps that would be the way to go.
Thanks again to both of you.
Reply
#5
(Oct-18-2016, 08:45 PM)Barrowman Wrote: If I can use tkinter and pygame together perhaps that would be the way to go.
using these two libraries together are notorious for problems. It is better not to.
Recommended Tutorials:
Reply
#6
(Oct-18-2016, 08:51 PM)metulburr Wrote:
(Oct-18-2016, 08:45 PM)Barrowman Wrote: If I can use tkinter and pygame together perhaps that would be the way to go.
using these two libraries together are notorious for problems. It is better not to.

Wow, that's a shame I copied and slightly modified your code and it worked within my program and i could still select other options in my menu except that the pygame window didn't actually close. That's a shame.
Reply
#7
(Oct-18-2016, 08:45 PM)Barrowman Wrote: @nilamo,
Thanks for your code. I thought it does what I wanted  but when my friend looked at it he said that it was not scrolling as smoothly as what he had seen. Also scrolledtext doesn't seem to be available in python 2.7.12 but is in 3.5.2
I expect I could convert my current code although I am using urllib2 which doesn't seem to be available in 3.5.2 and maybe eyeD3 isn't either.

Scrolledtext is pure-python.  So if you want to go that way, you can make your own version fairly easily (like, copy/paste easily, and maybe rename tkinter to Tkinter).  https://hg.python.org/cpython/file/3.5/L...ledtext.py
Reply
#8
(Oct-18-2016, 09:04 PM)nilamo Wrote:
(Oct-18-2016, 08:45 PM)Barrowman Wrote: @nilamo,
Thanks for your code. I thought it does what I wanted  but when my friend looked at it he said that it was not scrolling as smoothly as what he had seen. Also scrolledtext doesn't seem to be available in python 2.7.12 but is in 3.5.2
I expect I could convert my current code although I am using urllib2 which doesn't seem to be available in 3.5.2 and maybe eyeD3 isn't either.

Scrolledtext is pure-python.  So if you want to go that way, you can make your own version fairly easily (like, copy/paste easily, and maybe rename tkinter to Tkinter).  https://hg.python.org/cpython/file/3.5/L...ledtext.py

Okay so I copied it, changed tkinter to Tkinter and saved it to /usr/lib/python2.7
but when I start the script i get this error:
Quote:Traceback (most recent call last):
  File "./scrolling1.py", line 2, in <module>
    from Tkinter import Scrolledtext as st
ImportError: cannot import name Scrolledtext
Reply
#9
Right, you're creating that file instead of importing it from tkinter. So it'd just be "import {whatever-you-named-the-file} as st"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Updating tkinter text BliepMonster 5 5,982 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,866 Jun-26-2022, 06:26 PM
Last Post: menator01
  tkinter change the text of the checkbox zazas321 1 3,833 Sep-17-2021, 06:19 AM
Last Post: zazas321
  tkinter text widget word wrap position chrisdb 6 7,568 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,643 Mar-10-2021, 04:21 PM
Last Post: Sir
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 3,998 Dec-28-2020, 10:00 PM
Last Post: nanok66
Photo Tkinter TEXT background image _ShevaKadu 5 7,767 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  tkinter | Button color text on Click Maryan 2 3,371 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Text Upload LearningLittlebyLittle 0 2,048 Sep-04-2020, 07:55 PM
Last Post: LearningLittlebyLittle
  [Tkinter] Indentation for Tkinter-made Text Editor ShakeyPakey 4 5,140 Jun-08-2020, 03:13 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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