[英]With Tkinter trying to open a text file in Text() widget
伙計們,我試圖在 Text() 小部件中打開一個文本文件。問題是當我單擊打開文件時,我得到文件對話框,當我 select 一個文件並單擊打開時,我得到 TypeError。請告知。謝謝。 這是相關代碼
open = PhotoImage(file='ico/open.gif')
filemenu.add_command(label="Open",accelerator='Ctrl + O',compound='left',image=open,command=open_file)
file_name = None
def open_file(event=None):
input_file_name = tkinter.filedialog.askopenfilename(defaultextension=".txt",filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if input_file_name:
global file_name
file_name = input_file_name
root.title('{} - {}'.format(os.path.basename(file_name), PROGRAM_NAME))
content.delete(1.0, END)
with open(file_name) as _file:
content.insert(1.0, _file.read())
我得到這個錯誤
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Crystal\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\Crystal\Desktop\New folder\text_ed\ui.py", line 61, in open_file
with open(file_name) as _file:
TypeError: 'PhotoImage' object is not callable
錯誤告訴你問題所在。 您已經用圖像替換了內置的open
語句。 將您的圖像命名為其他名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.