English 中文(简体)
PYTHON:Tkinter.OptionsMenu问题:选项菜单不执行用户选择的命令功能
原标题:PYTHON:Tkinter.OptionMenu Question : option menu does not execute the command function to user selection

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

“”“”

问题回答

当您编写<code>command=self.update_departure()</code>时,您说的是“执行命令self._update_deaving,并使用该调用的结果作为命令的名称”。由于<code>自我_update_departure()返回None,这与执行命令=None

换句话说,省略()——您需要传递对函数的引用。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...