PyBox / Blog / How to

How to run Tkinter on Android (and why it needs X11)

Tkinter apps need a display server. Android does not provide one. Here is how to get one on your phone, and a clock to try.

How to run Tkinter on Android (and why it needs X11): the PyBox screen this guide uses
PyBox is the Python IDE I build. Everything below works with it, and the steps are the same ideas you would use anywhere. Get it on Google Play.

Why Tkinter fails on a plain Android Python

Tkinter wraps the Tk toolkit, and Tk on Linux draws through the X11 protocol. A normal Android has a different graphics stack, so Python code that calls tk.Tk() finds no display and fails with an error about a missing display.

What PyBox does

PyBox runs an X server inside the app, with a window manager embedded in it. A Tkinter script opens a real window on the PyBox desktop. The desktop has a panel with CPU, memory, FPS and clock, a taskbar, and a windows list. Touch, keyboard and clipboard are mapped through to X11.

A first example

import time
import tkinter as tk

root = tk.Tk()
root.title("Hello from PyBox")
root.geometry("420x240+120+100")

label = tk.Label(root, font=("Helvetica", 40))
label.pack(expand=True)

def tick():
    label.config(text=time.strftime("%H:%M:%S"))
    root.after(200, tick)

tick()
root.mainloop()

Create a project, paste this into main.py and press Run. The window appears on the desktop. The starter projects include Tkinter Calculator, Paint App, 2048, Minesweeper and Pendulum Lab to read next.

Tuning

  • Touch mode: Direct clicks where you touch. Touchpad moves a cursor with your finger. There is two-finger tap for right-click and natural scrolling.
  • Resolution and DPI: set in Settings, and apply the next time a GUI app starts.
  • Fit to window: resizes the desktop on rotation and in split screen.

Tips

  • Use after() for animation, never while True in the main thread.
  • Give windows a geometry so they do not stack in a corner.
  • Canvas drawing is the easiest way to get nice-looking results.

More in Run GUI apps on Android.

Questions

Does Tkinter work on Android?

Not in a plain console, because Tkinter draws through X11. PyBox runs an X11 server inside the app, so Tkinter windows open on its desktop.

Can I use pygame and Kivy the same way?

Yes. They also open windows on the PyBox X11 desktop. See the GUI apps guide.

Anshuman MishraBuilds PyBox at Vytros, a Python IDE for Android. About the developer.

Code Python on your phone.