<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: notsogoodaditya</title>
    <description>The latest articles on DEV Community by notsogoodaditya (@notsogoodaditya).</description>
    <link>https://dev.to/notsogoodaditya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F600471%2F99e9f316-1fb4-4ecf-a8cc-f4ffaa9478aa.png</url>
      <title>DEV Community: notsogoodaditya</title>
      <link>https://dev.to/notsogoodaditya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/notsogoodaditya"/>
    <language>en</language>
    <item>
      <title>Guess the Number Game | PYTHON GUI (Tkinter)</title>
      <dc:creator>notsogoodaditya</dc:creator>
      <pubDate>Sat, 20 Mar 2021 10:54:04 +0000</pubDate>
      <link>https://dev.to/notsogoodaditya/guess-the-number-game-python-gui-tkinter-48cn</link>
      <guid>https://dev.to/notsogoodaditya/guess-the-number-game-python-gui-tkinter-48cn</guid>
      <description>&lt;p&gt;In order to make a GUI (Graphical User Interface) for our guess the number game in Python&lt;/p&gt;

&lt;p&gt;We must import two modules&lt;/p&gt;

&lt;p&gt;Python3&lt;/p&gt;

&lt;p&gt;import random&lt;br&gt;
from tkinter import *&lt;/p&gt;

&lt;p&gt;We must create a window which is easy enough.&lt;/p&gt;

&lt;p&gt;Python3&lt;/p&gt;

&lt;p&gt;from tkinter import *&lt;br&gt;
import random&lt;/p&gt;

&lt;p&gt;main = Tk()      #Creating Window named main.&lt;br&gt;
main.title("Guess The Number")   #Assigning a Title to our Window&lt;br&gt;
main.geometry("420x320")           #Defining Resolution&lt;/p&gt;

&lt;p&gt;After running this it would look somethin like this&lt;/p&gt;

&lt;p&gt;To make it look interesting we'll add an Image.&lt;/p&gt;

&lt;p&gt;Python&lt;/p&gt;

&lt;p&gt;from tkinter import *&lt;br&gt;
import random&lt;/p&gt;

&lt;p&gt;main = Tk()&lt;br&gt;
main.title("Guess The Number")&lt;br&gt;
main.geometry("420x320")&lt;/p&gt;

&lt;p&gt;mark_img = PhotoImage(file="D:\Question Mark.png")  #Add the directoru of the image file&lt;/p&gt;

&lt;p&gt;canvas = Canvas(main,width=90,height=90)&lt;br&gt;
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&lt;br&gt;
canvas.create_image(10,10,anchor=NW,image=mark_img)&lt;/p&gt;

&lt;p&gt;After executing the above code it would look somthing like this.&lt;/p&gt;

&lt;p&gt;However, if you're having trouble with the resolution of the image.&lt;/p&gt;

&lt;p&gt;Use this image: &lt;a href="https://media.geeksforgeeks.org/wp-content/uploads/20210224212200/QuestionMark.png"&gt;https://media.geeksforgeeks.org/wp-content/uploads/20210224212200/QuestionMark.png&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;else , We can simply skip this.&lt;/p&gt;

&lt;p&gt;We'll now define a function for generating random numbers and list with atmost exactly element.&lt;/p&gt;

&lt;p&gt;Python3&lt;/p&gt;

&lt;p&gt;guess = [0]&lt;/p&gt;

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

&lt;p&gt;We'll now create a function named game which willl contain most of our widgets&lt;/p&gt;

&lt;p&gt;Python3&lt;/p&gt;

&lt;p&gt;from tkinter import *&lt;br&gt;
import random&lt;/p&gt;

&lt;p&gt;main = Tk()&lt;br&gt;
main.title("Guess The Number")&lt;br&gt;
main.geometry("420x320")&lt;/p&gt;

&lt;p&gt;mark_img = PhotoImage(file="D:\Question Mark.png")&lt;/p&gt;

&lt;p&gt;canvas = Canvas(main,width=90,height=90)&lt;br&gt;
canvas.place(x=169,y=20,width=90,height=100)        #GeeksforGeeks&lt;br&gt;
canvas.create_image(10,10,anchor=NW,image=mark_img)&lt;/p&gt;

&lt;p&gt;guess = [0]&lt;/p&gt;

&lt;p&gt;def generator():&lt;br&gt;
    guess[0]=random.randint(1,1000)&lt;br&gt;
    print(guess[0])&lt;/p&gt;

&lt;p&gt;generator()&lt;/p&gt;

&lt;h1&gt;
  
  
  generating a random number
&lt;/h1&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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&amp;lt;guess[0]:
        greet = Label(root,text="Too Low.Try again")
        greet.place(x=100,y=100)      #GFG
        game()

    elif user_guess&amp;gt;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)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;game()&lt;br&gt;
main.mainloop() &lt;/p&gt;

&lt;p&gt;The following program will give the output somewhat similar to this.&lt;/p&gt;

&lt;p&gt;Thank You.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
