I'm new to programming, and I've had some trouble running the code perfectly.
I chose the python language because of its flexibility.
For some time I've been trying to create a macro with a GUI, I use Tkinter, the window is the way I planned it, and the macro commands as well. But when I press the button to turn on the macro, the window hangs and stops responding, I can not click the button again to disable the macro or anything, but I realize the macro continues to run perfectly.
I'm counting on your help to heal this lock in Tkinter's window. Here is my code.
from Tkinter import *
import ctypes
import time
class Janela:
def __init__(self, toplevel):
self.primeira = Frame(toplevel)
self.primeira.pack()
self.segunda = Frame(toplevel, pady=10)
self.segunda.pack()
self.terceira = Frame(toplevel,)
self.terceira.pack()
self.title = Label(self.primeira, text='"Perera" Trainer', font=('Verdana', '13', 'bold'))
self.title['width'] = 20
self.title['height'] = 5
self.title.pack()
self.nome = Label(self.segunda, text='Hotkey da spell: ', font=('Verdana', '12',), width=13).pack(side=LEFT)
self.hotkey = Entry(self.segunda, width=2, font=('Verdana', '12', 'bold'))
self.hotkey.focus_force()
self.hotkey.pack(side=LEFT)
Label(self.terceira, text='Ligar Trainer', font=('Arial', '12', 'bold'), height=3,).pack()
self.botao = Button(self.terceira, font=('Arial', '13', 'bold'), text='ON', bg='gray')
self.botao.bind("<Button-1>", self.ligar)
self.botao.pack()
def ligar(self, event):
if self.botao['bg'] == 'gray':
self.botao['bg'] = 'deepskyblue'
else:
self.botao['bg'] = 'gray'
VAR = self.hotkey.get()
self.hot = int(VAR[1])
while self.botao['bg'] == 'deepskyblue':
time.sleep(2)
if self.hot == 1:
ctypes.windll.user32.keybd_event(0x70, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x70, 0, 0x0002, 0)
elif self.hot == 2:
ctypes.windll.user32.keybd_event(0x71, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x71, 0, 0x0002, 0)
elif self.hot == 3:
ctypes.windll.user32.keybd_event(0x72, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x72, 0, 0x0002, 0)
elif self.hot == 4:
ctypes.windll.user32.keybd_event(0x73, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x73, 0, 0x0002, 0)
elif self.hot == 5:
ctypes.windll.user32.keybd_event(0x74, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x74, 0, 0x0002, 0)
elif self.hot == 6:
ctypes.windll.user32.keybd_event(0x75, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x75, 0, 0x0002, 0)
elif self.hot == 7:
ctypes.windll.user32.keybd_event(0x76, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x76, 0, 0x0002, 0)
elif self.hot == 8:
ctypes.windll.user32.keybd_event(0x77, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x77, 0, 0x0002, 0)
elif self.hot == 9:
ctypes.windll.user32.keybd_event(0x78, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x78, 0, 0x0002, 0)
elif self.hot == 10:
ctypes.windll.user32.keybd_event(0x79, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x79, 0, 0x0002, 0)
elif self.hot == 11:
ctypes.windll.user32.keybd_event(0x7A, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x7A, 0, 0x0002, 0)
elif self.hot == 12:
ctypes.windll.user32.keybd_event(0x7B, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x7B, 0, 0x0002, 0)
time.sleep(2)
ctypes.windll.user32.mouse_event(0x8, 0, 0, 0, 0)
ctypes.windll.user32.mouse_event(0x10, 0, 0, 0, 0)
time.sleep(2)
ctypes.windll.user32.keybd_event(0xA2, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x25, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x25, 0, 0x0002, 0)
ctypes.windll.user32.keybd_event(0xA2, 0, 0x0002, 0)
time.sleep(0.2)
ctypes.windll.user32.keybd_event(0xA2, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x26, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x26, 0, 0x0002, 0)
ctypes.windll.user32.keybd_event(0xA2, 0, 0x0002, 0)
time.sleep(0.2)
ctypes.windll.user32.keybd_event(0xA2, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x27, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x27, 0, 0x0002, 0)
ctypes.windll.user32.keybd_event(0xA2, 0, 0x0002, 0)
time.sleep(0.2)
ctypes.windll.user32.keybd_event(0xA2, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x28, 0, 0, 0)
ctypes.windll.user32.keybd_event(0x28, 0, 0x0002, 0)
ctypes.windll.user32.keybd_event(0xA2, 0, 0x0002, 0)
raiz = Tk()
Janela(raiz)
raiz.mainloop()