You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派3(Ubuntu MATE):如何用Python Tkinter阻止用户访问系统并锁定GUI

Hey there, let's work through this problem you're having with your Raspberry Pi 3 Tkinter GUI. I've built a few kiosk-style setups on Ubuntu MATE before, so here are some practical, tested solutions to lock down the interface and keep your GUI front-and-center:

1. Fix Window Layering for overrideredirect

The issue with toplevel widgets appearing behind your main window on the Pi is likely due to how MATE's Marco window manager handles undecorated windows. Instead of relying solely on update_idletasks(), explicitly force all windows (main and toplevel) to stay on top:

import tkinter as tk

root = tk.Tk()

# Remove window decorations first
root.overrideredirect(True)

# Set window to full screen, positioned at the top-left corner
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.geometry(f"{screen_width}x{screen_height}+0+0")

# Force main window to stay above all other elements
root.attributes("-topmost", True)

# Helper function to create toplevel windows that also stay on top
def create_toplevel():
    top = tk.Toplevel(root)
    top.overrideredirect(True)
    top.attributes("-topmost", True)
    top.geometry("300x200+100+100")
    tk.Label(top, text="This stays on top!").pack(pady=20)

# Test button to verify toplevel behavior
tk.Button(root, text="Open Toplevel", command=create_toplevel).pack(pady=50)

root.mainloop()

By adding -topmost to every window (main and toplevel), you override the window manager's default stacking behavior on the Pi.

2. Use System Full-Screen Mode (More Reliable for Kiosks)

If overrideredirect keeps causing issues, try Tkinter's native full-screen mode instead. This plays nicer with Ubuntu MATE's window manager while still locking down the interface:

import tkinter as tk

root = tk.Tk()

# Enable full-screen mode (covers entire display)
root.attributes("-fullscreen", True)

# Disable the window close event (blocks users from closing via window manager)
root.protocol("WM_DELETE_WINDOW", lambda: None)

# Keep window pinned to the top
root.attributes("-topmost", True)

# Optional: Hide the cursor for a true kiosk experience
# root.config(cursor="none")

root.mainloop()

This method lets the window manager recognize the window but removes all ways to close or minimize it.

3. Block System Shortcuts (Critical for Full Lockdown)

Even with a full-screen GUI, users could still use keyboard shortcuts (like Alt+Tab or Ctrl+Alt+T) to escape to the system. Disable these shortcuts on Ubuntu MATE:

Option A: Disable via Python

First install xdotool (if not already present):

sudo apt install xdotool

Then add this to your script to block common escape shortcuts:

import subprocess

# Disable keybindings for window switching, terminal, and desktop
subprocess.run(["gsettings", "set", "org.mate.Marco.general", "enable-keybindings", "false"])
subprocess.run(["gsettings", "set", "org.mate.Marco.keybindings", "run-command-terminal", "''"])
subprocess.run(["gsettings", "set", "org.mate.Marco.global-keybindings", "show-desktop", "''"])

# Restore settings when your app exits (use try/finally to avoid permanent changes)
try:
    root.mainloop()
finally:
    subprocess.run(["gsettings", "reset", "org.mate.Marco.general", "enable-keybindings"])
    subprocess.run(["gsettings", "reset", "org.mate.Marco.keybindings", "run-command-terminal"])
    subprocess.run(["gsettings", "reset", "org.mate.Marco.global-keybindings", "show-desktop"])

Option B: Manual Setup

You can also disable shortcuts through System > Preferences > Hardware > Keyboard Shortcuts — just remove or reassign any shortcuts that let users exit your GUI.

4. Boot Directly into Your GUI (Kiosk Mode)

To ensure users can't access the desktop at all, configure Ubuntu MATE to boot straight into your app:

  1. Create an autostart entry for your script:

    • Create a file named kiosk-gui.desktop in ~/.config/autostart/
    • Add this content:
      [Desktop Entry]
      Type=Application
      Name=Kiosk GUI
      Exec=python3 /path/to/your/gui_script.py
      X-GNOME-Autostart-enabled=true
      
  2. Enable auto-login for your user:

    • Go to System > Administration > Login Window > Users tab
    • Check "Automatic login" for your user account
  3. Optional: Hide desktop icons and panel:

    gsettings set org.mate.background show-desktop-icons false
    gsettings set org.mate.panel toplevel-id-list "[]"
    

Combining all these steps will create a locked-down environment where users can't close your GUI or access the underlying Ubuntu MATE system on the Raspberry Pi 3.

内容的提问来源于stack exchange,提问作者Ori Levy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:44:02