(help)Tkinter, how to make labels scrolling?

R

Ren Wenshan

Hi, everyone:

I am new to programming and Python and these days I've been working
on a
tiny program for practice and encountered with a problem.

My tiny program read a line from a data file one time, and store it
in a list, till the list is full. This is the init.
Then when I press Button Start, I want the program will read a
line,
list.pop(0) list.append(line) in a loop. Thus, make the labels
scrolling.
English is not my mother tongue, I hope I've made myself
understood.

the whole source code:

from Tkinter import *
import sys
import time

# position of buttons
row_button = 5
col_button = 4

# font size
size_title = 20
size_button = 12
size_text = 14

# the length of name_list
Len_List = 3


class meal( Frame ):

def __init__(self):
Frame.__init__(self)
self.pack( expand = YES, fill = BOTH)
self.master.title("Languages")

self.label_1 = Label(self, text = "Too many languages to
choose...", font = ("arial", size_title))
self.label_2 = Label(self, text = "Which is the Lucky one",
font = ("arial", size_title-4))
self.label_1.grid(row = 0, column = 0)
self.label_2.grid(row = 1, column = 2)


self.button_start = Button(self, text = "start", font =
("arial", size_button), command = self.start)
self.button_stop = Button(self, text = "stop", font =
("arial", size_button))
self.button_quit = Button(self, text = "quit", font =
("arial", size_button), command = self.quit)

self.button_start.grid(row = row_button, column = col_button)
self.button_stop.grid(row = row_button, column = col_button+1)
self.button_quit.grid(row = row_button,column = col_button+2)


self.name_list = [None] * Len_List
self.label_list = [None] * Len_List

self.fp = open("data.txt", 'r')
for i in range(Len_List):
self.name_list = self.fp.readline()
for i in range(Len_List):
self.label_list = Label(self, text = self.name_list,
font = ("arial", 12))
self.label_list.grid(row = 2+i, column = 2)

def start(self):
self.line = self.fp.readline()
if not self.line:
self.fp.seek(0)
self.line = self.fp.readline()
self.name_list.pop(0)
self.name_list.append(self.line)

for i in range(Len_List):
self.label_list.destroy()
self.label_list = Label(self, text = self.name_list,
font = ("arial", 12))
self.label_list.grid(row = 2+i, column = 2)

def quit(self):
sys.exit(0)


app = meal()
app.mainloop()






Best wishes

Vincent Ren
 
F

Francesco Bochicchio

Hi, everyone:

   I am new to programming and Python and these days I've been working
on a
tiny program for practice and encountered with a problem.

   My tiny program read a line from a data file one time, and store it
in a list, till the list is full. This is the init.
   Then when I press Button Start, I want the program will read a
line,
list.pop(0) list.append(line) in a loop. Thus, make the labels
scrolling.
   English is not my mother tongue, I hope I've made myself
understood.

   the whole source code:

from Tkinter import *
import sys
import time

# position of buttons
row_button = 5
col_button = 4

# font size
size_title = 20
size_button = 12
size_text = 14

# the length of name_list
Len_List = 3

class meal( Frame ):

    def __init__(self):
        Frame.__init__(self)
        self.pack( expand = YES, fill = BOTH)
        self.master.title("Languages")

        self.label_1 = Label(self, text = "Too many languages to
choose...", font = ("arial", size_title))
        self.label_2 = Label(self, text = "Which is the Lucky one",
font = ("arial", size_title-4))
        self.label_1.grid(row = 0, column = 0)
        self.label_2.grid(row = 1, column = 2)

        self.button_start = Button(self, text = "start", font =
("arial", size_button), command = self.start)
        self.button_stop = Button(self, text = "stop", font =
("arial", size_button))
        self.button_quit = Button(self, text = "quit", font =
("arial", size_button), command = self.quit)

        self.button_start.grid(row = row_button, column = col_button)
        self.button_stop.grid(row = row_button, column = col_button+1)
        self.button_quit.grid(row = row_button,column = col_button+2)

        self.name_list = [None] * Len_List
        self.label_list = [None] * Len_List

        self.fp = open("data.txt", 'r')
        for i in range(Len_List):
            self.name_list = self.fp.readline()
        for i in range(Len_List):
            self.label_list = Label(self, text = self.name_list,
font = ("arial", 12))
            self.label_list.grid(row = 2+i, column = 2)

    def start(self):
        self.line = self.fp.readline()
        if not self.line:
            self.fp.seek(0)
            self.line = self.fp.readline()
        self.name_list.pop(0)
        self.name_list.append(self.line)

        for i in range(Len_List):
            self.label_list.destroy()
            self.label_list = Label(self, text = self.name_list,
font = ("arial", 12))
            self.label_list.grid(row = 2+i, column = 2)

    def quit(self):
        sys.exit(0)

app = meal()
app.mainloop()

Best wishes

Vincent Ren


Hi,

if you want to realize an 'animated scrolling' effect, you need to
move the scrolling code out of the start callback
in a function which is called periodically by the GUI mainloop. In
Tkinter, you can do that using Toplevel.after to
have a fuction be called after a timeout. Here is your 'meal' class
with the modifications needed to make an
'animated scroll'. I renamed your start method as _scroll_text and
wrote new start and stop methods to start and stop
the scrolling.

Ciao
-----
FB


lass meal( Frame ):
SCROLL_DELAY = 500 # milliseconds
def __init__(self):
Frame.__init__(self)
self.pack( expand = YES, fill = BOTH)
self.master.title("Languages")

self.label_1 = Label(self, text = "Too many languages to
choose...", font = ("arial", size_title))
self.label_2 = Label(self, text = "Which is the Lucky one",
font = ("arial", size_title-4))
self.label_1.grid(row = 0, column = 0)
self.label_2.grid(row = 1, column = 2)

self.button_start = Button(self, text = "start", font =
("arial", size_button), command = self.start)
self.button_stop = Button(self, text = "stop", font =
("arial", size_button), command = self.stop )
self.button_quit = Button(self, text = "quit", font =
("arial", size_button), command = self.quit)

self.button_start.grid(row = row_button, column = col_button)
self.button_stop.grid(row = row_button, column = col_button+1)
self.button_quit.grid(row = row_button,column = col_button+2)

self.name_list = [None] * Len_List
self.label_list = [None] * Len_List

self.fp = open("data.txt", 'r')
for i in range(Len_List):
self.name_list = self.fp.readline()
for i in range(Len_List):
self.label_list = Label(self, text = self.name_list,
font = ("arial", 12))
self.label_list.grid(row = 2+i, column = 2)

self.after_id = None

def _scroll_text(self):
#print "_scroll_text"
self.line = self.fp.readline()
if not self.line:
self.fp.seek(0)
self.line = self.fp.readline()
self.name_list.pop(0)
self.name_list.append(self.line)

for i in range(Len_List):
self.label_list.destroy()
self.label_list = Label(self, text = self.name_list,
font = ("arial", 12))
self.label_list.grid(row = 2+i, column = 2)
self.after_id = self.master.after(self.SCROLL_DELAY,
self._scroll_text)

def start(self):
self.after_id = self.master.after(self.SCROLL_DELAY,
self._scroll_text)

def stop(self):
if self.after_id:
self.master.after_cancel(self.after_id)
self.after_id = None

def quit(self):
sys.exit(0)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top