Reputation: 11
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
can someone throw light on it?
the same works if it do not use a class structure.
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('\t')[0])
self.ArrivalTime.append(line.split('\t')[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 - Station J.C. Bermond 07:30 07:45 08:00 08:10 08:15 08:30
NICE - J. Médecin / Hôtel des Postes 07:32 07:47 08:02 08:12 08:17 08:32
NICE - Grimaldi 07:33 07:48 08:03 08:13 08:18 08:33
NICE - Rivoli 07:34 07:49 08:04 08:14 08:19 08:34
""""""""""""""""""""""""""""
Upvotes: 1
Views: 4530
Reputation: 385890
When you write command=self.update_departure()
you are saying "execute the command self._update_departure, and use the result of that call as the name of the command". Since self._update_departure()
returns None
, it's the same as doing command=None
In other words, omit the ()
-- you need to pass a reference to the function.
Upvotes: 7