i use python 3xx

from tkinter import *
from tkinter import ttk

i couldnt install PIL on my computer so I used Pillow. Hope you dont run into any problems

import datetime
import pickle

init the variables here since i dont use objects in your code

name = ""
course = ""
reference_number = ""
email = ""
timestamp = ""
room_number = ""
facebook = ""

data = {}

now = datetime.datetime.now()
filewin = Tk()
style = ttk.Style()

begin

style.configure("TLabel", foreground="black", font = 'Courier 10')
ttk.Style().configure("TButton", padding=3, relief="flat", background="#ccc",font = 'Courier 10')

filewin.title("Queen's Hall Data Manager",)
filewin.geometry("900x500+500+250")

label = ttk.Label(filewin, text = "Please Enter Your Info in the boxes below",foreground='blue')
label.place(x= 100, y=25)

notebook = ttk.Notebook(filewin)
notebook.place(x = 100, y = 50, width = 450)

frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
frame3 = ttk.Frame(notebook)
notebook.add(frame1, text = 'ADD USER')
notebook.add(frame2, text = 'DISPLAY')
notebook.add(frame3, text = 'SEARCH')

##########################################################################FRAME 1

this is were the person enters the details into the GUI
name Entry

label = ttk.Label(frame1, text="Full Name :", foreground='blue',)

label.grid(row=1, column=1,sticky = 'W', padx = 15, pady = 10)
entry_box1= ttk.Entry(frame1, width = 35,)
entry_box1.place(x= 100, y = 10)

course Entry

label = ttk.Label(frame1, text="Course :",foreground='blue')
label.grid(row=2, column=1,sticky = 'W', padx = 15, pady = 10)
entry_box2= ttk.Entry(frame1, width = 35,)
entry_box2.place(x= 100, y = 48)

Reference Entry

label = ttk.Label(frame1, text="Ref Num :",foreground='blue')
label.grid(row=3, column=1,sticky = 'W', padx = 15, pady = 10)
entry_box3= ttk.Entry(frame1, width = 35,)
entry_box3.place(x= 100, y = 87)

Room Entry

label = ttk.Label(frame1, text="Room :",foreground='blue')
label.grid(row=4, column=1,sticky = 'W', padx = 15, pady = 10)
entry_box4= ttk.Entry(frame1, width = 35,)
entry_box4.place(x= 100, y = 126)

main or annex

ticked_main = BooleanVar()
ticked_annex = BooleanVar()

main = ttk.Checkbutton(frame1, text = "Main", variable = ticked_main)
annex = ttk.Checkbutton(frame1, text = "Annex", variable = ticked_annex)

facebook Entry

label = ttk.Label(frame1, text="Facebook :",foreground='blue')
label.grid(row=5, column=1,sticky = 'W', padx = 15, pady = 10)
entry_box5= ttk.Entry(frame1, width = 35,)
entry_box5.place(x= 100, y = 167)

email Entry

label = ttk.Label(frame1, text="Email :",foreground='blue')
label.grid(row=6, column=1,sticky = 'W', padx = 15, pady = 10)
entry_box6= ttk.Entry(frame1, width = 35,)
entry_box6.place(x= 100, y = 207)

date of birth

label = ttk.Label(frame1, text="D.O.B :",foreground='blue')
label.grid(row=7, column=1,sticky = 'W', padx = 15, pady = 10)
entry_box7= ttk.Entry(frame1, width = 35,)
entry_box7.place(x= 100, y = 247)

Cell Phone

label = ttk.Label(frame1, text="Phone :",foreground='blue')
label.grid(row=8, column=1,sticky = 'W', padx = 15, pady = 10)
entry_box8= ttk.Entry(frame1, width = 35,)
entry_box8.place(x= 100, y = 287)

function for storing the given data into a text file
Function
for now I'm using only the few variables declared at the top of the code. Name and co.

def adduser():

name = "Name: " + entry_box1.get()
course = "Course: " + entry_box2.get()
reference_number = "Ref Number : " + entry_box3.get()
room_number = 'room_number : ' + entry_box4.get()
main = 'Main :' + str(ticked_main.get())
annex = 'Annex : '+ str(ticked_annex.get())
facebook = 'Facebook  = :' + entry_box5.get()
email = "Email : " + entry_box6.get()
timestamp = str(now)

data[name] = course, reference_number, room_number, main, annex, facebook,email, timestamp
addstudent = open ("data.txt", "a")

addstudent.write(str(data))
addstudent.close()
done!

def clear_entry():
entry_box1.delete(0, END)
entry_box2.delete(0, END)
entry_box3.delete(0, END)
entry_box4.delete(0, END)
entry_box5.delete(0, END)
entry_box6.delete(0, END)
entry_box7.delete(0, END)
entry_box8.delete(0, END)

def exitmode():

    m = Message(filewin, text = "Are you Tired of Typing???", foreground='blue')
    filewin.destroy()
save button

b = ttk.Button(frame1, text="Add New Royal", command=adduser)
b.grid(row=9, column=1,sticky = 'W', padx = 15, pady = 10)

clear

r = ttk.Button(frame1, text="Show Logo", state='disabled')
r.grid(row=9, column=2,sticky = 'W', padx = 10, pady = 10)

exit button

e = ttk.Button(frame1, text="Exit", command=exitmode)
e.grid(row=9, column=2,sticky = 'W', padx = 10, pady = 10)

CLEAR button

c = ttk.Button(frame1, text="CLEAR", command=clear_entry)
c.grid(row=9, column=3,sticky = 'W', padx = 10, pady = 10)

#######################################FRAME 2

I believe this should be here so you can call it later in the read function

s_b = ttk.Entry(frame2, width = 25,)
s_b.place(x= 130, y = 20)

tt = Text(frame2,width = 50,height = 13)
tt.grid(row=2, column=1, padx = 15, pady = 1)

this is where my problem is,,,i cant use this fuction to read from the text file and display the corresponding results in the text widget

def read():

    query = open ("data.txt", "r").read()
    data = eval(query)


name = "Name: " + s_b.get()
if name in data:
    tt.insert(0.,data[name])
    #print(data[name])

s_earch = ttk.Label(frame2, text="SEARCH BY NAME :",foreground='blue')
s_earch.grid(row=1, column=1,sticky = 'W', padx = 15, pady = 20)

frame2b = ttk.Button(frame2, text="SEARCH", command=read)
frame2b.place(x = 300, y= 10)

#

ss3 = ttk.Label(frame3, text="ROOM :",foreground='blue')
ss3.grid(row=1, column=1,sticky = 'W', padx = 15, pady = 20)

sb3= ttk.Entry(frame3, width = 25,)
sb3.place(x= 130, y = 20)

fram3b = ttk.Button(frame3, text="DISPLAY")
fram3b.place(x = 300, y= 10)

ff3 = Text(frame3,width = 50,height = 18)
ff3.grid(row=2, column=1, padx = 15, pady = 1)

filewin.mainloop()

Dani AI

Generated

Short diagnosis: the file currently ends up with multiple stringified Python dicts appended one after another, so after the first write the file is a valid dict literal but after subsequent writes it becomes something like
{...}{...} which is not valid Python — eval() then raises a SyntaxError. eval() on disk data is also a security risk. Also a Text widget should be updated with a proper index (for example '1.0' or END) and usually cleared first with tt.delete('1.0', END).

Practical fixes (pick one):

  • Use a stable serialization format (recommended): store each record as JSON on its own line (JSONL). That makes appending safe and reading simple. Example pattern:
import json

def add_user(record, filename='data.jsonl'):
    with open(filename, 'a', encoding='utf-8') as f:
        f.write(json.dumps(record) + '\n')

def find_by_name(name, filename='data.jsonl'):
    name = name.strip().lower()
    with open(filename, 'r', encoding='utf-8') as f:
        for line in f:
            rec = json.loads(line)
            if rec.get('name','').lower() == name:
                return rec
    return None

When displaying, format the returned record into a human string, clear the Text widget, then tt.insert('1.0', formatted_string).

  • Or keep one JSON object for all users: read the whole file into a dict, update the dict, then overwrite the file with json.dump(...). Do not open with 'a' and write str(dict).

Extra tips: use with open(...) to guarantee file closure, catch FileNotFoundError for empty files, strip and normalize names for case-insensitive search, and avoid globals named data that shadow things. As suggested, ensure the search function is actually invoked by the button; as and noted, post code as formatted code to make debugging easier.

Recommended Answers

All 6 Replies

Please post your code as code.

pls how?

this is where my problem is,,,i cant use this fuction to read from the text file and display the corresponding results in the text widget

I don't understand what you mean. Insert is the correct way to insert text into a Text widget.

Edit: Ah, you don't call the read() function (bad name as it shadows Python read functions). You have to click a button, or something, after the text is entered, which will execute the read() function and start at "This example creates an Entry widget, and a Button that prints the current contents:"

You can also use a function to create the widgets and simplify the code. Note that each entry box is appended to the list so clearing them is just partsing the list. You would get() an entry in same way you do now except you would use list_of_entries[2].get() for entry_box3, etc. Finally, you can use grid() only and

## example only - modify and correct to fit
def create_row(frame, lit, row_in, y_in):
    label = ttk.Label(frame, text=lit, foreground='blue',)
    label.grid(row=row_in, column=1,sticky = 'W', padx = 15, pady = 10)
    entry_box= ttk.Entry(frame, width = 35,)
    ##entry_box.place(x= 100, y = y_in)
    entry_box.grid(row=row_in, column=2)

list_of_entries=[]
row = 1
y = 10
for lit in ["Full Name: ", "Course :", "Ref Num :"]:
    entry_box=create_row(frame1, lit, row, y)
    list_of_entries.append(entry_box)
    row += 1
    y += 39

def clear_entry():
    for entry_box in list_of_entries:
        entry_box.delete(0, END)

I think you highlightthe code and press the Code button on the grey bar on top.

thanks y'all.i have been able to work my way around the code,,i can save the entries in a text widget as usual and i have written a read function that can search for the info of a person by entring the name in an entry widget and displays in a text widget,,but this time it can only display only one name,,it gives an error when i type another name to search for it in the text file...here is the code.
this is the write function

def adduser():
    #addstudent.write("\n")
    name = "Name: " + entry_box1.get()
    course = "Course: " + entry_box2.get()
    reference_number = "Ref Number : " + entry_box3.get()

    email = "Email : " + entry_box6.get()
    timestamp = str(now)

    data[name] = course, reference_number, email, timestamp
    addstudent = open ("data.txt", "a")

    addstudent.write(str(data))
    addstudent.close()

and this is the read function

def read():
    query = open ("data.txt", "r").read()
    # run eval on the query, dunno if there is a safer method on python3
    data = eval(query)
    # since up at the write function the data is being stored in the format "Name: " + actual_name
    # we'll use that same method to search
    name = "Name: " + s_b.get()
    if name in data:
        tt.insert(0.,data[name])
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.