Q:

Python - While Loop changing a variable

I'm currently creating a program that will constantly check for a keypress, and if it's W or S it'll change the speed. But I've got a problem of Tk not responding? Anyone help cheers. My code is:
 import tkinter as tk #Importing the GUI Library
import time #Importing Time#
def onKeyPress(event): #Defining my function of keypress, and using GUI Library to get the keypress.
        time.sleep(1) #Telling it to wait one second otherwise it will crash.
        text.insert('end', 'You pressed %s\n' % (event.char, )) #Telling the user if he pressed the key or not.
        speed = 50 #Setting the speed to 50 default
        while speed > 0:
            if event.char == 'w': #if key pressed = w:
                speed = speed + 1 #Change speed by 1
                time.sleep(1)
            if event.char == 's':
                speed = speed - 1
                time.sleep(1)
                print(speed)

root = tk.Tk()
root.geometry('300x200')
text = tk.Text(root, background='black', foreground='white', font=('Comic Sans MS', 12))
text.pack()
root.bind('<KeyPress>', onKeyPress)
root.mainloop()

A:

Your main issue is that you're entering an infinite loop when you press a letter other than 's'. When you press 's' it will take 50 seconds to exit the loop only to begin again upon the next key press. Additionally your speed variable is only inside the keypress function so you can't access it anyway, you are also resetting it to 50 every time a key is pressed
A couple of key things you need to know about tkinter is that time.sleep should be heavily avoided and trying to have while loops running in the background.
Now there are a couple of options to do this. You can either bind the function to each individual key.
import tkinter as tk

def onKeyPress(event, value):
    global speed # alter the global speed variable inside a function

    # Keep in mind this insert will only occur for the selected keys
    text.insert('end', 'You pressed %s\n' % (event.char, ))
    speed += value # update speed variable with value
    print(speed) # print current speed  

speed = 50

root = tk.Tk()
root.geometry('300x200')
text = tk.Text(root, background='black', foreground='white', font=('Comic Sans MS', 12))
text.pack()

# Individual key bindings
root.bind('<KeyPress-w>', lambda e: onKeyPress(e, 1)) # value is 1 for 'w'
root.bind('<KeyPress-s>', lambda e: onKeyPress(e, -1)) # value is -1 for 's'

root.mainloop()

Or alternative as before you can use if statements to check the letter entered. if elif is used instead of an if for each letter is because although it has the same effect, the if block will terminate when a condition is True otherwise it will go through all the if statements.
import tkinter as tk

def onKeyPress(event):
    global speed # alter the global speed variable inside a function

    text.insert('end', 'You pressed %s\n' % (event.char, ))

    if event.char == 'w':
        speed += 1
    elif event.char == 's':
        speed -= 1
    print(speed) # print current speed

speed = 50

root = tk.Tk()
root.geometry('300x200')
text = tk.Text(root, background='black', foreground='white', font=('Comic Sans MS', 12))
text.pack()
root.bind('<KeyPress>', onKeyPress)

root.mainloop()

Side Note:
Currently when I press a letter it will insert "sYou pressed s" because of the initial keypress. If you add this before the insert line then it will delete the typed character giving "You pressed s" instead.
text.delete("%s-1c" % 'insert', 'insert')

