[Tutor] Changing menu "title" using Tkinter

Kent Johnson kent_johnson at skillsoft.com
Mon Oct 4 14:05:57 CEST 2004


This is similar to the previous question, but you have to call entryconfig 
on the top menu. Here is a revised makemenu(). Note the top menu now needs 
to be set with tearoff=0. (I don't understand this - without tearoff=0 the 
entryconfig() call gives the error
TclError: unknown option "-label")

def makemenu(self):
    top = Menu(self, tearoff=0)
    self.config(menu=top)

    def changeLang():
        if v.get() == 'fr':
            fileMenu.entryconfig(0, label='Ouvrir')
            fileMenu.entryconfig(1, label='Quitter')
            top.entryconfig(0, label='Fichier')
        else:
            fileMenu.entryconfig(0, label='Open')
            fileMenu.entryconfig(1, label='Quit')
            top.entryconfig(0, label='File')

    def onOpen():
        filename = askopenfilename()
        msg['text'] = filename
    fileMenu = Menu(top, tearoff=0)

    fileMenu.add_command(label='Open', command=onOpen)
    fileMenu.add_command(label='Quit', command=self.quit)

Kent
At 06:21 AM 10/4/2004 -0300, André Roberge wrote:
>First, my apologies for the wrong subject line in my previous message,
>and thank you to Kent Johnson for his useful pointer.
>
>Moving along....
>I would like to change the label 'File' to 'Fichier' in the following example.
>I've tried various options, but with no success.
>Any help would be greatly appreciated.
>
>André Roberge
>
>================
>from Tkinter import *
>from tkFileDialog import askopenfilename
>def makemenu(self):
>    top = Menu(self)
>    self.config(menu=top)
>
>    def changeLang():
>        if v.get() == 'fr':
>            fileMenu.entryconfig(0, label='Ouvrir')
>            fileMenu.entryconfig(1, label='Quitter')
>#  --> here is where I would like to set the label
>        else:
>            fileMenu.entryconfig(0, label='Open')
>            fileMenu.entryconfig(1, label='Quit')
>
>    def onOpen():
>        filename = askopenfilename()
>        msg['text'] = filename
>    fileMenu = Menu(top, tearoff=0)
>    fileMenu.add_command(label='Open', command=onOpen)
>    fileMenu.add_command(label='Quit', command=self.quit)
>
># the following is the label that I would like to change
>    top.add_cascade(label='File', menu=fileMenu)
>
>    otherMenu = Menu(top, tearoff=0)
>
># I would like eventually to change the following label also...
>    top.add_cascade(label='Other', menu=otherMenu)
>
>    LANGUAGES = [
>        ('English', 'en'),
>        ('Francais', 'fr') ]
>    v = StringVar()
>    v.set('en') # initialize
>    for text, language in LANGUAGES:
>        lang = Radiobutton(root, text=text, variable=v, value=language,
>                           command=changeLang)
>        lang.pack(anchor=W)
>
>    msg = Label(root, text='test')
>    msg.pack()
>
>if __name__ == '__main__':
>    root = Tk()
>    makemenu(root)
>    root.mainloop()
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list