Hi,

The same code as my previous post, but because of this list thing, i'm not able to run specific commands for different options. Here i have 2 options under the 'Names' Menu, 'rajat','prasun', now when the user selects either of them i should be able to know which one he selected and run the command associated for it. i've tried some stuff like trying to use 'variable' or 'value' properties but its just not working.. any hints would be helpful..

from Tkinter import *

fileObj = open("c:/test","w")
fileObj.write("rajat")
fileObj.write("\n")
fileObj.write("prasun")
fileObj.close()

file1 = open("c:/test","r")
lines = file1.readlines()
file1.close()

root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar)
menubar.add_cascade(label="Names",menu=filemenu)

for line in lines:
    filemenu.add_cascade(label=line)

root.config(menu=menubar)
root.mainloop()

A Google for "Tkinter menu" will yield numerous examples. New Mexico Tech http://infohost.nmt.edu/tcc/help/pubs/tkinter/
and Fredrik Lundh http://hem1.passagen.se/eff/
have good Tkinter sites. The following code came from "Charming Python", also a good site. http://www.ibm.com/developerworks/linux/library/l-tkprg/#h5

def help_menu():
    help_btn = Tkinter.Menubutton(menu_frame, text='Help', underline=0)
    help_btn.pack(side=Tkinter.LEFT, padx="2m")
    help_btn.menu = Tkinter.Menu(help_btn)
    help_btn.menu.add_command(label="How To", underline=0, command=HowTo)
    help_btn.menu.add_command(label="About", underline=0, command=About)
    help_btn['menu'] = help_btn.menu
    return help_btn

hi..
I havn't checked the links yet but this example is not what i want, Here we are calling a separate command for each menu option, however i have to be able to call the same command for each Menu option but in the function i should be able to find out that from which Menu Option the call has come so that i can do some specific work then. Basically for each Menu Option i will have a file of the same menu name in some locaion. So if i can make out the 'label' property in the function i can simply replace the file name with the 'label' name and thus using just one fn i can accomplish this. i need this because the menu options are extensible also, so i cant dynamically keep adding more fns for new menu's. hence i need to fire just one command for each option

Ok so i found that out ... we need use the 'variable' and 'value' properties of the radiobutton

something like:

self.filemenu.insert_radiobutton(label=topic.strip(),index=self.indexVal,variable=self.v,value=topic.strip(),
                                             command=self.ShowTopicContent)

now when the user clicks on any menu i can check for the 'variable' value. self.v is a string

print self.v.get()

This gives me the 'value' property and i can identify which option was selected.

Keeping this still open, incase someone has a better way of doing it..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.