Python Forum
very newbie problem on text file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: very newbie problem on text file (/thread-41946.html)



very newbie problem on text file - zapad - Apr-12-2024

hello
I am a very beginner in python (2 months) and I am trying co complete a script that seems
very simple even for a beguinner , but I am giving up!
I am just willing to open a text file - the name is fixed- without using the filedialog tool .
So what is wrong in my code below Huh .

The error is at the end of the script .
many thanks!
--------------------------------------------------
import tkinter as tk
def open():
        filename = "sample1.txt"
        if filename:
           my_file = open(filename)
           textArea.insert(tk.END, my_file.read())
        elif not filename:
            messagebox.showinfo("Cancel", "You clicked Cancel")

root = tk.Tk()
root.title("Text try")

text_widget = tk.Text(root, wrap=tk.WORD)
text_widget.pack(padx=20, pady=20, fill="both", expand=True)

save_button = tk.Button(root, text="lire File", command=open)
save_button.pack(pady=10)

status_label = tk.Label(root, text="", padx=20, pady=10)
status_label.pack()
root.mainloop()

[i][b]'''   error message
  File "C:\Users\Zapad\AppData\Local\Programs\Python\Python312\A-GARDE-MODULES-SOURCE\zzoihj.py", line 6, in open
    my_file = open(filename)
              ^^^^^^^^^^^^^^
TypeError: open() takes 0 positional arguments but 1 was given '''[/b]
[/i]



RE: very newbie problem on text file - deanhystad - Apr-12-2024

You cannot define a function named "open". It replaces the "open" you try to use here:
my_file = open(filename)



RE: very newbie problem on text file - zapad - Apr-12-2024

hi deanhystad
thanks ! your post did the job!
tks & good evening