Tkinter GUI hangs when looping

1

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()
    
asked by anonymous 13.01.2016 / 03:24

2 answers

0

Your first problem is that you use time.sleep , and the second is to use while loop. When you use the time.sleep function blocks the mainloop of the Tk object you created. The same happens if you have a loop (which lasts some time), or an operation that lasts a long time. Here you have a tutorial if you want to know more about the subject and what are some possible solutions.

  

link

Not without good what you want to create (and for what) with this program, but one solution would be to use after " of any tkinter widget (or rather any widget has this function because it inherits from Widget parent or parent) This function theoretically does not block the interface, and allows you to schedule the execution of a piece of code between a specified time in milliseconds.

Here you get your program modified but using the after function. If you do not understand something, ask.

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 callback1(self):
        print "callback 1" 

        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)

        self.primeira.after(2000, self.callback2)

    def callback2(self):
        print "callback 2"         
        ctypes.windll.user32.mouse_event(0x8, 0, 0, 0, 0)
        ctypes.windll.user32.mouse_event(0x10, 0, 0, 0, 0)
        self.primeira.after(2000, self.callback3)

    def callback3(self):
        print "callback 3"           
        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)
        self.primeira.after(200, self.callback4)

    def callback4(self):
        print "callback 4"           
        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)
        self.primeira.after(200, self.callback5)

    def callback5(self):
        print "callback 5"                   
        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)
        self.primeira.after(200, self.callback6)

    def callback6(self):
        print "callback 6"                   
        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)

        if self.botao['bg'] == 'deepskyblue':
            self.primeira.after(200, self.callback1)

    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)

        if self.botao['bg'] == 'deepskyblue':
            self.primeira.after(2000, self.callback1)


raiz = Tk()
Janela(raiz)
raiz.mainloop()

Note that I no longer use while loop but yes a if with the possibility of scheduling a call back to the function (or callback, as it says in English) callback1 , the first one that is called with all ifs.

    
14.01.2016 / 03:05
-1

Well try using command instead of a event handle :

self.botao = Button(self.terceira, font=('Arial', '13', 'bold'), text='ON', bg='gray', command=self.ligar)
self.botao.pack()


def ligar(self):
    ...

I think the problem might be while running while , too, try try instead of while :

try:
    if self.botao['bg'] == 'deepskyblue':
        time.sleep(2)
        if self.hot == 1:
            ...

except ValueError:
    print("ops!!! algo deu errado")
    
14.01.2016 / 02:54