Python Tkinter notepad save and save as function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JakeFrom98
    New Member
    • Apr 2018
    • 1

    Python Tkinter notepad save and save as function

    Hello so I'm trying to make a basic notepad in tkinter but I'm having issues with the save function. So if a files opens in the notepad I'd like to be able to press save and it will save the data without a filedialog box popping up. I've taken the liberty of shortening the code. This is my first time using this website so I hope the format is correct.

    from tkinter import *
    import os
    import tkinter as tk
    import tkinter.scrolle dtext as ScrolledText
    import subprocess
    from tkinter import Tk, scrolledtext, Menu, filedialog, messagebox, Text, simpledialog, filedialog
    from tkinter.scrolle dtext import ScrolledText

    def new():
    if len(notepad.get ('1.0', END+'-1c')) > 0:
    if messagebox.asky esno("Save?", "Do you wish to save?"):
    save()
    notepad.delete( '1.0', END)

    else:
    notepad.delete( '1.0', END)

    def save():

    def save_as():
    file = filedialog.asks aveasfile(mode= 'w', defaultextensio n='.py', filetypes=(("Py thon file", ".py"), ("All files", "*.*")))
    if file !=None:
    data = notepad.get('1. 0', END+'-1c')
    file.write(data )
    file.close()


    root = Tk()
    menu = Menu(root)
    root.config(men u=menu)

    root.title('Wri tten in Python')
    root.minsize(wi dth=100, height=100)
    root.geometry(' 800x500+350+150 ') #Height, Width, X, Y coordinates of the program

    #NotePad
    notepad = Notepad(root, width=1000, height=100) #Height and width of notepad
    notepad.pack()

    subMenu = Menu(menu)
    menu.add_cascad e(label="File", menu=subMenu)
    subMenu.add_com mand(label="New ", command=new)
    subMenu.add_com mand(label="Ope n...", command=open)
    subMenu.add_com mand(label="Sav e", command=save)
    subMenu.add_com mand(label="Sav e As...", command=save_as )
    subMenu.add_sep arator()
    subMenu.add_com mand(label="Exi t", command=exit)
    root.mainloop()
Working...