So I want to make something like Notepad, but I want to open .txt
files only.
I don't know how to do this and didn't found anything on the Internet.
My code:
import tkinter as tk # Import Tkinter
from tkinter.filedialog import askopenfilename # Import dialog box (to ask for file directory)
window = tk.Tk() # Create window
window.wm_geometry("600x600") # Set geometry
window.title("Test") # Rename
class FileOperations: # Class for file operations
def open_file(self, path): # File open function
file_opened = open(path, "r") # Open
file_contentment = file_opened.read() # Read
return file_contentment # Return
file_ops = FileOperations() # Assign variable to a class
newfile_button = tk.Button(master=window, text="New", width=10, height=1, font=("Arial", 10)) # Not done yet, do not mention it
newfile_button.grid(column=0, row=0) # Grid
savefile_button = tk.Button(master=window, text="Save File", width=10, height=1, font=("Arial", 10)) # Not done yet, do not mention it
savefile_button.grid(column=1, row=0) # Grid
openfile_button = tk.Button(master=window, text="Open File", width=10, height=1, font=("Arial", 10),
command=lambda: file_ops.open_file(askopenfilename())) # Fix it?
openfile_button.grid(column=2, row=0) # Grid
tk.mainloop() # Mainloop
Any suggestions are helpful.
CodePudding user response:
Just use param filetypes
like this
askopenfilename(filetypes=(("Text Files", "*.txt"),))