-1

i used functions and tk frames / entry and i cant seem to get the value of the entry

i used a frame to put all my widgets. i cant seem to get the value of all my tk entry.

i want to be able to get the value, store it in the file same as the date and time and then be able to retrieve and print it as a label.

here's a snippet in my codes:

from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from PIL import Image, ImageTk
import uuid
import json
from pyglet import font
from tkcalendar import *
from datetime import *
from tktimepicker import SpinTimePickerModern
from tktimepicker import constants

#tk.resizable(height = None, width = None)

# set colours
bg_colour = "#141414"

# load custom fonts
font.add_file("fonts/Quicksand-Regular.ttf")
font_bold = font.load('Quicksand', bold=True)

font.add_file("fonts/Quicksand-Regular.ttf")
font_reg = font.load('Quicksand', bold=False)

def clear_widgets(frame):
    # select all frame widgets and delete them
    for widget in frame.winfo_children():
        widget.destroy()


def close():
   #win.destroy()
   root.quit()

        
def load_frame1():
    clear_widgets(frame2)
    # stack frame 1 above frame 2
    frame1.tkraise()
    # prevent widgets from modifying the frame
    frame1.pack_propagate(False)

    # create logo widget
    img = Image.open("assets/logo_mc.png")
    logo_img = ImageTk.PhotoImage(img)
    logo_widget = tk.Label(frame1, image=logo_img, bg=bg_colour)
    logo_widget.image = logo_img
    logo_widget.pack(pady=30)

    #img = tk.PhotoImage(file="assets/logo_mc.png")
    #w = tk.Label(frame1, image=img)
    #w.pack(pady=30)


    # create label widget for instructions
    tk.Label(
        frame1, 
        text="Be the MADDEST web developer ever!",
        bg=bg_colour,
        fg="#7d6dbb",
        font=(font_bold, 17)
        ).pack(pady=10)
    tk.Label(
        frame1, 
        text="Ready for an interview?",
        bg=bg_colour,
        fg="#7d6dbb",
        font=(font_reg, 15)
        ).pack()
    
    # create button widget 2
    tk.Button(
        frame1,
        text="SET AN APPOINTMENT NOW",
        font=(font_reg, 15),
        bg="#7d6dbb",
        fg="white",
        cursor="hand2",
        activebackground="#83d2c9",
        activeforeground="#141414",
        command=lambda:load_f2()
        ).pack(pady=30)

def load_f2():  
    clear_widgets(frame1)
    # stack frame 1 above frame 2
    frame2.tkraise()
    # prevent widgets from modifying the frame
    frame2.pack_propagate(True)


    def get_entry():
        print(e_name.get())
        print(e_email.get())
        print(e_age.get())
        print(e_city.get())
        print(e_num.get())
        #f = open("saved app.txt",'a')
        #f.write("Date: " + selected_date +  "\nTime: " + selected_time)
        #f.close()

    def save_app():
        answer = messagebox.askquestion(title='Checking...', message='Are you sure with your chosen date and time?')
        if (answer == 'yes'):
            check_date = cal.get_date()
            convert_datetime = datetime.strptime(check_date, '%m/%d/%y')

            while True:
                if((convert_datetime.weekday() == 5) or (convert_datetime.weekday() == 6)):
                    #error, wont continue
                    messagebox.showerror(title='Error!', message='We only work on weekdays! Kindly select a date accordingly.')
                    break
                else:
                    get_entry()
                    selected_time = cb_time.get()
                    selected_date = check_date

                    f = open("saved app.txt",'a')
                    f.write("Date: " + selected_date +  "\nTime: " + selected_time)
                    f.close()

                    messagebox.showinfo("Saved", "Appointment saved successfully!")
                    break
        else:
            messagebox.showinfo(title='Try again', message='Choose your appointment wisely!')

    def print_app():
        p = tk.Tk()
        p.geometry('400x500')
        p.title('Print Appointment')
        p.resizable(False, False)

        r = open("saved app.txt", "r")
        results = r.readlines()
        

        l2 = tk.Label(
                p, 
                text="Kindly Screenshot this information.",
                font=("font_reg", 10)
                )
        l2.pack()

        tk.Label(p, text=str(results)).pack()

    tk.Label(
        frame2, 
        text="SET YOUR APPOINTMENT",
        bg=bg_colour,
        fg="#8675CD",
        font=("font_reg", 16)
        ).place(x=500, y=30)
    tk.Label(
        frame2, 
        text="Enter your details.",
        bg=bg_colour,
        fg="white",
        font=("font_reg", 10)
        ).place(x=350, y=85)
    tk.Label(
        frame2, 
        text="Name: ",
        bg=bg_colour,
        fg="white",
        font=("font_reg", 10)
        ).place(x=350, y=120)
    e_name = tk.Entry(
        frame2,
        bg="#d6d7ed",
        textvariable=e_name
        ).place(x=430, y=120)
    tk.Label(
        frame2, 
        text="Email: ",
        bg=bg_colour,
        fg="white",
        font=("font_reg", 10)
        ).place(x=350, y=150)
    e_email = tk.Entry(
        frame2,
        bg="#d6d7ed",
        textvariable=e_email
        ).place(x=430, y=150)
    tk.Label(
        frame2, 
        text="Age: ",
        bg=bg_colour,
        fg="white",
        font=("font_reg", 10)
        ).place(x=350, y=180)
    e_age = tk.Entry(
        frame2,
        bg="#d6d7ed",
        textvariable=e_age
        ).place(x=430, y=180)
    tk.Label(
        frame2, 
        text="City: ",
        bg=bg_colour,
        fg="white",
        font=("font_reg", 10)
        ).place(x=350, y=210)
    e_city = tk.Entry(
        frame2,
        bg="#d6d7ed",
        textvariable=e_city
        ).place(x=430, y=210)
    tk.Label(
        frame2, 
        text="Phone No.: ",
        bg=bg_colour,
        fg="white",
        font=("font_reg", 10)
        ).place(x=350, y=240)
    e_num = tk.Entry(
        frame2,
        bg="#d6d7ed",
        textvariable=e_num
        ).place(x=430, y=240)

    #cal_f = LabelFrame(frame2,text="Select a date.", foreground="white", bg=bg_colour)
    #cal_f.place(x=550, y=100)

    #style = ttk.Style(frame2)
    #style.theme_use('clam')

    tk.Label(
        frame2, 
        text="Select a date.",
        bg=bg_colour,
        fg="white",
        font=("font_reg", 10)
        ).place(x=700, y=85)

    cal = Calendar(frame2, selectmode="day", background="#404040", disabledbackground="#gray", bordercolor="#d6d7ed", 
               headersbackground="#7d6dbb", normalbackground="#2e2d2d", foreground='#d6d7ed', 
               normalforeground='#d6d7ed', headersforeground='#d6d7ed')
    cal.place(x=700, y=110)

    #time_picker = AnalogPicker(frame2)
    #time_picker.pack()

    #theme = AnalogThemes(time_picker)
    #theme.setDracula()

    tk.Label(
        frame2, 
        text="Select a time.",
        bg=bg_colour,
        fg="white",
        font=("font_reg", 10)
        ).place(x=700, y=320)
    
    # define the style for combobox widget
    style= ttk.Style()
    style.theme_use('clam')
    style.configure("TCombobox", fieldbackground= "#7d6dbb", background= "white")
    
    n = tk.StringVar()
    cb_time = ttk.Combobox(frame2, width = 20, textvariable = n, text="8:00 A.M.", state='readonly') 

    # adding combobox drop down list
    cb_time['values'] = ("8:00 A.M.",
                          "9:00 A.M.",
                          "10:00 A.M.",
                          "11:00 A.M.",
                          "13:00 P.M.",
                          "14:00 P.M.",
                          "15:00 P.M.",
                          "16:00 P.M.",
                          "17:00 P.M.",)
    cb_time.place(x=700, y=350)
    cb_time.set("8:00 A.M.")

    tk.Button(
        frame2,
        text="SAVE",
        font=(font_reg, 10),
        bg="#7d6dbb",
        fg="white",
        cursor="hand2",
        activebackground="#83d2c9",
        activeforeground="#141414",
        command=lambda:save_app()
        ).place(x=555, y=450)
    tk.Button(
        frame2,
        text="PRINT",
        font=(font_reg, 10),
        bg="#7d6dbb",
        fg="white",
        cursor="hand2",
        activebackground="#83d2c9",
        activeforeground="#141414",
        command=lambda:print_app()
        ).place(x=610, y=450)
    tk.Button(
        frame2,
        text="BACK",
        font=(font_reg, 10),
        bg="#7d6dbb",
        fg="white",
        cursor="hand2",
        activebackground="#83d2c9",
        activeforeground="#141414",
        command=lambda:load_frame1()
        ).place(x=666, y=450)
    tk.Button(
        frame2,
        text="DONE",
        font=(font_reg, 10),
        bg="#7d6dbb",
        fg="white",
        cursor="hand2",
        activebackground="#83d2c9",
        activeforeground="#141414",
        command=lambda:close()
        ).place(x=720, y=450)
    
    




        
    
# initiallize app with basic settings
root = tk.Tk()
root.title("Madcat Agency | Hiring Now")
root.resizable(False, False)
 
# create a frame widgets
frame1 = tk.Frame(root, width=1250, height=690, bg=bg_colour)
frame2 = tk.Frame(root, bg=bg_colour)
frame3 = tk.Frame(root, bg=bg_colour)

# place frame widgets in window
for frame in (frame1, frame2):
    frame.grid(row=0, column=0, sticky="nesw")

# load the first frame
load_frame1()


# run app
root.mainloop()

1 Answers1

0

This is because you have a callback function ...

    def get_entry():
        print(e_name.get())
        print(e_email.get())
        print(e_age.get())
        print(e_city.get())
        print(e_num.get())

that is trying to get() from a bunch of variables that are None. The variables e_name, e_mail e_num and e_age are all NoneType and therefore do not have a get method.

The reason all of those variables are None is because that is what the .place() method returns which you are calling on the lines where you declare those variables.

    e_name = tk.Entry(
        frame2,
        bg="#d6d7ed",
        textvariable=e_name
        ).place(x=430, y=120)
    ...
    e_email = tk.Entry(
        frame2,
        bg="#d6d7ed",
        textvariable=e_email
        ).place(x=430, y=150)
    ...
    e_age = tk.Entry(
        frame2,
        bg="#d6d7ed",
        textvariable=e_age
        ).place(x=430, y=180)
    ...
    e_city = tk.Entry(
        frame2,
        bg="#d6d7ed",
        textvariable=e_city
        ).place(x=430, y=210)
    ...
    e_num = tk.Entry(
        frame2,
        bg="#d6d7ed",
        textvariable=e_num
        ).place(x=430, y=240)

You are also referencing a variable before it has been defined which would also raise an exception.

What you can do is split each of the declarations above into smaller individual operations.

For example:

    e_name = StringVar()
    e_email = StringVar()
    e_age = StringVar()
    e_city = StringVar()
    e_num = StringVar()

    e_name_entry = tk.Entry(frame2, bg="#d6d7ed", textvariable=e_name)
    e_email_entry = tk.Entry(frame2,bg="#d6d7ed", textvariable=e_email)
    e_age_entry = tk.Entry(frame2,bg="#d6d7ed", textvariable=e_age)
    e_city_entry = tk.Entry(frame2, bg="#d6d7ed", textvariable=e_city)
    e_num_entry = tk.Entry(frame2, bg="#d6d7ed", textvariable=e_num)

    e_name_entry.place(x=430, y=120)
    e_email_entry.place(x=430, y=150)
    e_age_entry.place(x=430, y=180)
    e_city_entry.place(x=430, y=210)
    e_num_entry.place(x=430, y=240)
Alexander
  • 16,091
  • 5
  • 13
  • 29