Python/Tkinter: Why is my cascade menu not working? -
i'm trying learn tkinter though right i'm stuck @ 1 part. i'd have cascade menu added main menu. problem don't know i'm doing wrong. help.
from tkinter import * import tkfiledialog class application(frame): def __init__(self, master=none): frame.__init__(self, master) self.master = master self.create_wgts() def create_wgts(self): self.mainmenu=menu(self.master) self.wgt_confirm = button(text='confirm', command=self.traceback).pack(side=right) self.wgt_entry = entry() self.wgt_entry.pack(side=right, fill=x, expand=1, ipadx=200) self.wgt_entry.bind('<key-return>', self.hit_enter) self.casmenu_file = menu(self.mainmenu) self.casmenu_file.add_command(label="open file", command=self.file_select) self.casmenu_file.add_command(label="write file", command=self.file_write) self.casmenu_file.add_separator() self.casmenu_file.add_command(label="exit", command=self.traceback) self.mainmenu.add_cascade(label='file', menu=self.casmenu_file) def traceback(self): print "traceback called" def hit_enter(self, event): print "hit enter" def file_select(self): print "file>open called" def file_write(self): print "file>write file called" root = tk() app = application(master=root) app.mainloop()
you forgot add: self.master.config(menu=self.mainmenu)
here's full code listing:
from tkinter import * import tkfiledialog class application(frame): def __init__(self, master=none): frame.__init__(self, master) self.master = master self.create_wgts() def create_wgts(self): self.mainmenu=menu(self.master) self.wgt_confirm = button(text='confirm', command=self.traceback).pack(side=right) self.wgt_entry = entry() self.wgt_entry.pack(side=right, fill=x, expand=1, ipadx=200) self.wgt_entry.bind('<key-return>', self.hit_enter) self.casmenu_file = menu(self.mainmenu) self.casmenu_file.add_command(label="open file", command=self.file_select) self.casmenu_file.add_command(label="write file", command=self.file_write) self.casmenu_file.add_separator() self.casmenu_file.add_command(label="exit", command=self.traceback) self.mainmenu.add_cascade(label='file', menu=self.casmenu_file) self.master.config(menu=self.mainmenu) def traceback(self): print "traceback called" def hit_enter(self, event): print "hit enter" def file_select(self): print "file>open called" def file_write(self): print "file>write file called" root = tk() app = application(master=root) app.mainloop()
Comments
Post a Comment