FAQ
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[i] = self.fp.readline()
for i in range(Len_List):
self.label_list[i] = Label(self, text = self.name_list[i],
font = ("arial", 12))
self.label_list[i].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[i].destroy()
self.label_list[i] = Label(self, text = self.name_list[i],
font = ("arial", 12))
self.label_list[i].grid(row = 2+i, column = 2)

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


app = meal()
app.mainloop()






Best wishes

Vincent Ren

Search Discussions

  • Francesco Bochicchio at Dec 28, 2009 at 10:38 am

    On 28 Dic, 09:44, Ren Wenshan wrote:
    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[i] = self.fp.readline()
    ? ? ? ? for i in range(Len_List):
    ? ? ? ? ? ? self.label_list[i] = Label(self, text = self.name_list[i],
    font = ("arial", 12))
    ? ? ? ? ? ? self.label_list[i].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[i].destroy()
    ? ? ? ? ? ? self.label_list[i] = Label(self, text = self.name_list[i],
    font = ("arial", 12))
    ? ? ? ? ? ? self.label_list[i].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[i] = self.fp.readline()
    for i in range(Len_List):
    self.label_list[i] = Label(self, text = self.name_list[i],
    font = ("arial", 12))
    self.label_list[i].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[i].destroy()
    self.label_list[i] = Label(self, text = self.name_list[i],
    font = ("arial", 12))
    self.label_list[i].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)
  • Ren Wenshan at Dec 29, 2009 at 1:34 pm
    It works, Thank you very much.
    On Dec 28, 6:38?pm, Francesco Bochicchio wrote:

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 28, '09 at 8:44a
activeDec 29, '09 at 1:34p
posts3
users2
websitepython.org

People

Translate

site design / logo © 2022 Grokbase