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

Pydroid中Tkinter的asksaveasfile无法正确赋值导致AttributeError问题求助

Pydroid中Tkinter的asksaveasfile无法正确赋值导致AttributeError问题求助

我写了一段Tkinter代码,用来把文本框内容保存到本地文件,在常规Python环境下运行完全正常,但放到Pydroid里就出现了AttributeError问题,折腾了好久没搞明白原因,想请大家帮忙看看。

核心代码

from tkinter.filedialog import *
import tkinter as tk

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.apptitle = "TODO.py"
        #...其他代码
        self.openfile = None  # 启动时无打开的文件
        self.savefile = None  # 启动时无保存的文件
        self.currentfile = None  # 启动时无当前活跃文件
        self.filetype = [("Text File", "*.txt"), ("Markdown File", "*.md")]
        #...其他代码

    def filemenu_save(self):
        if self.currentfile is None: 
            # 如果当前没有活跃文件...
            self.savefile = asksaveasfile(filetypes=self.filetype, defaultextension=self.filetype) 
            # ...弹出保存对话框
            try:
                self.currentcontent = self.textbox.get("1.0", tk.END).rstrip("\n") 
                # 获取文本框内容
                with open(self.savefile.name, "w", encoding="utf-8-sig") as f:
                    f.write(self.currentcontent) 
                # 将内容写入文件
                self.currentfile = self.savefile  # 标记当前活跃文件
                self.title(self.apptitle + " - " + self.savefile.name)  # 更新窗口标题
            except IOError as e:
                print("写入文件时出错:", e)
        else:
            try:
                self.currentcontent = self.textbox.get("1.0", tk.END).rstrip("\n")
                with open(self.currentfile.name, "w", encoding="utf-8-sig") as f:
                    f.write(self.currentcontent)
                self.currentfile = self.currentfile
            except IOError as e:
                print("写入文件时出错:", e)
        #...其他代码

这段代码的逻辑很简单:如果当前没有打开的文件,就调用asksaveasfile弹出保存对话框,拿到文件对象后写入文本内容;如果已有活跃文件,就直接覆盖写入。

遇到的错误情况

  1. 未安装Pydroid Permission Plugin且未添加import sys时:
    触发保存功能会抛出以下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/tkinter/__init__.py", line 2071, in __call__
return self.func(*args)
~~~~~~~~~^^^^^^^
File "/storage/emulated/0/GitHub Mobile Stuff/TODO.py/TODO.py", line 149, in filemenu_save
with open(self.savefile.name, "w", encoding="utf-8-sig") as f:
^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'name'
  1. 安装Pydroid Permission Plugin并添加import sys后:
    错误信息变成了这样:
Exception in Tkinter callback
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.13/tkinter/__init__.py", line 2071, in __call__
return self.func(*args)
~~~~~~~~~^^^^^^^
File "/storage/emulated/0/GitHub Mobile Stuff/TODO.py/TODO.py", line 150, in filemenu_save
f.write(self.currentcontent)
^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'name'

我原本以为asksaveasfile会正常返回文件对象,后续代码能顺利执行,但在Pydroid里self.savefile似乎一直是None,导致访问.name属性时报错。我参考了Pydroid自带的示例代码,添加了import sys,也安装了权限插件,但问题依然存在。我Python经验不算丰富,实在搞不懂为什么Pydroid里的变量赋值会出现这种异常,希望有经验的朋友能帮我分析原因,或者给出解决办法。

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 06:42:58