Posts: 163
Threads: 13
Joined: Oct 2016
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?
Posts: 3,458
Threads: 101
Joined: Sep 2016
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()
Posts: 5,151
Threads: 396
Joined: Sep 2016
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:
Posts: 163
Threads: 13
Joined: Oct 2016
@ 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.
Posts: 5,151
Threads: 396
Joined: Sep 2016
(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:
Posts: 163
Threads: 13
Joined: Oct 2016
(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.
Posts: 3,458
Threads: 101
Joined: Sep 2016
(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
Posts: 163
Threads: 13
Joined: Oct 2016
(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
Posts: 3,458
Threads: 101
Joined: Sep 2016
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"
|