DEV Community

notsogoodaditya
notsogoodaditya

Posted on

Guess the Number Game | PYTHON GUI (Tkinter)

In order to make a GUI (Graphical User Interface) for our guess the number game in Python

We must import two modules

Python3

import random
from tkinter import *

We must create a window which is easy enough.

Python3

from tkinter import *
import random

main = Tk() #Creating Window named main.
main.title("Guess The Number") #Assigning a Title to our Window
main.geometry("420x320") #Defining Resolution

After running this it would look somethin like this

To make it look interesting we'll add an Image.

Python

from tkinter import *
import random

main = Tk()
main.title("Guess The Number")
main.geometry("420x320")

mark_img = PhotoImage(file="D:\Question Mark.png") #Add the directoru of the image file

canvas = Canvas(main,width=90,height=90)
canvas.place(x=169,y=20,width=90,height=100) #defining postion of the canvas/image as distance from x-axis and x-axis in pixels
canvas.create_image(10,10,anchor=NW,image=mark_img)

After executing the above code it would look somthing like this.

However, if you're having trouble with the resolution of the image.

Use this image: https://media.geeksforgeeks.org/wp-content/uploads/20210224212200/QuestionMark.png

else , We can simply skip this.

We'll now define a function for generating random numbers and list with atmost exactly element.

Python3

guess = [0]

def generator():
guess[0]=random.randint(1,1000) #will give us a random number inclusive of 1 & 1000.
#print(guess[0]) #optional statement to check the program

We'll now create a function named game which willl contain most of our widgets

Python3

from tkinter import *
import random

main = Tk()
main.title("Guess The Number")
main.geometry("420x320")

mark_img = PhotoImage(file="D:\Question Mark.png")

canvas = Canvas(main,width=90,height=90)
canvas.place(x=169,y=20,width=90,height=100) #GeeksforGeeks
canvas.create_image(10,10,anchor=NW,image=mark_img)

guess = [0]

def generator():
guess[0]=random.randint(1,1000)
print(guess[0])

generator()

generating a random number

def game():
guess_text = Label(main,text="Guess the number") #Adding a label/text in our window=main.
guess_text.place(x=160,y=120)

user_guess_gui = Entry(main) 
#Entry widget to take input from user
#here () defines window in which we want our widget to be placed in 
user_guess_gui.place(x=150,y=150)
#placing the widget

def user():
    root = Tk()         #Creating another window named root
    root.title("AGAIN?")
    root.geometry("320x240")
    #root.configure(bg='color') #Using the Following code we can change the bg color of window

    user_guess = int(user_guess_gui.get())   #get() function to get the user input

    if user_guess==guess[0]:  #Comparing user guess with the generated number
        again = Label(root,text="\tYOU WON!!!!!",fg='light green')
        #NOTE this Label widget is created in the root window
        again.place(x=90,y=45)

        again = Label(root,text="Would you like to play again?")
        again.place(x=90,y=85)

        def greeting():
            greet = Label(root,text="Thank You for Playing.")
            greet.place(x=100,y=160)
            root.withdraw()
            main.withdraw()
            #withdraw() to close the window is user does not wish to continue



        def yes():
            generator()
            yes_but = Button(root,text="YES?",command=game)
            #Creating button to take user input in the Yes/No format
            #command funtion to define the fuction which is to be executed by the button
            yes_but.place(x=90,y=120)

        def no():
            no_but = Button(root,text="NO?",command=greeting)
            #NOTE greeting function will close both the windows
            no_but.place(x=200,y=120)

        yes()
        no()

    elif user_guess<guess[0]:
        greet = Label(root,text="Too Low.Try again")
        greet.place(x=100,y=100)      #GFG
        game()

    elif user_guess>guess[0]:
        greet = Label(root,text="Too High.Try again")
        greet.place(x=100,y=100)
        game()


submit = Button(main,text="CHECK",command=user)
#Submit button to submit the user input
#to get the user input we have used the get() function
submit.place(x=180,y=175)
Enter fullscreen mode Exit fullscreen mode

game()
main.mainloop()

The following program will give the output somewhat similar to this.

Thank You.

Top comments (0)