import tkinter from tkinter import * from tkinter.filedialog import asksaveasfilename from tkinter.filedialog import askopenfilename from PIL import Image import urllib.request, io import support # define globals and initialize global canvas, image, board, tk_image, root, dialog, window_width, window_height, container canvas = None image = None board = None tk_image = None root = None dialog = None container = None window_width = 825 window_height = 825 def wrap_image_for_tkInter( img ): try: from PIL import ImageTk return ImageTk.PhotoImage(img) except: iname = "_photo-op-temporary.png" img.save(iname) return PhotoImage(file=iname) def url_get_image( link ) : # returns the image at link stream = urllib.request.urlopen( link ) data = stream.read() file = io.BytesIO( data ) image = Image.open(file) image = image.convert('RGB') return image def create_button( container, label, action ) : ''' return a new button stored in container whose contents is label, that when clicked performs action ''' button = tkinter.Button( container , width=7, text=label ) # button is to 50respond to a left click button.bind( '', action ) # get button to arrange itself button.pack( padx=2, pady=5, side=LEFT ) # hand back our new button return button def open_file_click( event ) : ''' open an image ''' global canvas, image, board, tk_image, root, dialog, container filename = askopenfilename() try : img = Image.open( filename ) except : return image = resize( img ) support.initialize_globals( image ) image = support.cleanup( image ) support.initialize_board( image ) board = support.BOARD tk_image = wrap_image_for_tkInter( board ) support.initialize_board( image ) restore_click( None ) def left_click( event ) : global canvas, image, board, tk_image, root, dialog support.slide_left() tk_image = wrap_image_for_tkInter( board ) canvas.config( image=tk_image ) def right_click( event ) : global canvas, image, board, tk_image, root, dialog support.slide_right() tk_image = wrap_image_for_tkInter( board ) canvas.config( image=tk_image ) def up_click( event ) : global canvas, image, board, tk_image, root, dialog support.slide_up() tk_image = wrap_image_for_tkInter( board ) canvas.config( image=tk_image ) def down_click( event ) : global canvas, image, board, tk_image, root, dialog support.slide_down() tk_image = wrap_image_for_tkInter( board ) canvas.config( image=tk_image ) def scramble_click( event ) : global canvas, image, board, tk_image, root, dialog support.scramble_board( image ) support.BLACK_TILE_SPOT = (3, 3) tk_image = wrap_image_for_tkInter( board ) canvas.config( image=tk_image ) def restore_click( event ) : global canvas, image, board, tk_image, root, dialog support.initialize_board( image ) support.BLACK_TILE_SPOT = (3, 3) tk_image = wrap_image_for_tkInter( board ) canvas.config( image=tk_image ) def create_edit_menu( container ) : menubar = tkinter.Menubutton( container, text='Commands' ) menubar.grid() edit_menu = tkinter.Menu( menubar, tearoff=0 ) edit_menu.add_command( label='Left' , command=slide_left ) edit_menu.add_command( label='Right' , command=slide_right ) edit_menu.add_command( label='Up' , command=slide_up ) edit_menu.add_command( label='Down' , command=slide_down ) edit_menu.add_command( label='Scramble' , command=scramble_board ) menubar.pack() return menubar def url_get( link ) : # returns the image at link stream = urllib.request.urlopen( link ) data = stream.read() file = io.BytesIO( data ) image = Image.open(file) image = image.convert('RGB') return image def GUI( name='15 Puzzle', window_width=window_width, window_height=window_height, bg='#000000' ) : global canvas, image, board, tk_image, root, dialog, container # define root window for the application root = Tk() # title the window root.title( name ) # create the contain for the window content pane container = Frame( root, width=window_width, height=window_height, background=bg ) # configure the container container.pack_propagate( 0 ) container.pack() # start off with the mandrill image link = 'http://www.cs.virginia.edu/~cs1112/images/mandrill/full.png' link = 'http://www.cs.virginia.edu/~cs1112/images/tj.png' link = 'http://www.cs.virginia.edu/~cs1112/images/youcandoit.jpg' image = url_get_image( link ) image = resize( image ) support.initialize_globals( image ) image = support.cleanup( image ) support.initialize_board( image ) board = support.BOARD tk_image = wrap_image_for_tkInter( board ) canvas = tkinter.Label(container, image=tk_image ) buttons = LabelFrame( container, text='', labelanchor=N, background="#4682B4" ) # create buttons for selecting photo operation open_file_button = create_button( buttons, 'Open file', open_file_click ) left_button = create_button( buttons, 'Left', left_click ) right_button = create_button( buttons, 'Right', right_click ) up_button = create_button( buttons, 'Up', up_click ) down_button = create_button( buttons, 'Down', down_click ) scramble_button = create_button( buttons, 'Scramble', scramble_click ) restore_button = create_button( buttons, 'Restore', restore_click ) buttons.pack( pady=10 ) # arrange the widgets in the window canvas.pack() # start event handling root.mainloop() def resize( img ) : global window_width, window_height w, h = image.size max_w = window_width - 125 max_h = window_height - 125 scale_w = w / max_w scale_h = h / max_h scale = max( scale_w, scale_h ) if ( scale > 1 ) : w = int( w / scale ) h = int( h / scale ) img = img.resize( ( w, h ) ).convert( 'RGB' ) return img if ( __name__ == '__main__' ) : GUI()