By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,044 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,044 IT Pros & Developers. It's quick & easy.

Python Tkinter notepad save and save as function

P: 1
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.scrolledtext as ScrolledText
import subprocess
from tkinter import Tk, scrolledtext, Menu, filedialog, messagebox, Text, simpledialog, filedialog
from tkinter.scrolledtext import ScrolledText

def new():
if len(notepad.get('1.0', END+'-1c')) > 0:
if messagebox.askyesno("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.asksaveasfile(mode='w', defaultextension='.py', filetypes=(("Python 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(menu=menu)

root.title('Written in Python')
root.minsize(width=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_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New", command=new)
subMenu.add_command(label="Open...", command=open)
subMenu.add_command(label="Save", command=save)
subMenu.add_command(label="Save As...", command=save_as)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=exit)
root.mainloop()
Apr 2 '18 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.