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

Tkinter脚本打开文件报错:TypeError问题排查求助

问题分析与修复

错误原因

你遇到的TypeError是因为gape函数里犯了两个关键错误:

  • 调用read()得到的是文件对象,却把它传给了Tk()构造函数,而Tk()只接受字符串(窗口名称)或None,不接受文件对象
  • 打开新窗口应该用Toplevel()而不是重复创建Tk(),一个Tkinter程序只能有一个主Tk()实例,额外窗口要用顶层窗口组件

修复步骤

1. 删掉无用的read()函数

这个函数返回文件对象,实际在打开窗口时根本用不上,直接删除即可。

2. 重写gape()函数

修改后的函数逻辑:

  • 创建顶层窗口作为文件内容展示窗口
  • 在顶层窗口内添加Text组件和滚动条
  • 读取data.txt内容并插入到Text组件中

修正后的gape()代码:

def gape():
    # 创建顶层窗口,而不是新的Tk实例
    top_window = Toplevel(window)
    top_window.title("文件内容")
    top_window.geometry("400x300")

    # 创建Text组件,父窗口是top_window
    text_widget = Text(top_window)
    text_widget.pack(side=LEFT, fill=BOTH, expand=True)

    # 创建滚动条,父窗口是top_window
    scrollbar = Scrollbar(top_window, command=text_widget.yview)
    scrollbar.pack(side=RIGHT, fill=Y)

    text_widget.config(yscrollcommand=scrollbar.set)

    # 读取文件内容并插入
    try:
        with open("data.txt", "r", encoding="utf-8") as f:
            content = f.read()
            text_widget.insert(INSERT, content)
    except FileNotFoundError:
        # 如果文件不存在,提示信息
        text_widget.insert(INSERT, "文件不存在,暂无内容")

3. 完整修正后的代码

from tkinter import *
from tkinter import messagebox
from tkinter import filedialog

window = Tk()
window.geometry("400x400")
window.title("Write to File")

def write():
    data = "\n" + "navn: " + txt_name.get(1.0, "end-1c") + "\n" + "Etternavn: " + txt_konto.get(1.0, "end-1c") + "\n" + "Telefon nr: " + txt_kid.get(1.0, "end-1c") + "\n" + "Epost: " + txt_beløp.get(1.0, "end-1c")
    # 用with语句自动关闭文件,更安全
    with open("data.txt", "a", encoding="utf-8") as file:
        file.write(data)
    txt_name.delete("1.0", END)
    txt_konto.delete("1.0", END)
    txt_kid.delete("1.0", END)
    txt_beløp.delete("1.0", END)

def gape():
    top_window = Toplevel(window)
    top_window.title("文件内容")
    top_window.geometry("400x300")

    text_widget = Text(top_window)
    text_widget.pack(side=LEFT, fill=BOTH, expand=True)

    scrollbar = Scrollbar(top_window, command=text_widget.yview)
    scrollbar.pack(side=RIGHT, fill=Y)

    text_widget.config(yscrollcommand=scrollbar.set)

    try:
        with open("data.txt", "r", encoding="utf-8") as f:
            content = f.read()
            text_widget.insert(INSERT, content)
    except FileNotFoundError:
        text_widget.insert(INSERT, "文件不存在,暂无内容")

lbl_name = Label(window, text="Navn:").pack()
txt_name = Text(window, height = 1, width = 20)
txt_name.pack()

lbl_konto = Label(window, text="Etternavn").pack()
txt_konto = Text(window, height = 1, width=20)
txt_konto.pack()

lbl_kid = Label(window, text="telefon nr").pack()
txt_kid = Text(window, height=1,width=20)
txt_kid.pack()

lbl_beløp = Label(window,text="Epost").pack()
txt_beløp = Text(window,height=1,width=20)
txt_beløp.pack()

btn_write = Button(window,text="Lagre", command=write).pack()
btn_read = Button(window,text="Åpne", command=gape).pack()

btn_close = Button(window, text="Exit", command=window.destroy)
btn_close.pack()

window.mainloop()

额外优化说明

  • 用with语句操作文件,避免忘记关闭文件导致的资源泄漏
  • 添加了文件不存在的异常处理,提升程序健壮性
  • 给顶层窗口设置了标题和初始大小,用户体验更好

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 20:03:21