“Текстовый редактор Tkinter” Ответ

Текстовый редактор Tkinter

import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename

def open_file():
    """Open a file for editing."""
    filepath = askopenfilename(
        filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
    )
    if not filepath:
        return
    txt_edit.delete("1.0", tk.END)
    with open(filepath, mode="r", encoding="utf-8") as input_file:
        text = input_file.read()
        txt_edit.insert(tk.END, text)
    window.title(f"Simple Text Editor - {filepath}")

def save_file():
    """Save the current file as a new file."""
    filepath = asksaveasfilename(
        defaultextension=".txt",
        filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
    )
    if not filepath:
        return
    with open(filepath, mode="w", encoding="utf-8") as output_file:
        text = txt_edit.get("1.0", tk.END)
        output_file.write(text)
    window.title(f"Simple Text Editor - {filepath}")

window = tk.Tk()
window.title("Simple Text Editor")

window.rowconfigure(0, minsize=800, weight=1)
window.columnconfigure(1, minsize=800, weight=1)

txt_edit = tk.Text(window)
frm_buttons = tk.Frame(window, relief=tk.RAISED, bd=2)
btn_open = tk.Button(frm_buttons, text="Open", command=open_file)
btn_save = tk.Button(frm_buttons, text="Save As...", command=save_file)

btn_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5)
btn_save.grid(row=1, column=0, sticky="ew", padx=5)

frm_buttons.grid(row=0, column=0, sticky="ns")
txt_edit.grid(row=0, column=1, sticky="nsew")

window.mainloop()
Code Cat

Текстовый редактор Tkinter

import tkinter as tk
from tkinter import *


root = tk.Tk()
root.geometry("520x280")
root.resizable(0, 0)
root.title("Test Editor")


def make_menu(w):
    global the_menu
    the_menu = tk.Menu(w, tearoff=0)
    the_menu.add_command(label="Cut")
    the_menu.add_command(label="Copy")
    the_menu.add_command(label="Paste")

def printInput():
    paste = testEntry.get()
    #   1.0, "end-1c"   put this in the get method 
    lbl.config(text =paste)

def show_menu(e):
    w = e.widget
    the_menu.entryconfigure("Cut",
    command=lambda: w.event_generate("<<Cut>>"))

    the_menu.entryconfigure("Copy",
    command=lambda: w.event_generate("<<Copy>>"))

    the_menu.entryconfigure("Paste",
    command=lambda: w.event_generate("<<Paste>>"))

    the_menu.tk.call("tk_popup", the_menu, e.x_root, e.y_root)


printButton = tk.Button(root, text = "Print", command = printInput)

printButton.pack()


make_menu(root)

testEntry = tk.Entry(); testEntry.pack()
testEntry.bind_class("Entry", "<Button-3><ButtonRelease-3>", show_menu)


lbl = tk.Label(root, text = "")
lbl.pack()

root.mainloop()
Duncan Banner

Ответы похожие на “Текстовый редактор Tkinter”

Вопросы похожие на “Текстовый редактор Tkinter”

Больше похожих ответов на “Текстовый редактор Tkinter” по Python

Смотреть популярные ответы по языку

Смотреть другие языки программирования