Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. from tkinter import *
  2.  
  3. #----------------------------------------------------------------------
  4.  
  5. class MainWindow():
  6.  
  7.     #----------------
  8.  
  9.     def __init__(self, main):
  10.  
  11.         # canvas for image
  12.         self.canvas = Canvas(main, width=60, height=60)
  13.         self.canvas.grid(row=0, column=0)
  14.  
  15.         # images
  16.         self.my_images = []
  17.         self.my_images.append(PhotoImage(file = "1.png"))
  18.         self.my_images.append(PhotoImage(file = "2.png"))
  19.         self.my_images.append(PhotoImage(file = "3.png"))
  20.         self.my_images.append(PhotoImage(file = "4.png"))
  21.         self.my_images.append(PhotoImage(file = "5.png"))
  22.         self.my_images.append(PhotoImage(file = "6.png"))
  23.         self.my_images.append(PhotoImage(file = "7.png"))
  24.         self.my_images.append(PhotoImage(file = "8.png"))
  25.         self.my_images.append(PhotoImage(file = "9.png"))
  26.         self.my_images.append(PhotoImage(file = "10.png"))
  27.         self.my_images.append(PhotoImage(file = "11.png"))
  28.         self.my_image_number = 0
  29.  
  30.         # set first image on canvas
  31.         self.image_on_canvas = self.canvas.create_image(0, 0, anchor = NW, image = self.my_images[self.my_image_number])
  32.  
  33.         # button to change image
  34.         self.button = Button(main, text="Change", command=self.onButton)
  35.         self.button.grid(row=1, column=0)
  36.  
  37.     #----------------
  38.  
  39.     def onButton(self):
  40.  
  41.         # next image
  42.         self.my_image_number += 1
  43.  
  44.         # return to first image
  45.         if self.my_image_number == len(self.my_images):
  46.             self.my_image_number = 0
  47.  
  48.         # change image
  49.         self.canvas.itemconfig(self.image_on_canvas, image = self.my_images[self.my_image_number])
  50.  
  51. #----------------------------------------------------------------------
  52.  
  53. root = Tk()
  54. MainWindow(root)
  55. root.geometry("300x300")
  56. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement