A
A
Arthur Samurai2021-09-11 19:03:57
Python
Arthur Samurai, 2021-09-11 19:03:57

How to properly implement multithreading in Tkinter?

The problem is that in the pr -> printing function, rather long calculations take place, and at this moment the window hangs and does not respond, and the output in the text field occurs only after the end of the main calculation process. How are threads implemented correctly? I'm using Threads, but it doesn't help in this example, the window still hangs, how to make the calculations separate from the mainloop() function here?

#!/usr/bin/env python
import os
import threading
import time

from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext  


class MainWindow(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        self.init_main()


    def init_main(self):
        lb_log = Label(text=u"логин ")
        lb_log .pack()
        ent_value_log = Entry(width=40)
        ent_value_log .pack()
        lb_pass = Label(text=u"пароль ")
        lb_pass.pack()
        ent_value_pass = Entry(width=40)
        ent_value_pass.pack()
        btn_print = ttk.Button(text='print', command=lambda: self.printing(ent_value_log.get(), ent_value_pass.get()))
        btn_print .place(x=340, y=20)
        self.console = scrolledtext.ScrolledText(state='disable')
        self.console.pack(pady=15)


    def insert_to_console(self, text):
        self.console.configure(state='normal')  # enable insert
        self.console.insert(END, text)
        self.console.yview(END)  # autoscroll
        self.console.configure(state='disabled')


    def pr(self, login, password):
        self.insert_to_console(login + ' ' + password)
        time.sleep(5)
        self.insert_to_console(login + ' ' + password)
        time.sleep(5)

    def printing(self, login, password):
        pycrypt = threading.Thread(target=self.pr(login, password))
        pycrypt.start()

    def run_app():
        root = tk.Tk()
        app = MainWindow(root)
        root.title("palevo")
        root.geometry("420x400")
        root.mainloop()

zv = MainWindow
zv.run_app()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2021-09-12
@zvepb

pycrypt = threading.Thread(target=self.pr, args=(login, password))

S
Stefan, 2021-09-11
@MEDIOFF

In Python, threads are not parallel (yes, they are not parallel anyway, since python uses OS threads), read about the GIL, in short, you always have only one thread active, and there is no difference what you consider in the main thread , that in a separate one, only one works and while it holds the GIL all the others are waiting (in fact, your program will work even slower with threads, in the case of CPU-bound tasks, because you still spend time switching contexts), so if your task is calculations - then take this task into a separate process, the multiprocessing library will help you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question