Tkinter试图在Text()小部件中打开文本文件

问题描述 投票:-1回答:1

伙计,我试图在Text()小部件中打开文本文件。问题是当我单击“打开文件”时,出现文件对话框,而当我选择一个文件并单击“打开”时,出现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
python tkinter openfiledialog
1个回答
0
投票

错误告诉您问题所在。您已用图像替换了内置的open语句。为您的图片命名。

© www.soinside.com 2019 - 2024. All rights reserved.