R
R
Roman Novikov2020-05-13 12:56:06
Python
Roman Novikov, 2020-05-13 12:56:06

How to make an infinite loop in python tkinter so that the interface does not freeze?

I'm writing a small utility to close processes automatically. And faced the problem of cycles. Since the entire graphical interface is one big endless loop, when an additional loop is turned on, the weight of the interface freezes. So, it is necessary that he does not lay down and work further.
Here is the code ( I'm new to programming and this code can be bad. I know) :

print("Запуск...", end='')
from tkinter import *
import time
import os
import pyautogui as pg
import subprocess as sp
print('ok! ')

#Functions


def logicp(event):
    def_name=name.get()
    def_text=text.get()
    try:
        def_delay=delay.get()
        def_delay=int(def_delay)
    except:
        work['text']=''.join("Можно только число!")
        time.sleep(4)
        work['text']=''.join("Остановлено")
    if (def_name and def_text and def_delay):
        work['text']=''.join("Работа...")
        while 1:
            print("Проверка...")
            time.sleep(def_delay)
            tasks=sp.getoutput('tasklist');
            if 'Zoom.exe' in tasks:
                os.system('taskkill /f /im Zoom.exe')
                pg.alert(str(def_text), str(def_name))
    else:
        pass

root=Tk()

root.resizable(width=False, height=False)
root.geometry('400x300')
root.title('Zoom parser')

work=Label(text='Остановлено', font='Consolas 10')
start=Button(root, text='Начать')

#enters

text=Entry(root, width=30)
textl=Label(text='Текст', font='Consolas 10')
name=Entry(root, width=30)
namel=Label(text='Имя окна', font='Consolas 10')
delay=Entry(root, width=10)
delayl=Label(text='Задержка', font='Consolas 10')


#Packer

work.pack()
start.pack()
textl.pack()
text.pack()
namel.pack(side=RIGHT)
name.pack(side=RIGHT)
delayl.pack(side=LEFT)
delay.pack(side=LEFT)

#Binds

start.bind("<Button-1>", logicp)

root.mainloop()


Without a loop in the function logicp()it will not work! And without root.mainloop()the program will immediately close.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2020-05-13
@Zarom

Move the body of the while loop into a separate function, let's say check_task
Instead of the while loop and the last line in check_task ,root.after(def_delay, check_task)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question