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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to