# This code solves the last problem on Midterm 2, Spring 2008 from Tkinter import * import tkMessageBox count = 0 # How many times have they pushed a button? def update(): "This is called everytime any button is pushed" global count, root count += 1 # Update the count by adding one ans = tkMessageBox.askyesno(message='You have pushed the button %d times. Continue?' %(count)) if not ans: root.destroy() def addButton(parent, mySide): myBtn = Button(parent, text='BTN', command=update) # Add a command to be called everytime a button is pushed myBtn.pack(side=mySide) def layoutWidgets2(parent): ''' Layout the widgets going like this: BTN BTN BTN ''' leftFrame = Frame(parent) # Create the left frame and put it in the parent window rightFrame = Frame(parent) # Create the bottom frame and put it in the parent window addButton(leftFrame, TOP) # Add button into the top frame addButton(leftFrame, TOP) # Add button into the top frame addButton(rightFrame, TOP) # Add button into the bottom frame leftFrame.pack(side=LEFT) # Pack this frame and put the next frame's LEFT side as close to this frame as possible rightFrame.pack(side=LEFT) # Pack this frame and put the next frame's LEFT side as close to this frame as possible def layoutWidgets(parent): ''' Layout the widgets going like this: BTN BTN BTN ''' topFrame = Frame(parent) # Create the top frame and put it in the parent window bottomFrame = Frame(parent) # Create the bottom frame and put it in the parent window addButton(topFrame, LEFT) # Add button into the top frame addButton(topFrame, LEFT) # Add button into the top frame addButton(bottomFrame, TOP) # Add button into the bottom frame topFrame.pack(side=TOP) # Pack this frame and put the next frame's TOP side as close to this frame as possible bottomFrame.pack(side=TOP) # Pack this frame and put the next frame's TOP side as close to this frame as possible # THIS DOES NOT WORK!!!! def layoutWidgetsBad(parent): addButton(parent, TOP) addButton(parent, RIGHT) addButton(parent, BOTTOM) def main(): global root root = Tk() # Create the root window layoutWidgets(root) #layoutWidgets2(root) #layoutWidgetsBad(root) root.mainloop() main()