Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

number guessing game using tkinter python

import tkinter as tk
import random
 
# Create a new window
window = tk.Tk()
 
# Set the dimensions of the created window
window.geometry("600x400")
 
# Set the background color of the window
window.config(bg="#065569")
 
window.resizable(width=False,height=False)
 
# Set Window Title
window.title('Number Guessing Game')
 
# The code for the buttons and text and other 
# interactive UI elements go here 
 
TARGET = random.randint(0, 1000)
RETRIES = 0
 
 
def upate_result(text):
    result.configure(text=text)
 
# Create a new game
def new_game():
    guess_button.config(state='normal')
    global TARGET, RETRIES
    TARGET = random.randint(0, 1000)
    RETRIES = 0
    update_result(text="Guess a number between
 1 and 1000")
 
# Continue the ongoing game or end it
def play_game():
    global RETRIES
 
    choice = int(number_form.get())
     
    if choice != TARGET:
        RETRIES += 1
     
        result = "Wrong Guess!! Try Again"
        if TARGET < choice:
            hint = "The required number lies between 0 and {}".format(result)
        else:
            hint = "The required number lies between {} and 1000".format(choice)
        result += "

HINT :
" + hint
     
    else:
        result = "You guessed the correct number after {} retries".format(RETRIES)
        guess_button.configure(state='disabled')
        result += "
" + "Click on Play to start a new game"
     
    update_result(result)
 
# Heading of our game
title = tk.Label(window,text="Guessing Game",font=("Arial",24),fg="#fffcbd",bg="#065569")
 
# Result and hints of our game
result = tk.Label(window, text="Click on Play to start a new game", font=("Arial", 12, "normal", "italic"),fg = "White", bg="#065569", justify=tk.LEFT)
 
# Play Button
play_button = tk.Button(window, text="Play Game", font=("Arial", 14, "bold"), fg = "Black", bg="#29c70a", command=new_game)
 
# Guess Button
guess_button = tk.Button(window,text="Guess",font=("Arial",13), state='disabled', fg="#13d675",bg="Black", command=play_game)
 
# Exit Button
exit_button = tk.Button(window,text="Exit Game",font=("Arial",14), fg="White", bg="#b82741", command=exit)
 
 
# Entry Fields
guessed_number = tk.StringVar()
number_form = tk.Entry(window,font=("Arial",11),textvariable=guessed_number)
 
 
# Place the labels
 
title.place(x=170, y=50)
result.place(x=180, y=210)
 
# Place the buttons
exit_button.place(x=300,y=320)
guess_button.place(x=350, y=147) 
play_button.place(x=170, y=320)
 
# Place the entry field
number_form.place(x=180, y=150)
 
# Start the window
window.mainloop()
Comment

PREVIOUS NEXT
Code Example
Python :: python identation 
Python :: python inline assignment 
Python :: how to fetch reverse foreign key on model object django 
Python :: function with parameters python 
Python :: google popup not opening 
Python :: python stop running instances 
Python :: permutation test python 
Python :: mysql insert into python many 
Python :: ENCAPSUALTION 
Python :: Créer un décorateur python 
Python :: Print feature importance per feature 
Python :: Python Tkinter Frame Widget Syntax 
Python :: Using pushbullet to export whatsapp chat 
Python :: python equivalent linkedhashmap 
Python :: how many three-letter words with or without meaning can be formed using the letters of the word "python"? 
Python :: Create An Empty List(Array) In Python 
Python :: gensim loop through models 
Python :: python error bars 
Python :: sklearn list parameters 
Python :: generate a hash/secret python 
Python :: find element by partial link text selenium python 
Python :: commend in .env 
Python :: Using iterable unpacking operator * with extend 
Python :: remove from list python by index 
Python :: when i press tab it shows ipynb_checkpoints/ in jupyter notebook 
Python :: reassign variable python 
Python :: The module in NAME could not be imported: django.contrib.user_auth.password_validation.UserAttributeSimilarityValidator. Check your AUTH_PASSWORD_VALI 
Python :: django template child data in nested loop 
Python :: gcp jupyter use python variables in magic bigquery 
Python :: Show output of views in html using ajax, django 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =