1+ import tkinter
2+ from tkinter import *
3+ from PIL import ImageTk ,Image
4+ import random
5+
6+
7+
8+ guess = str ()
9+
10+ wrong = 0
11+
12+ wordlist = ["dog" ,"cat" ,"bookkeeper" ,"ant" ,"phone" ,"laptop" ,"trainers" ,"bottle" ,"clock" ,"speaker" ,"keyboard" ,"fortnight" ]
13+
14+ word = wordlist [random .randint (0 ,len (wordlist )- 1 )]
15+ displayword = "_ " * len (word )
16+ print (word )
17+
18+
19+ def check_guess ():
20+ global img ,wrong ,title ,display ,displayword ,word
21+ if (wrong <= 6 - 1 ):
22+ if (amount .get () in word ):
23+ while amount .get () in word :
24+ x = list (displayword )
25+ x [int (word .index (amount .get ()))* 2 ] = amount .get ()
26+ displayword = "" .join (x )
27+
28+ x = list (word )
29+ x [word .index (amount .get ())] = "0"
30+ word = "" .join (x )
31+
32+ display .configure (text = displayword )
33+ if ("_" not in displayword ):
34+ title .configure ( text = 'You Won' )
35+ else :
36+ wrong += 1
37+ img = ImageTk .PhotoImage (Image .open ("{}.png" .format (wrong )))
38+ canvas .create_image (20 , 20 , anchor = NW , image = img )
39+
40+ else :
41+ display .configure ( text = 'You lose' )
42+ title .configure ( text = 'You Killed Him! :(' )
43+
44+ root = Tk ()
45+ root .title ("Hangman" )
46+ root .minsize (width = 450 , height = 450 )
47+ root .attributes ("-alpha" , 0.90 )
48+ root .configure (background = "#282828" )
49+
50+
51+ title = Label (root , text = 'Guess The Letter' , fg = "red" ,font = "Geneva 30" ,bg = "#282828" )
52+ title .pack ()
53+
54+ display = Label (root , text = displayword , fg = "white" ,font = "Geneva 40" ,bg = "#282828" )
55+ display .pack ()
56+
57+ canvas = Canvas (root , width = 250 , height = 250 ,background = "#282828" ,bd = 0 , highlightthickness = 0 )
58+ canvas .place (relx = 0.37 , rely = 0.5 , anchor = CENTER )
59+
60+ img = ImageTk .PhotoImage (Image .open ("{}.png" .format (wrong )))
61+ canvas .create_image (20 , 20 , anchor = NW , image = img )
62+
63+
64+ amount = Entry (root ,textvariable = guess , fg = "white" , font = "Geneva 25 bold" ,bg = "#282828" ,justify = CENTER )
65+ amount .place (relx = 0.44 , rely = 0.9 , anchor = CENTER )
66+
67+ enter = Button (root ,text = "Guess" , fg = "#282828" , font = "Geneva 30 bold" ,bg = '#54FA9B' ,activebackground = '#282828' ,justify = CENTER ,command = check_guess )
68+ enter .configure (bg = '#54FA9B' )
69+ enter .place (relx = 0.85 , rely = 0.9 , anchor = CENTER )
70+
71+
72+ root .mainloop ()
0 commit comments