Home > Back-end >  How do I refresh text in python?
How do I refresh text in python?

Time:03-23

I have a python app on Gnu/Linux. The program enables or disables the camera and microphone from the kernel. The program works fine except for a little problem that I cannot resolve. The program has 4 buttons. There are 2 buttons that enable camera and microphone and two buttons that disable them. There are four texts: Camera is not loaded, Camera is loaded, Microphone is loaded and Microphone is not loaded.

PROBLEM:

The problem is that the messages show both texts. It shows Camera is loaded and Camera not loaded. Example when I open the app for the first time it says Camera not loaded. When I press the button to enable the camera the text says Camera is loaded but the problem is the text camera not loaded is not deleting. It shows both messages Camera is loaded and Camera not loaded. If I exit from the program and load again the problem solved, It shows only the message camera is loaded. I want to refresh the new message without exiting the program and load again.

Can anyone help me to solve that?

Here is Python program:

def update():
    root.after(1000, update)  # run itself again after 1000 ms
    var1()

def var1():
    var1 = StringVar()
    exit_code_cam = os.system("lsmod |grep uvcvideo")
    load = "Camera is loaded"
    notLoad = "Camera not loaded"

    if exit_code_cam == 0:
        l1 = Label(root, textvariable=var1, fg="green")
        l1.place(x=2, y=30)
        var1.set(load)
    else:
        l1 = Label(root, textvariable=var1, fg="red")
        l1.place(x=2, y=10)
        var1.set(notLoad)

def button_add1():
    cam_mic_bash.enable_cam()
    var1()

def button_add3():
    cam_mic_bash.disable_cam()
    var1()

# Define Buttons
button_1 = Button(root, text="Allow camera", width=20, height=5, padx=0, pady=0, command=button_add1)
button_3 = Button(root, text="Block camera", width=20, height=5, padx=0, pady=0, command=button_add3)

# Put the buttons on the screen
button_1.place(x=0, y=75)
button_3.place(x=0, y=150)

# run first time
update()

root.mainloop()

CodePudding user response:

Instead of creating new labels over and over again, create the label once and configure it according to your requirements, á la

import os
from tkinter import StringVar, Tk, Label, Button

root = Tk()
camera_loaded_stringvar = StringVar()
camera_label = Label(root, textvariable=camera_loaded_stringvar)
camera_label.place(x=2, y=10)

def update_loop():
    root.after(1000, update_loop)  # run itself again after 1000 ms
    update_camera_label()


def update_camera_label():
    is_loaded = os.system("lsmod | grep uvcvideo") == 0
    if is_loaded:
        camera_loaded_stringvar.set("Camera is loaded")
        camera_label.configure(fg="green")
    else:
        camera_loaded_stringvar.set("Camera not loaded")
        camera_label.configure(fg="red")


def button_add1():
    enable_cam()
    update_camera_label()


def button_add3():
    disable_cam()
    update_camera_label()


allow_camera_button = Button(root, text="Allow camera", width=20, height=5, padx=0, pady=0, command=button_add1)
block_camera_button = Button(root, text="Block camera", width=20, height=5, padx=0, pady=0, command=button_add3)

allow_camera_button.place(x=0, y=75)
block_camera_button.place(x=0, y=150)

update_loop()
root.mainloop()
  • Related