0

I started develeping a simple app with Tkinter module of python. My codes are not very complex but when I click the button on the screen, Pycharm freezes. Here is my code below,

from random import *
from tkinter import *

window=Tk()

luckynumber=randint(0,50)

def GuessGame():
guessedNumber=int(guessdigit.get())
while True:
    if guessedNumber == luckynumber:
        cx2=Label(window,text="Congrats!",font=("Fixedsys",20))
        cx2.grid(row=3,column=0)
        break
    elif guessedNumber < luckynumber:
        cx2 =Label(window, text="You have to guess more than that!", font=
("Fixedsys", 20))
        cx2.grid(row=3, column=0)
    elif guessedNumber > luckynumber:
        cx2 =Label(window, text="You have to guess less than that!", font=
("Fixedsys", 20))
        cx2.grid(row=3, column=0)



cx1=Label(window,text="You have to guess the number!",font=("Fixedsys",20))
cx1.grid(row=0,column=0)
guessdigit=Entry(window,font=("Fixedsys",20))
guessdigit.grid(row=1,column=0)
cx3=Button(window,text="To submit your guess, click it!",font=
("Fixedsys",20),command=GuessGame)
cx3.grid(row=2,column=0)

window=mainloop()
Casca
  • 125
  • 7

2 Answers2

1

You are using a while loop within your tkinter code.

When you are using Tkinter, you cannot use any while loops as this basically throws Tkinter off.

Give this post a read, to understand why you shouldn't be using while loops within a Tkinter application.

Also, I presume this is not identical to your actual code as your indentation is off for everything within def GuessGame(): block.

Goralight
  • 2,067
  • 6
  • 25
  • 40
0

you have an endless while loop. you should remove the while loop and quit your program if guessedNumber == luckkynumber. something like this should work:

from random import *
from tkinter import *

window=Tk()

luckynumber=randint(0,50)

def GuessGame():
   guessedNumber=int(guessdigit.get())
   # NO while loop: this function will execute each time the user press the button
   if guessedNumber == luckynumber:
      cx2=Label(window,text="Congrats!",font=("Fixedsys",20))
      cx2.grid(row=3,column=0)
      window.quit() # Quit your window if user guess the number
   elif guessedNumber < luckynumber:
      cx2 =Label(window, text="You have to guess more than that!", font=
("Fixedsys", 20))
      cx2.grid(row=3, column=0)
   elif guessedNumber > luckynumber:
      cx2 =Label(window, text="You have to guess less than that!", font=
("Fixedsys", 20))
      cx2.grid(row=3, column=0)



cx1=Label(window,text="You have to guess the number!",font=("Fixedsys",20))
cx1.grid(row=0,column=0)
guessdigit=Entry(window,font=("Fixedsys",20))
guessdigit.grid(row=1,column=0)
cx3=Button(window,text="To submit your guess, click it!",font=
("Fixedsys",20),command=GuessGame)
cx3.grid(row=2,column=0)

window=mainloop()
Gsk
  • 2,929
  • 5
  • 22
  • 29
  • You should accept the answer if you found and understood what you were looking for – Gsk Dec 21 '17 at 13:06