In this video I’ll show you how to save data from your Tkinter app to a Dat File instead of a database!

Databases are complicated! Wouldn’t it be easier to just save your basic data to a single file that you can open in your app whenever you want?

The Pickle Module allows us to save to a dat file easily, and then load the data from the dat file just as easily. In this video we’ll look at saving and loading data to a dat file from both a Tkinter Text Widget as well as a Tkinter Listbox.

Python Code: save_dat.py
(Github Code)

from tkinter import *
import pickle

root = Tk()
root.title('Codemy.com - Save To Dat File!')
root.iconbitmap('c:/gui/codemy.ico')
root.geometry("500x400")

# Creating a list of sizes
sizes = [
	"Small",
	"Medium",
	"Large"
]

#my_text = Text(root, width=40, height=10)
#my_text.pack(pady=20)
my_list = Listbox(root)
my_list.pack(pady=20)

for item in sizes:
	my_list.insert(END, item)


def save_file():
	# Grab the stuff from our text box
	stuff = my_list.get(0, END)

	# Define a filename
	filename = "data/dat_stuff"

	#Open the file
	output_file = open(filename, 'wb')

	# Actually add the data to the file
	pickle.dump(stuff, output_file)


def open_file():
	# Define a filename
	filename = "data/dat_stuff"

	#Open the file
	input_file = open(filename, 'rb')

	# Load the data from the file into a variable
	stuff = pickle.load(input_file)	

	# Output to list box
	for item in stuff:
		my_list.insert(END, item)
	
	print(stuff)

# Delete from list box
def delete_items():
	my_list.delete(0, END)


my_button1 = Button(root, text="Save File", command=save_file)
my_button2 = Button(root, text="Open File", command=open_file)
my_button3 = Button(root, text="Delete Items", command=delete_items)

my_button1.pack(pady=20)
my_button2.pack(pady=20)
my_button3.pack(pady=20)

root.mainloop()

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of...