(help)Tkinter, how to make labels scrolling?

Discussion in 'Python' started by Ren Wenshan, Dec 28, 2009.

  1. Ren Wenshan

    Ren Wenshan Guest

    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
    Ren Wenshan, Dec 28, 2009
    #1
    1. Advertising

  2. 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 = 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)
    Francesco Bochicchio, Dec 28, 2009
    #2
    1. Advertising

  3. Ren Wenshan

    Ren Wenshan Guest

    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
    Ren Wenshan, Dec 29, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Psymaster
    Replies:
    0
    Views:
    379
    Psymaster
    Jul 19, 2003
  2. Bob Greschke

    tkinter test field, scrolling, threads

    Bob Greschke, Feb 27, 2004, in forum: Python
    Replies:
    1
    Views:
    422
    Eric Brunel
    Mar 1, 2004
  3. uri bushey

    Tkinter and PMW - dynamic labels

    uri bushey, Jul 29, 2004, in forum: Python
    Replies:
    2
    Views:
    609
    uri bushey
    Jul 29, 2004
  4. Replies:
    1
    Views:
    417
    Eugene Druker
    Jun 14, 2005
  5. headspin

    simple TkInter question - labels

    headspin, Dec 13, 2005, in forum: Python
    Replies:
    1
    Views:
    287
    James Stroud
    Dec 13, 2005
Loading...

Share This Page