I'm trying to create an assistant for an old game I play. This will show you the length of time left on a spell youve cast using a progress bar (tied to macros. IE the D macro I have is for a spell 3.7 seconds long as shown below). Im a bit new to programming so I have done this in stages. I have completed the program so that it searches my macros and sends the correct timer on Key event to the function. The problem is however the spells simply queue rather than interrupting when a new macro is pressed again.
I was trying to solve this in a shorter experimental script as I find it easier to do bits as learning and then combine them later. As you see here I tried to run the timer on a separate thread so I can cancel it halfway through (in the complete program this would be when a macro is pressed before its finished).
Going from examples I found on this site I thought this global variable in the thread would work but it still simply finishes running func(x) before cancelling it and printing that it has been halted. Sorry if I havent phrased this well enough - or phrased it too much. My first time posting here
Code below - Thanks in advance!
import time
import sys
from tkinter import *
from tkinter.ttk import *
root = Tk()
progress = Progressbar(root, orient = HORIZONTAL, length = 500, mode = 'determinate')
progress.pack()
# on key press check spells
global casting
def func(x):
global casting
while casting == True and progress['value']<100:
increments = 100/x
progress['value'] += increments
root.update_idletasks()
time.sleep(0.01)
else:
progress['value'] = 0
root.update_idletasks()
return
#def cancel():
# time.sleep(2)
## global casting
# casting = False
# print("Halted casting")
casting = True
t1 = threading.Thread(target=func(370))
t1.start()
time.sleep(0.9)
casting = False
print("Halted")
t1.join
###### New code updated
from tkinter import *
from tkinter.ttk import *
global casting
myMacros = {
"my_keys": {
"d": ["Flamestrike", 370],
"x": ["Poison", 150],
"q": ["Cure", 100],
"a": ["Lightning", 250],
"w": ["Greater Heal", 300],
"z": ["Magic Arrow", 100],
"e": ["Magic Reflection", 350]
}
}
def update_progressbar(x):
if casting is True and progress['value'] < 100:
increments = 100/x
progress['value'] += increments
# schedule this function to run again in 10ms
root.after(10, update_progressbar, x)
else:
progress['value'] = 0
def cancel():
global casting
casting = False
def cast(x):
global casting
casting = True
update_progressbar(x)
def key(event):
# Adding dynamic spellcasts grabbed from dictionary
if(event.char in myMacros["my_keys"]):
cancel()
# print(myMacros["my_keys"][event.char][0])
# print(myMacros["my_keys"][event.char][1])
cast(myMacros["my_keys"][event.char][1])
root = Tk()
progress = Progressbar(root, orient = HORIZONTAL, length = 500, mode = 'determinate')
#start_button = Button(root, text="Cast", command=cast)
#cancel_button = Button(root, text="Cancel", command=cancel)
progress.pack(side="top", fill="x")
#start_button.pack(side="left")
#cancel_button.pack(side="left")
root.bind("<Key>",key)
root.mainloop()