Home > OS >  Preloading Entry tkinter with Date
Preloading Entry tkinter with Date

Time:12-14

I am writing a program, that users enter information in which the data is then outputted into a CSV once clicked generate.

The info that needs to be entered is: Date, Time, Test Station, Serial Number, and then 3 radio button selections.

Currently, i have it so i can enter the date, time, and serial number. But i want the application to automatically update the date and time so that users don't have to enter it. I have imported a date module but no idea how to get it working within the text box.

from tkinter import *

from datetime import date
today = date.today()
d1 = today.strftime("%d/%m/%y")

def save_info():
    date_info = date.get()
    time_info = time.get()
            
    serialNumber_info = serialNumber.get()
    serialNumber_info = str(serialNumber_info)
            
    print(date_info, time_info, serialNumber_info)

    file = open("test.csv", "a")
    file.write(date_info)
    file.write(",")
    file.write(time_info)
    file.write(",")
    file.write(serialNumber_info)
    file.write("\n")
    file.close()
    print(" User ", date_info, " Has been registered")

    date_entry.delete(0, END)
    time_entry.delete(0, END)
    serialNumber_entry.delete(0, END)

                
screen = Tk()
d1_var = StringVar(screen, d1)
screen.geometry("500x500")
screen.title("Python Form")
heading = Label(text = "Python Form", bg = "grey", fg = "black", width = "500", height = "3")
heading.pack()

date_text = Label(text = "Enter Date '(13/12/2022)' ",)
time_text = Label(text = "Enter Time '(16:45)'",)
serialNumber_text = Label(text = "Enter Serial Number ",)
date_text.place(x = 15, y = 70)
time_text.place(x = 210, y = 70)
serialNumber_text.place(x = 15, y = 210)

date = StringVar()
time = StringVar()
serialNumber = IntVar()

date_entry = Entry(textvariable = d1_var, width = "30")
time_entry = Entry(textvariable = time, width = "30")
serialNumber_entry = Entry(textvariable = serialNumber, width = "30")

date_entry.place(x = 15, y = 100)
time_entry.place(x = 210, y = 100)
serialNumber_entry.place(x = 15, y = 240)
register = Button(screen,text = "Register", width = "30", height = "2", command = save_info, bg = "grey")
register.place(x = 15, y = 400)

CodePudding user response:

the text variable you define to your Entry widget should be a tkinter StringVar. You can define it after creating the Tk() and set any value you want.

screen = Tk()
d1_var = StringVar(screen, d1)

Then just change the Entry textvariable to d1_var instead:

date_entry = Entry(textvariable = d1_var, width = "30")
  • Related