Package butler :: Module butlerGUI
[hide private]
[frames] | no frames]

Source Code for Module butler.butlerGUI

  1  ''' 
  2  Created on Jul 20, 2011 
  3   
  4  @author: santtu:: 
  5   
  6    ////////////////NOTE TO STUDENTS /////////////////////// 
  7    // You don't have to understand the source code of this  
  8    // class when this project is first encountered. 
  9    ///////////////////////////////////////////////////////// 
 10   
 11  ''' 
 12   
 13  from Tkinter import * 
 14  import tkSimpleDialog 
 15  from user import User 
 16  from wine import Wine 
 17   
 18   
 19   
20 -class ButlerGUI(object):
21 ''' 22 This class contains the user interface for the Butler 23 wine diary program, which allows a single user to add 24 evaluations of wines into the application window. 25 26 Please note that multiple users and persistent storage of 27 data into files are not supported by this version of 28 the program. 29 ''' 30 31 32
33 - def __init__(self):
34 ''' 35 Creates a new Butler user interface (i.e. a window and its contents) 36 and makes it visible onscreen. 37 ''' 38 #Create frame and set frame attributes 39 self.window = Tk() 40 self.window.wm_title("Butler Wine Diary") 41 self.window.geometry('640x400+0+0') 42 # Initialize data model (i.e., the user): 43 self.user = User() 44 # Initialize and place window contents 45 self.name_labels = [] 46 label = Label(self.window, text=' Wine') 47 label.grid(row=0, column=0) 48 label = Label(self.window, text='Price') 49 label.grid(row=0, column=1) 50 label = Label(self.window, text='Rating') 51 label.grid(row=0, column=2) 52 label = Label(self.window, text='Value for money') 53 label.grid(row=0, column=3) 54 add_wine_button = Button(self.window, text='Add wine', command=self.read_wine) 55 add_wine_button.grid(row=0, column=4) 56 57 # Create menu 58 menubar = Menu(self.window) 59 filemenu = Menu(menubar, tearoff=0) 60 filemenu.add_command(label="Quit", command=self.window.quit) 61 menubar.add_cascade(label="Program", menu=filemenu) 62 63 # display the menu 64 self.window.config(menu=menubar) 65 66 # Make the whole thing visible onscreen. 67 self.window.mainloop()
68 69 70 71
72 - def read_wine(self):
73 ''' 74 An event handler method: reacts to the pressing of 75 buttons by the user. In this case, if the 76 "Add wine" button is pressed, the user is prompted 77 for the data of the new wine diary entry, and the 78 new wine is added for the user. 79 ''' 80 name = tkSimpleDialog.askstring('Wine name', 'Enter wine name:', parent=self.window) 81 description = tkSimpleDialog.askstring('Description', 'Enter wine description:', parent=self.window) 82 prize = tkSimpleDialog.askfloat('Wine price', 'Enter wine price in euros:', parent=self.window, minvalue=0) 83 rating = tkSimpleDialog.askinteger('Wine rating', 'Enter wine rating:', parent=self.window, minvalue=0) 84 new_wine = Wine(name, description, prize, rating) 85 self.add_new_wine(new_wine)
86 87 88
89 - def add_new_wine(self, new_wine):
90 ''' 91 Adds a new wine evaluation to the user's wine diary 92 and updates the GUI accordingly. 93 94 @param new_wine: new wine object to add 95 ''' 96 # Update user by adding wine. 97 self.user.add_wine(new_wine) 98 99 # Find component row in which to place the labels for the new wine. 100 wines = self.user.get_tasted_wines() 101 new_row = len(wines) 102 103 # Create and place new labels for the wine data 104 new_name_label = Label(self.window, text=new_wine.get_name()) 105 106 self.name_labels.append(new_name_label) 107 new_name_label.grid(row=new_row, column=0) 108 new_name_label = Label(self.window, text=new_wine.get_prize()) 109 new_name_label.grid(row=new_row, column=1) 110 new_name_label = Label(self.window, text=new_wine.get_rating()) 111 new_name_label.grid(row=new_row, column=2) 112 new_name_label = Label(self.window, text=new_wine.get_value_for_money()) 113 new_name_label.grid(row=new_row, column=3) 114 self.mark_favorite_wine() 115 self.window.update()
116 117
118 - def mark_favorite_wine(self):
119 #A couple of picture constants that are used in the GUI: 120 STAR_BOTTLE_ICON = PhotoImage(file="bottle_face.gif", width=20, height=20) 121 BOTTLE_ICON = PhotoImage(file="bottle.gif", width=20, height=20) 122 123 wines = self.user.get_tasted_wines() 124 # Loop through all the wine name labels and attach the STAR_BOTTLE_ICON 125 # to the one wine that is the user's current favorite. 126 for wine_number in range(0, len(self.name_labels)): # stepper 127 wine = wines[wine_number] 128 if wine == self.user.get_favorite(): 129 name_label = Label(self.window, text=self.name_labels[wine_number]['text'],image=STAR_BOTTLE_ICON, compound=LEFT) 130 name_label.photo = STAR_BOTTLE_ICON 131 else: 132 name_label = Label(self.window, text=self.name_labels[wine_number]['text'],image=BOTTLE_ICON, compound=LEFT) 133 name_label.photo = BOTTLE_ICON 134 name_label.grid(row=wine_number+1, column=0)
135 136 137 138 if __name__ == '__main__': 139 # Starts up the Butler wine diary user interface. 140 ButlerGUI() 141