我在 Gnu/Linux 上有一個 python 應用程式。該程式從內核啟用或禁用攝像頭和麥克風。該程式運行良好,除了一個我無法解決的小問題。該程式有 4 個按鈕。有 2 個啟用攝像頭和麥克風的按鈕,以及兩個禁用它們的按鈕。有四個文本:Camera is not loaded、Camera is loaded和。Microphone is loadedMicrophone is not loaded
問題:
問題是訊息顯示兩種文本。它顯示Camera is loaded和Camera not loaded。例如,當我第一次打開應用程式時,它說相機未加載。當我按下按鈕啟用相機時,文本顯示相機已加載,但問題是未加載的文本相機未洗掉。它同時顯示訊息Camera is loaded和Camera not loaded. 如果我退出程式并再次加載問題已解決,它只顯示訊息camera is loaded。我想在不退出程式的情況下重繪 新訊息并再次加載。
誰能幫我解決這個問題?
這是Python程式:
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()
uj5u.com熱心網友回復:
與其一遍又一遍地創建新標簽,不如創建一次標簽并根據您的要求進行配置,ala
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/447555.html
標籤:Python python-3.x 重击 贝壳 tkinter
