我需要控制开关显示屏,同时在显示屏打开时,使用tkinter-python创建欢迎消息。
我编写的程序能够控制开关显示屏,但只有在退出程序后才会显示tkinter标签。
请问为什么在编译程序时tkinter标签没有被显示?
import sys
import os
import time
from tkinter import *
import tkinter as tk
from tkinter.font import Font
import RPi.GPIO as GPIO
#pin description
sensor = 11
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor,GPIO.IN)
print("Initializing PIR sensor....")
time.sleep(12)
print("PIR ready")
print("")
#initializing tkinter parameters
root = tk.Tk()
text = tk.Text(root)
font1 = Font(family = 'Helvetica', size = 30, weight = 'bold')
font2 = Font(family = 'Helvetica', size = 20, weight = 'bold')
font3 = Font(family = 'Helvetica', size = 15, weight = 'bold')
explanation = """WELCOME TO MY GUI"""
colin = tk.PhotoImage(file="background.gif")
#background=tk.Label(root,compound = tk.CENTER, text=explanation,font=font1, image=colin).pack()
def exitProgram():
print("Exit button pressed")
GPIO.cleanup()
root.quit()
exitButton =tk.Button(root, text = "Exit", font = font3, command = exitProgram, height = 1, width = 4, bd =1)
exitButton.place(x= 350, y =435)
root.attributes("-fullscreen",True)
if sys.platform.startswith('linux'):
os.system("xset dpms force off")
else:
os.system("xset dpms force on")
try:
while True:
i = GPIO.input(sensor)
if i==1:
background=tk.Label(root,compound = tk.CENTER, text=explanation,font=font1, image=colin).pack()
print("Human presence detected")
if sys.platform.startswith('linux'):
#background = tk.Label(root, compound = tk.CENTER, text=explanation, font = font1,image=colin).pack()
os.system("xset dpms force on")
time.sleep(30)
else:
print("no human around")
if sys.platform.startswith('linux'):
os.system("xset dpms force off")
time.sleep(5)
except KeyboardInterrupt:
GPIO.cleanup()
root.mainloop()
如您所见,我正在使用传感器来检测任何动作。如果检测到动作,则屏幕会打开并显示欢迎消息。
您还会看到,在草稿的不同位置,我已经注释了相同的代码行。我尝试将背景标签放置在不同的位置,但仍然出现同样的问题。屏幕会打开和关闭,但tkinter标签只有在我退出程序后才会显示。