Sou novato em programação e tenho enfrentado algumas dificuldades para executar o código com perfeição.
Escolhi a linguagem python pela sua flexibilidade.
Há algum tempo venho tentando criar um macro com GUI, uso o Tkinter, a janela está do jeito que eu planejei, e os comandos do macro também. Só que quando eu aperto o botão para ligar o macro, a janela trava e para de responder, não consigo clicar no botão novamente para desativar o macro nem nada do tipo, porém percebo que o macro continua em execução perfeitamente.
Conto com a ajuda de vocês para sanar esse travamento na janela do Tkinter. Aqui está meu código.
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()