Tkinter
在 iPhone 和 iPad 离线运行 Tkinter 脚本,需 PythonIDE 1.8.9。
PythonIDE 1.8.9 在 iPhone 和 iPad 上内置 Python 标准库的 tkinter 和 tkinter.ttk。打开已有的 .py 文件,点编辑器的「运行」,也可以在前台终端运行 python3 main.py。Tkinter 无需联网或额外安装。
#快速开始
把下面的代码保存为 main.py 并运行。
已复制
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Tkinter 示例")
message = ttk.Label(root, text="你好,Tkinter!")
message.pack(padx=24, pady=16)
def greet():
message.configure(text="按钮已点按")
ttk.Button(root, text="打招呼", command=greet).pack(pady=(0, 8))
ttk.Button(root, text="关闭", command=root.destroy).pack(pady=(0, 16))
root.mainloop()
#预期效果
屏幕中央打开一个带中文标题的小窗口。点「打招呼」会更新窗口里的文字,点「关闭」会退出窗口。
#窗口操作
小窗口打开时居中。大窗口按可用高度等比显示,较宽的窗口可用双指横移,或点右上角的适配按钮查看完整窗口。窗口标题和控件可以显示中文。
#运行现有脚本
- 把脚本引用的图片、配置和数据文件放到项目目录。使用 Windows 或 macOS 专用路径、模块的脚本需要按 iOS 环境调整。
- 在同一脚本中切换图形界面时,先退出 Tk 窗口,再打开 Pygame 显示窗口,调用
pygame.quit()后即可重新创建 Tk 窗口。 - Tkinter 窗口需要 App 在前台,适合从编辑器或前台终端运行。
#常见问题
- 导入
tkinter失败时,先在 App 设置里确认版本为 1.8.9 或更高。 - 窗口右侧超出视野时,点右上角的适配按钮,或双指横移到需要操作的位置。
#English quick start
Tkinter and ttk are bundled for offline use in PythonIDE 1.8.9 on iPhone and iPad. Save the sample as main.py and run it from the editor or foreground terminal. Pan wide windows with two fingers or tap the fit button to see the whole window. Keep the script’s assets alongside the project and adapt desktop-specific dependencies for iOS.