# ========================================================= # File: gui_menu.py # 2015-04-27 by A. Ortiz, ITESM CEM. # # This program demonstrates how to write a Tkinter # GUI-based application with a menu that contains several # options for the user. Requires Python 3. # ========================================================= from tkinter import * from tkinter.messagebox import * #---------------------------------------------------------- def average(x): """Calculate the arithmetic mean of the list of numbers contained in a. """ return sum(x) / len(x) #---------------------------------------------------------- def fact(n): """Calculate the factorial of n. n! = (1)(2)(3)...(n - 1)(n) """ r = 1 for i in range(1, n + 1): r *= i return r #---------------------------------------------------------- def main(): """Main program. Controls all the GUI elements. """ #------------------------------------------------------ def about(): showinfo('About', 'GUI Example Application 1.0\n' '© 2015 by Ariel Ortiz') #------------------------------------------------------ def confirm_quit(): response = askyesno('Quit', 'Are you sure you want to quit?') if response: root.destroy() #------------------------------------------------------ def dialog_average(): #-------------------------------------------------- def calc_average(): data_string = data.get('1.0', END).strip() data_list = data_string.split() if len(data_list) == 0: showerror('You made a boo-boo!', 'Please specify at least one number.') return nums = [] for e in data_list: if not e.isdigit(): showerror('You made a boo-boo!', 'Please input only digits.') return else: nums.append(int(e)) dialog.destroy() r = average(nums) showinfo('Result', 'The average is {0:.2f}'.format(r)) #-------------------------------------------------- dialog = Toplevel(root) dialog.transient(root) dialog.grab_set() dialog.title('Average') text = Label(dialog, text='Input numbers:\n' '(use spaces or \n' 'to separate values)') text.grid(row=0, column=0, padx=10, pady=10) data = Text(dialog, height=5, width=10) data.grid(row=0, column=1, padx=10, pady=10) cont = Button(dialog, text='Continue', command=calc_average) cont.grid(row=1, column=1, padx=10, pady=10) #------------------------------------------------------ def dialog_factorial(): #-------------------------------------------------- def calc_factorial(): data_string = data.get('1.0', END).strip() if not data_string.isdigit(): showerror('You made a boo-boo!', 'Please input only digits.') return n = int(data_string) dialog.destroy() r = fact(n) showinfo('Result', 'The factorial of {0} is {1}'.format(n, r)) #-------------------------------------------------- def enter_calc_factorial(event): calc_factorial() #-------------------------------------------------- dialog = Toplevel(root) dialog.transient(root) dialog.grab_set() dialog.title('Factorial') text = Label(dialog, text='Input number:') text.grid(row=0, column=0, padx=10, pady=10) data = Text(dialog, height=1, width=10) data.grid(row=0, column=1, padx=10, pady=10) data.bind('', enter_calc_factorial) cont = Button(dialog, text='Continue', command=calc_factorial) cont.grid(row=1, column=1, padx=10, pady=10) #------------------------------------------------------ root = Tk() root.minsize(width=300, height=200) root.title('Example') app_menu = Menu(root) root.config(menu=app_menu) compute_menu = Menu(app_menu) app_menu.add_cascade(label='Compute', menu=compute_menu) compute_menu.add_command(label='Average...', command=dialog_average) compute_menu.add_command(label='Factorial...', command=dialog_factorial) compute_menu.add_separator() compute_menu.add_command(label='Quit', command=confirm_quit) help_menu = Menu(app_menu) app_menu.add_cascade(label='Help', menu=help_menu) help_menu.add_command(label='About...', command=about) root.mainloop() main()