Home > database >  Tkinter window Completly freeze when you move it
Tkinter window Completly freeze when you move it

Time:11-22

The window of Tkinter just completly freeze with all the widgets when I move the Tkinter window and that's my problem I tested it with another code and it always does the same thing

Is the problem exclusively with tkinter?

just move your tkinter window from left to right you will see that absolutely all the program freeze it's incredible

Someone said to put main in a separated thread but how ? Like without example I don't even know what It means :(

how do you put the threads outside of mainloop() ? What does it mean ? I putted root.mainloop() before the thread1 = threading.Thread(target= lambda : fct(), daemon=True) thread1.start() and it does nothing

from tkinter import *
from tkinter import ttk 
import time
import threading
import win32api
import pyautogui 



root = Tk()
root.geometry('800x438')
root.resizable(False,False)
root.configure(bg='gray')


label = Label(root, text='Display content', fg='yellow', bg='black', font=('Arial', 13), width=20)
label.place(relx=0.5,rely=0.3)




firstentryvar = StringVar()
secondentryvar = StringVar()



firstentry = Entry(root, textvariable=firstentryvar , justify=CENTER, font = ('Arial', 12))
secondentry = Entry(root, textvariable=secondentryvar, justify=CENTER, font = ('Arial', 12))





def displaycontent(*args): 

    firstentry.pack()
    secondentry.pack()
    label.bind('<Button-1>', hidecontent)



def hidecontent(*args): 

    firstentry.pack_forget()
    secondentry.pack_forget()
    label.bind('<Button-1>', displaycontent)

    


label.bind('<Button-1>', displaycontent)

def function1(*args): 
     count = 0
     bool = False
     while count < 10: 
         
            for i in firstentry.get(): 
                if bool == False: 
                    count  =1 
                    print(i)
                    bool = True
                else: 
                    bool = False
              


def function2(*args): 
   while True: 
    if win32api.GetKeyState(0x45) < 0: 
          
          print('you pressed e')
  
    

thread1 = threading.Thread(target = lambda : function1(), daemon=True)
thread1.start()

thread2 = threading.Thread(target = lambda : function2(), daemon=True)
thread2.start()


   
root.mainloop() 

the code may not mean much but it's enough to reproduce my example, well you will notice that if you click on the display label and then move the window without entering anything in the entries the window will bug/freeze why?

CodePudding user response:

I played a bit with your code and it seems that problem is with your while loops. Even though you used threads correctly, using while loops this way makes your program uses all the resources to loop into it. What I means is as you started program, even before you press label to show entry widgets, your loops just iterated thousand of times if not tens of thousands. However, simply putting a time sleep, you can easily stop this exponential resource consuming. However, you shouldn't use time.sleep with tkinter if you aren't using inside threads. As we are using loops inside threads, there is no problem.

For example:

def function1(*args): 
    count = 0
    bool = False
    while count < 10:
        time.sleep(0.1)
        for i in firstentry.get(): 
            if bool == False: 
                count  =1 
                print(i)
                bool = True
            else: 
                bool = False
  • Related