使用python3在Raspberry Pi上打开和关闭Tkinter GUI

问题描述 投票:3回答:2

我需要打开和关闭显示器,当显示器打开时,我需要它来显示使用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标签。

python-3.x user-interface tkinter raspberry-pi3 sensor
2个回答
1
投票

你已经陷入了tkinter应用程序的标准陷阱。您有一个无限循环,不允许tkinter GUI更新。切勿在tkinter应用程序中使用无限循环。只使用mainloop()

如果要将读取GPIO与tkinter混合使用,则需要采用以下方法之一

  1. 使用.after tkinter方法定期调度函数调用。该功能从GPIO引脚读取并根据GPIO状态更新GUI。 root.after(1000,do_function)将在1000毫秒后调用名为do_function的函数。如果您的do_function还包含对.after方法的调用,则该函数将安排自己在指定的时间段后再次调用。
  2. 使用GPIO回调。如果您使用gpiozero库而不是RPi.GPIO模块,则更容易,因为您可以使用button.when_pressed = do_function语法。

无论使用哪种方法,调用的函数都将添加任何代码,以根据GPIO引脚的状态更改GUI。


0
投票

在我中断程序后,Tkinter GUI显示,因为root.mainloop()被放置在程序的最后。

Tkinter understanding mainloop这个链接帮助我理解了主循环,并在python草图中进行了必要的更改。

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) #to warmup the sensor
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)

os.system("xset dpms force off")
#keep the display turned off until any motion is detected

try:
    while True:
        i = GPIO.input(sensor)
        if i==1:#turn on the display when motion is detected
           print("Human presence detected")
           os.system("xset dpms force on")
           root.update()
           time.sleep(30)
       else:
           print("no human around")
           os.system("xset dpms force off")
           time.sleep(5)

except KeyboardInterrupt:
    GPIO.cleanup()
© www.soinside.com 2019 - 2024. All rights reserved.