I am trying to create a python program as below. it basically reads a file with bus time table and in the Tkinter, it displays the departure list and arrival bus stops with their corresponding time.
here i am using the following code so that it will update the time for each departure or arrival stops. but when i select the stops it does not call the functions. I do not understand why.
Tkinter.OptionMenu(self.root,self.Departure,*self.BusStops,command=self.update_departure()).pack()
Tkinter.OptionMenu(self.root,self.Arrival,*self.BusStops,command=self.update_arrival()).pac
有人能照亮它吗?
如果它不使用类结构,也同样有效。
I am running on a windows xp 2002 service pack 2 with python 2.6 version import Tkinter import time
class App():
def __init__(self):
self.root = Tkinter.Tk()
self.DEBUG_ENABLE = 1
self.timetable_file_name = "200_timetable.txt"
self.BusStops = list()
self.ArrivalTime = list()
self.update_timetable()
self.Departure = Tkinter.StringVar()
self.Arrival = Tkinter.StringVar()
self.StartTime = Tkinter.StringVar()
self.EndTime = Tkinter.StringVar()
self.label = Tkinter.Label(text="")
self.label.pack()
self.update_clock()
self.Departure.set(self.BusStops[0])
self.Arrival.set(self.BusStops[-1])
self.StartTime.set("hi")
self.EndTime.set("ih")
self.OptMenuDep= Tkinter.OptionMenu(self.root,self.Departure,*self.BusStops,command=self.update_departure()).pack()
self.OptMenuArr= Tkinter.OptionMenu(self.root,self.Arrival,*self.BusStops,command=self.update_arrival()).pack()
self.OptMenuDepTime = Tkinter.OptionMenu(self.root,self.StartTime,"").pack()
self.OptMenuArrTime = Tkinter.OptionMenu(self.root,self.EndTime,"").pack()
self.root.mainloop()
def debug(self,message):
if self.DEBUG_ENABLE:
print "DEBUG MESSAGE : ", message
def update_clock(self):
now = time.strftime("%H:%M:%S")
self.label.configure(text=now)
self.root.after(200, self.update_clock)
def update_timetable(self):
self.file_desc = open(self.timetable_file_name)
for line in self.file_desc.readlines():
self.BusStops.append(line.split( )[0])
self.ArrivalTime.append(line.split( )[2:-1])
self.file_desc.close()
def update_departure(self):
self.debug("entering update departure")
stop_name = self.Departure.get()
count = 0
for stop in self.BusStops:
if (stop == stop_name):
break
else:
count += 1
self.StartTime.set(self.ArrivalTime[count])
count = 0
def update_arrival(self):
self.debug("entering update arrival")
stop_name = self.Arrival.get()
count = 0
for stop in self.BusStops:
if (stop == stop_name):
break
else:
count += 1
self.EndTime.set(self.ArrivalTime[count])
count = 0
# The Main Program Starts Here
app=App()
""""""""""""""""""""""""" the data format is as below for file 200_timetable.txt
NICE-J.C.Bermond车站07:30 07:45 08:00 08:10 08:15 08:30
NICE-J.Médecin/邮政酒店07:32 07:47 08:02 08:12 08:17 08:32
尼斯-格里马尔迪07:33 07:48 08:03 08:13 08:18 08:33
尼斯-里沃利07:34 07:49 08:04 08:14 08:19 08:34
“”“”