107k views
5 votes
Write a GUI-based program that allows the user to open, edit, and save text files. The GUI should include a labeled entry field for the filename and multi-line text widget for the text of the file. The user should be able to scroll through the text by manipulating a vertical scrollbar. Include command buttons labeled Open, Save, and New that allow the user to open, save and create new files. The New command should then clear the text widget and the entry widget.

1 Answer

6 votes

Final answer:

This question is about creating a GUI-based program in Python to open, edit, and save text files.

Step-by-step explanation:

Creating a GUI-based program typically requires a specific programming language and GUI toolkit. In this example, I'll provide a simple Python program using the Tkinter library to create a basic text editor with the specified features. Make sure you have Python installed on your system.

import tkinter as tk

from tkinter import filedialog

class TextEditorApp:

def __init__(self, root):

self.root = root

self.root.title("Text Editor")

# File-related variables

self.filename = tk.StringVar()

self.text_content = tk.StringVar()

# Entry field for the filename

self.filename_label = tk.Label(root, text="Filename:")

self.filename_entry = tk.Entry(root, textvariable=self.filename, width=30)

self.filename_label.grid(row=0, column=0, padx=5, pady=5)

self.filename_entry.grid(row=0, column=1, padx=5, pady=5)

# Text widget for the content of the file

self.text_widget = tk.Text(root, wrap="word", height=15, width=40)

self.text_widget.grid(row=1, column=0, columnspan=2, padx=5, pady=5)

# Vertical scrollbar for the text widget

self.scrollbar = tk.Scrollbar(root, command=self.text_widget.yview)

self.scrollbar.grid(row=1, column=2, sticky="ns")

self.text_widget.config(yscrollcommand=self.scrollbar.set)

# Command buttons

self.open_button = tk.Button(root, text="Open", command=self.open_file)

self.save_button = tk.Button(root, text="Save", command=self.save_file)

self.new_button = tk.Button(root, text="New", command=self.new_file)

self.open_button.grid(row=2, column=0, padx=5, pady=5)

self.save_button.grid(row=2, column=1, padx=5, pady=5)

self.new_button.grid(row=2, column=2, padx=5, pady=5)

def open_file(self):

file_path = filedialog.askopenfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])

if file_path:

with open(file_path, "r") as file:

content = file.read()

self.filename.set(file_path)

self.text_widget.d-elete(1.0, tk.END)

self.text_widget.insert(tk.END, content)

def save_file(self):

file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])

if file_path:

with open(file_path, "w") as file:

file.write(self.text_widget.get("1.0", tk.END))

self.filename.set(file_path)

def new_file(self):

self.filename.set("")

self.text_widget.d-elete(1.0, tk.END)

if __name__ == "__main__":

root = tk.T-k()

app = TextEditorApp(root)

root.mainloop()

This simple text editor uses Tkinter to create a basic GUI with an entry field, a text widget, and buttons for opening, saving, and creating new files. Copy and paste the code into a Python file (e.g., text_editor.py) and run it using the command python text_editor.py in the terminal.

User Matt Kocaj
by
5.2k points