Use Python CRUD TKinter Multi-Window GUI Application to manage JSON data.

preview_player
Показать описание
How to use the Label control
How to select the parent control
How to use the Entry control

How to use the Treeview Control
How to use heading and column
How to load a Treview Control with JSON
How to load JSON from your computer
How to use JSON.load
How to use JSON.dump
How to save JSON to your computer
How to remove all children from a Treeview Control
How to insert data into a Treeview Control
How to populate a dictionary to load a treeview control

Video uses: List, Dictionary, Tuple, Treeview, Json, uuid

#softwareNuggets, #pythonTK, #tkinter, #LoadJson, @SoftwareNuggets, #share_to_be_better
Рекомендации по теме
Комментарии
Автор

Your explanation is very clear and I fully understand what I just saw, to simplify the uuid object it is not practical for me. I try to replace it with a simple incremental similar to:
Id=len(dictionary) + 1
Thank you very much.

creyco
Автор

THAT IS MY KEYBOORD WAS CHANGING A TO Q AND Z TO W IWS DOWNLOADING YOUR VIDEO FOR TO STUDY IT AND I'M BEGINNER

abteyeb
Автор

Sir,
I am doing a similar project.. The json file i am dealing with has 14 columns... Can you please assist me by giving advice on how can I make my tree view scrollable both vertically and horizontally?

avishek
Автор

TEACHER DO YOU HAVE A VIDEO INVENTORY SYSTEM IN PYTHON ?

abteyeb
Автор

From your code, why do I got string indices must be integers?
That
Key["Id"]
Type error: string indices must be integers

hadadfadilah
Автор

can you advise, how can we have the table ordered based on a column.

georgelza
Автор

Source Code for application

# written by Software Nuggets
# Free to use this source code for any reason/project

from tkinter import *
from tkinter import ttk
import uuid
import json

global my_data_list
global currentRowIndex

my_data_list = []

primary = Tk();
primary.geometry("768x500")
primary.title('Manager Users using Child Window')

def make_new_record():
blankTuple = ('', '', '', '')
open_popup('add', blankTuple, primary)

btnNewRecord=Button(primary, text="Add New", bg="#34d2eb",
padx=2, pady=3, command=lambda:make_new_record())
btnNewRecord.grid(row=0, column=0, sticky="w")

trv =ttk.Treeview(primary, columns=(1, 2, 3, 4, 5), show="headings", height="16")
trv.grid(row=1, column=0, rowspan=16, columnspan=5)

trv.heading(1, text="Action", anchor="w")
trv.heading(2, text="ID", anchor="center")
trv.heading(3, text="First Name", anchor="center")
trv.heading(4, text="Last Name", anchor="center")
trv.heading(5, text="Cell Phone", anchor="center")

trv.column("#1", anchor="w", width=100, stretch=True)
trv.column("#2", anchor="w", width=270, stretch=True)
trv.column("#3", anchor="w", width=140, stretch=False)
trv.column("#4", anchor="w", width=140, stretch=False)
trv.column("#5", anchor="w", width=140, stretch=False)


def load_json_from_file():
global my_data_list
with open("c:\\tmp\\amigos.json", "r") as file_handler:
my_data_list = json.load(file_handler)
file_handler.close
print('file has been read and closed')

def remove_all_data_from_trv():
for item in trv.get_children():
trv.delete(item)

def load_trv_with_json():
global my_data_list

remove_all_data_from_trv()

rowIndex=1

for key in my_data_list:
guid_value = key["id"]
first_name = key["first_name"]
last_name = key["last_name"]
cell_phone = key["cell_phone"]

trv.insert('', index='end', iid=rowIndex, text="",
values=('edit', guid_value, first_name, last_name, cell_phone))
rowIndex=rowIndex+1

def
global trv
currentRowIndex = trv.selection()[0]
lastTuple = (trv.item(currentRowIndex, 'values'))
open_popup('edit', lastTuple, primary)

def open_popup(_mode, _tuple, primary):
global myname
child = Toplevel(primary);
child.geometry("768x500");
child.title('Child Window')
child.grab_set(); #allow it to receive events
#and prevent users from interacting
#with the main window



load_form = True;
input_frame = LabelFrame(child, text='Enter New Record',
bg="lightgray",
font=('Consolas', 14))

input_frame.grid(row=0, rowspan=6, column=0)

l1 = Label(input_frame, text="ID", width=25, height=2, anchor="w", relief="ridge", font=('Consolas', 14))
l2 = Label(input_frame, text="First Name", width=25, height=2, anchor="w", relief="ridge", font=('Consolas', 14))
l3 = Label(input_frame, text="Last Name", width=25, height=2, anchor="w", relief="ridge", font=('Consolas', 14))
l4 = Label(input_frame, text="Cell Phone", width=25, height=2, anchor="w", relief="ridge", font=('Consolas', 14))
l1.grid(column=0, row=0, padx=1, pady=0);
l2.grid(column=0, row=1, padx=1, pady=0);
l3.grid(column=0, row=2, padx=1, pady=0);
l4.grid(column=0, row=3, padx=1, pady=0);

id_value = StringVar()
id_value.set(uuid.uuid4())

crm_id=Label(input_frame, anchor="w", height=1,
relief="ridge", textvariable=id_value, font=('Consolas', 14))
crm_id.grid(row=0, column=1, padx=20)

crm_fn =Entry(input_frame, width=30, borderwidth=2, fg="black", font=('Consolas', 14))
crm_fn.grid(row=1, column=1)

crm_ln =Entry(input_frame, width=30, borderwidth=2, fg="black", font=('Consolas', 14))
crm_ln.grid(row=2, column=1)

crm_cellphone=Entry(input_frame, width=30, borderwidth=2, fg="black", font=('Consolas', 14))
crm_cellphone.grid(row=3, column=1)


btnAdd=Button(input_frame, text="Save", padx=5, pady=10, command=lambda:determineAction())
btnAdd.grid(row=4, column=0)

btnDelete=Button(input_frame, text="Delete", padx=5, pady=10, command=lambda:delete_record())
btnDelete.grid(row=4, column=3)

btnCancel=Button(input_frame, text="Cancel", padx=5, pady=10, command=lambda:child_cancel())
btnCancel.grid(row=4, column=4)

load_form = False;

def delete_record():
guid_value = id_value.get()
first_name = crm_fn.get()
last_name = crm_ln.get()
cell_phone = crm_cellphone.get()
process_request('_DELETE_', guid_value, first_name, last_name, cell_phone)
reload_main_form()
child.grab_release();
child.destroy()
child.update()


def child_cancel():
child.grab_release();
child.destroy()
child.update()

def reload_main_form():
load_trv_with_json()

def
crm_fn.config(bg=new_color)
crm_ln.config(bg=new_color)



def add_entry():
guid_value = id_value.get()
first_name = crm_fn.get()
last_name = crm_ln.get()
cell_phone = crm_cellphone.get()

if len(first_name)==0:

return

process_request('_INSERT_', guid_value, first_name, last_name, cell_phone)

def update_entry():
guid_value = id_value.get()
first_name = crm_fn.get()
last_name = crm_ln.get()
cell_phone = crm_cellphone.get()

if len(first_name)==0:

return

process_request('_UPDATE_', guid_value, first_name, last_name, cell_phone)

def
if len(_tuple)==0:
return;

id_value.set(_tuple[1]);
crm_fn.delete(0, END)
crm_fn.insert(0, _tuple[2])
crm_ln.delete(0, END)
crm_ln.insert(0, _tuple[3])
crm_cellphone.delete(0, END)
crm_cellphone.insert(0, _tuple[4])

if _mode=='edit':


def process_request(command_type, guid_value, first_name, last_name, cell_phone):
global my_data_list
global dirty

dirty=True

if command_type == "_UPDATE_":
row =
if row >= 0:
dict = {"id":guid_value, "first_name":first_name,
"last_name":last_name, "cell_phone":cell_phone}
my_data_list[row]=dict

elif command_type == "_INSERT_":
dict = {"id":guid_value, "first_name":first_name,
"last_name":last_name, "cell_phone":cell_phone}
my_data_list.append(dict)

elif command_type == "_DELETE_":
row =
if row >= 0:
del my_data_list[row];

save_json_to_file();
clear_all_fields();

def
global my_data_list
row = 0
found = False

for rec in my_data_list:
if rec["id"] == guid_value:
found = True
break
row = row+1

if(found==True):
return(row)

return(-1)

def determineAction():
if load_form == False:
if _mode == "edit":
update_entry();
else:
add_entry();

reload_main_form()
child.grab_release();
child.destroy()
child.update()


def save_json_to_file():
global my_data_list
with open("c:\\tmp\\amigos.json", "w") as file_handler:
json.dump(my_data_list, file_handler, indent=4)
file_handler.close
print('file has been written to and closed')


def load_json_from_file():
global my_data_list
with open("c:\\tmp\\amigos.json", "r") as file_handler:
my_data_list = json.load(file_handler)
file_handler.close
print('file has been read and closed')


def clear_all_fields():
crm_fn.delete(0, END)
crm_ln.delete(0, END)
crm_cellphone.delete(0, END)
crm_id.configure(text="")
crm_fn.focus_set()
id_value.set(uuid.uuid4())


trv.bind("<ButtonRelease>", MouseButtonUpCallBack)
load_json_from_file()
load_trv_with_json()
primary.mainloop()

SoftwareNuggets
welcome to shbcf.ru