Guest User

Untitled

a guest
Nov 14th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #test GUI
  2. from tkinter import *
  3. import tkinter as tk
  4. from PIL import Image, ImageTk
  5. from itertools import count
  6. import time
  7. from multiprocessing import Process
  8.  
  9. #create the window
  10. root = Tk()
  11.  
  12. #dice animation
  13. class ImageLabel(tk.Label):
  14. def load(self, im):
  15. if isinstance(im, str):
  16. im = Image.open(im)
  17. self.loc = 0
  18. self.frames = []
  19.  
  20. try:
  21. for i in count(1):
  22. self.frames.append(ImageTk.PhotoImage(im.copy()))
  23. im.seek(i)
  24. except EOFError:
  25. pass
  26.  
  27. try:
  28. self.delay = im.info['duration']
  29. except:
  30. self.delay = 100
  31.  
  32. if len(self.frames) == 1:
  33. self.config(image=self.frames[0])
  34. else:
  35. self.next_frame()
  36.  
  37. def unload(self):
  38. self.config(image=None)
  39. self.frames = None
  40.  
  41. def next_frame(self):
  42. if self.frames:
  43. self.loc += 1
  44. self.loc %= len(self.frames)
  45. self.config(image=self.frames[self.loc])
  46. self.after(self.delay, self.next_frame)
  47.  
  48. #photos
  49. photo_1 = PhotoImage(file="1.png")
  50. photo_2 = PhotoImage(file="2.png")
  51. photo_3 = PhotoImage(file="3.png")
  52. photo_4 = PhotoImage(file="4.png")
  53. photo_5 = PhotoImage(file="5.png")
  54. photo_6 = PhotoImage(file="6.png")
  55. ilabel = ImageLabel(root)
  56.  
  57. #funciton for gif loading
  58. def load_gif ():
  59. ilabel.load('11scaled.gif')
  60.  
  61. #rolling sequence
  62. import random
  63. def dice_roll ():
  64. x = random.randint(1,6)
  65. if x == 1:
  66. label1 = Label(root, image=photo_1)
  67. label1.place(x=115, y=100)
  68. elif x == 2:
  69. label2 = Label(root, image=photo_2)
  70. label2.place(x=115, y=100)
  71. elif x == 3:
  72. label3 = Label(root, image=photo_3)
  73. label3.place(x=115, y=100)
  74. elif x == 4:
  75. label4 = Label(root, image=photo_4)
  76. label4.place(x=115, y=100)
  77. elif x == 5:
  78. label5 = Label(root, image=photo_5)
  79. label5.place(x=115, y=100)
  80. elif x == 6:
  81. label6 = Label(root, image=photo_6)
  82. label6.place(x=115, y=100)
  83.  
  84. #button functions
  85. #multiprocessing?
  86. def button_press ():
  87. count = 0
  88. while count < 1:
  89. load_gif ()
  90. time.sleep (2)
  91. count + 1
  92. else:
  93. dice_roll ()
  94.  
  95. #modify root window
  96. root.geometry("300x300")
  97. root.title("Digital Dice 6000")
  98. thelabel = Label(root, text="Welcome to Digital Dice 6000!")
  99. button_roll = Button(root, text="Roll", fg="red", height="2", width="4", activebackground="gray", command=button_press)
  100.  
  101. thelabel.pack()
  102. ilabel.place(x=100, y=100)
  103. button_roll.place(x=135, y=35)
  104. #kick off the event loop
  105. root.mainloop()
Add Comment
Please, Sign In to add comment