Search code examples
python-3.xtexttkinterscreenlight

how to make the python program light, which is written to print the text on screen directly without window?


From last few days, I have been working on a program which one part is to show the text directly on the window screen, and also update/change them as per requirement. I have completed this part with Tkinter module in python.

The problem is whenever I run the program it behaves like a heavy program due to which other processes become slow. Also If I tried to do some other process in parallel, the background of the text becomes black, which is absolutely undesirable.as the shown in image

I also want to show some dynamic symbol like loading but the use of two Tkinter widget make it slower. if possible please make it more modular and light.

from tkinter import *
import win32api, win32con, pywintypes
from time import sleep

f=Tk()
var = StringVar()
var.set(' ')
f =Label(textvariable = var, font=('Cooper','60'), fg='blue', bg='white')
f.master.overrideredirect(True)
f.master.geometry("+900+200")
f.master.lift()
f.master.wm_attributes("-topmost", True)
f.master.wm_attributes("-disabled", True)
f.master.wm_attributes("-transparentcolor", "white")
f.pack()


for i in range(10):
    sleep(5) # Need this to slow the changes down
    var.set(u'[ A ]' if i%2 else u'[ B ]')
    f.update_idletasks()

also, want to ask can we do it without using the Tkinter module.so it becomes more light and modular. and dependency will be less.


Solution

  • Here is a code which makes your code responsive and also don't use your cpu too much.

    from tkinter import *
    import win32api, win32con, pywintypes
    import time
    
    f=Tk()
    var = StringVar()
    var.set(' ')
    f =Label(textvariable = var, font=('Cooper','60'), fg='blue', bg='white',bd=0)
    f.master.overrideredirect(True)
    f.master.geometry("+900+200")
    f.master.lift()
    f.master.wm_attributes("-topmost", True)
    f.master.wm_attributes("-disabled", True)
    f.master.wm_attributes("-transparentcolor", "white")
    f.pack()
    
    
    for i in range(10):
        f.update()
        t = time.time()
        while time.time() - t < 5:
            f.update()
        var.set(u'[ A ]' if i%2 else u'[ B ]')
        f.update_idletasks()
        f.update()
    

    Here is an image of task manager. Its taking only 15 MB memory and no cpu:

    enter image description here