I borrowed some code from another thread I found on this website, but when I tried to form a program around it, I found that one of my functions can only be called once.
import tkinter as tk
def total():
#borrowed code vvv
def keyDetect(event):
root.destroy()
if event.char == event.keysym:
print(event.char)
elif len(event.char) == 1:
print(event.keysym, event.char)
else:
print(event.keysym)
root = tk.Tk()
root.bind_all('<Key>', keyDetect)
root.withdraw()
root.mainloop()
#borrowed code ^^^
total()
print('test message #1')
total()
When this code is run, the definition 'total()' works the first time, but not the second. Why? And how do I fix it?
Thanks in advance!
EDIT:
If you remove the definition completely, the code still doesn't work
import tkinter as tk
def keyDetect(event):
root.destroy()
if event.char == event.keysym:
print(event.char)
elif len(event.char) == 1:
print(event.keysym, event.char)
else:
print(event.keysym)
root = tk.Tk()
root.bind_all('<Key>', keyDetect)
root.withdraw()
root.mainloop()
print('1')
root = tk.Tk()
root.bind_all('<Key>', keyDetect)
print('2')
root.withdraw()
print('3')
root.mainloop()
print('4')
But doing this does show where the problem is; as when this code is run "3" is printed, but "4" isn't.