Tkinter记事本保存文件报错:'Button' object is not callable 求助
在开发Tkinter记事本应用时,点击Save按钮调用savefile函数时触发以下错误:
TypeError: 'Button' object is not callable
完整应用代码:
import re from tkinter import * from tkinter import ttk import time from turtle import bgcolor import webbrowser from tkinter import messagebox from tkinter import filedialog root = Tk() root.title('Notepad') root.iconbitmap('C:/Users/Hero/Documents/Visual Studio code/My project/notes.ico') root.geometry("640x500") root.tk.call("source", "C:/Users/Hero/Documents/Visual Studio code/My project/azure.tcl") root.tk.call("set_theme", "dark") global x x = 20 def change_theme(): if root.tk.call("ttk::style", "theme", "use") == "azure-dark": root.tk.call("set_theme", "light") else: root.tk.call("set_theme", "dark") def increase_font_size(): global x global f x = x + 1 print(x) text.config(font=(f, x)) def decrease_font_size(): global x global f x = x - 1 print(x) text.config(font=(f, x)) def pfonts(): font_page = Tk() font_page.title('Fonts') font_page.geometry('295x200') font_page.configure(bg='#292929') font_page.tk.call("source", "C:/Users/Hero/Documents/Visual Studio code/My project/azure.tcl") font_page.tk.call("set_theme", "dark") global f fentry1 = ttk.Entry(font_page, width=80) fentry1.pack() label1 = Label(font_page, text='Enter the font name you want', font=("Tekton Pro", 17), bg='#292929') label2 = Label(font_page, text='warning: caps lock sensitive', font=("Tekton Pro", 8), bg='#292929') label1.pack() label2.pack(side=BOTTOM) def change_font(): global f f = fentry1.get() text.config(font=(f, x)) print(f) bbfont = ttk.Button(font_page, text='Confirm font', command=change_font) bbfont.pack(side=BOTTOM) def killer(): print("killing the instence") root.destroy() def savefile(): global filename filename = filedialog.asksaveasfilename(initialdir='/', title='Save File', filetypes=(('Text Files', '*.txt'), ('All Files', '*.*'))) textContent = text.get("1.0",END) textContent = str(textContent) myfile = open(filename, "w+") myfile.write(textContent) style=ttk.Style() style.theme_use('azure-dark') style.configure("Vertical.TScrollbar", background="grey", bordercolor="black", arrowcolor="white") barframe = Frame() barframe.pack(side=BOTTOM) bfonts = ttk.Button(barframe, text='Fonts', style='Accent.TButton', command=pfonts) open = ttk.Button(barframe, text='Open', style='Accent.TButton') save = ttk.Button(barframe, text='Save', style='Accent.TButton', command=savefile) close = ttk.Button(barframe, text='Close', style='Accent.TButton', command=killer) #fonts = ttk.Entry(barframe, width=10) calculater = ttk.Button(barframe, text='Calculater', width=10, style='Accent.TButton') calander = ttk.Button(barframe, text='Calander', width=10, style='Accent.TButton') increase_size = ttk.Button(barframe, text='+', width=2, command=increase_font_size) decrease_size = ttk.Button(barframe, text='-', width=2, command=decrease_font_size) bfonts.grid(row=0, column=0) open.grid(row=0, column=1) save.grid(row=0, column=2) close.grid(row=0, column=3) #fonts.grid(row=0, column=1) calculater.grid(row=0, column=4) calander.grid(row=0, column=5) increase_size.grid(row=0, column=6) decrease_size.grid(row=0, column=7) scroll = ttk.Scrollbar(root, orient='vertical') scroll.pack(side=RIGHT, fill='y') text=Text(root, font=("Georgia", x), yscrollcommand=scroll.set, bg='#292929', height=1, width=1) scroll.config(command=text.yview) text.pack(fill=BOTH, expand=1) root.mainloop()
报错堆栈信息:
Traceback (most recent call last): File "C:\Users\Hero\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__ return self.func(*args) File "c:\Users\Hero\Documents\Visual Studio code\My project\MainFrame.py", line 82, in savefile myfile = open(filename, "w+") TypeError: 'Button' object is not callable
解决方案
错误原因
代码存在变量名冲突:你将Python内置的文件打开函数open(),覆盖成了一个Button对象——定义Open按钮时使用了open作为变量名:
open = ttk.Button(barframe, text='Open', style='Accent.TButton')
当savefile函数调用open(filename, "w+")时,Python会认为你要调用这个Button对象,而非内置的文件操作函数,因此抛出'Button' object is not callable错误。
修复步骤
将Open按钮的变量名改为非内置函数名(比如open_btn),并同步修改后续的布局代码:
- 修改Button定义:
open_btn = ttk.Button(barframe, text='Open', style='Accent.TButton')
- 修改grid布局代码:
open_btn.grid(row=0, column=1)
额外优化建议
保存文件时添加用户取消操作的判断,避免空路径报错,同时使用with语句自动管理文件资源:
def savefile(): filename = filedialog.asksaveasfilename(initialdir='/', title='Save File', filetypes=(('Text Files', '*.txt'), ('All Files', '*.*'))) if filename: # 检查用户是否选择了文件 textContent = text.get("1.0", END) with open(filename, "w+") as myfile: # 自动关闭文件 myfile.write(textContent)
内容的提问来源于stack exchange,提问作者Real Swat
相关产品推荐
相关产品推荐

