用于 Raspberry Pi 的基于 Tkinter/Canvas 的类似信息亭的程序

Gra*_*tty -1 python kiosk tkinter raspberry-pi tkinter-canvas

我想在 Raspberry Pi(或者,就此而言,任何基于 Unix/Linux 的计算机)上运行一个 Python 程序,它可以有效地将整个屏幕变成一个画布,并允许我在其上实时绘制文本和图形对象。我理想地希望这也能自动隐藏桌面上的所有其他内容并消除窗口框架和任务栏,类似于以全屏模式播放视频(使用 ESC 退出)。

到目前为止,我的研究表明 Tkinter/Canvas 将是最简单的解决方案。但是,虽然我在网上找到了一些例子来完成我上面描述的部分内容,但我无法将这些部分放在一起完成所有工作的形式。我之前没有使用 Tkinter 的经验也无济于事。

如果有人能指出我所描述的设置的最小工作示例,我将不胜感激。

fur*_*ras 6

最小的例子。它创建无边框的全屏窗口,始终在顶部,
因此您无法切换到其他窗口,即。控制台杀死程序。

您可以通过按关闭它,ESC但如果不起作用,我会添加功能以在 5 秒后关闭ESC:)

它在全屏上绘制红色椭圆。

#!/usr/bin/env python3

import tkinter as tk

# --- functions ---

def on_escape(event=None):
    print("escaped")
    root.destroy()

# --- main ---

root = tk.Tk()

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

# --- fullscreen ---

#root.overrideredirect(True)  # sometimes it is needed to toggle fullscreen
                              # but then window doesn't get events from system
#root.overrideredirect(False) # so you have to set it back

root.attributes("-fullscreen", True) # run fullscreen
root.wm_attributes("-topmost", True) # keep on top
#root.focus_set() # set focus on window

# --- closing methods ---

# close window with key `ESC`
root.bind("<Escape>", on_escape)

# close window after 5s if `ESC` will not work
root.after(5000, root.destroy) 

# --- canvas ---

canvas = tk.Canvas(root)
canvas.pack(fill='both', expand=True)

canvas.create_oval((0, 0, screen_width, screen_height), fill='red', outline='')

# --- start ---

root.mainloop()
Run Code Online (Sandbox Code Playgroud)