Python Forum

Full Version: Unexplained Invalid syntax Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i am working on a Order form with tkinter. The first draft worked well, I wanted to add current date display on the form. When i run it I get a Syntax Error " invalid syntax" highlighting the "toothnumbervalue" I am a beginner in python . i have gone through the code several times but am unable to locate the cause of the error . Can somebody please show me the way.
from tkinter import *

root = Tk()

def getvals():
    print("Submitting form")

    print(f"{labnamevalue.get(),patientnamevalue.get(), workvalue.get(), toothnumbervalue.get(), materialvalue.get(), shadevalue.get(), labservicevalue.get(),orderdatevalue.(dt.datetime.now():%a, %b %d %Y)} ") 



    with open("records.txt", "a") as f:
        f.write(f"{labnamevalue.get(),patientnamevalue.get(), workvalue.get(), toothnumbervalue.get(), materialvalue.get(), shadevalue.get(), labservicevalue.get(),orderdatevalue.(dt.datetime.now():%a, %b %d %Y)}\n ")


root.geometry("644x344")
#Heading
Label(root, text="Arora Dental Care\n Lab Work Order ", font="comicsansms 13 bold", pady=15).grid(row=0, column=5)

#Text for our form
labname = Label(root, text=" Lab. Name")
patientname=Label(root, text=" Patient.Name")
orderdate=Label(root,text="Order Date")
work = Label(root, text="Work Required")
toothnumber = Label(root, text="Tooth Number")
material = Label(root, text="Material")
shade = Label(root, text="Shade")

#Pack text for our form
labname.grid(row=1, column=2)
patientname.grid(row=1, column=5)
orderdate.grid(row=2, column=5)
work.grid(row=2, column=2)
toothnumber.grid(row=3, column=2)
material.grid(row=4, column=2)
shade.grid(row=5, column=2)

# Tkinter variable for storing entries
labnamevalue = StringVar()
patientnamevalue = StringVar()
orderdatevalue= StringVar()
workvalue = StringVar()
toothnumbervalue = StringVar()
materialvalue = StringVar()
shadevalue = StringVar()
labservicevalue = IntVar()


#Entries for our form
labnameentry = Entry(root, textvariable=labnamevalue)
patientnameentry = Entry(root, textvariable=patientnamevalue)
orderdateentry=Label(root,text=f"{dt.datetime.now():%a,%b %d %Y}")
workentry = Entry(root, textvariable=workvalue)
toothnumberentry = Entry(root, textvariable=toothnumbervalue)
materialentry = Entry(root, textvariable=materialvalue)
shadeentry = Entry(root, textvariable=shadevalue)

# Packing the Entries
labnameentry.grid(row=1, column=3)
patientnameentry.grid(row=1, column=7)
orderdateentry.grid(row=2, column=5)
workentry.grid(row=2, column=3)
toothnumberentry.grid(row=3, column=3)
materialentry.grid(row=4, column=3)
shadeentry.grid(row=5, column=3)

#Checkbox & Packing it
labservice = Checkbutton(text="Want metal coping trial", variable = labservicevalue)
labservice.grid(row=7, column=3)

#Button & packing it and assigning it a command
Button(text=" submit ", command=getvals).grid(row=13, column=3)



root.mainloop()
Please post the error in its entirety, as it gives the line number and a pointer to what the problem is.
(Jul-31-2021, 07:42 AM)ndc85430 Wrote: [ -> ]Please post the error in its entirety, as it gives the line number and a pointer to what the problem is.
I am not getting any trace back its only a pop up message saying invalid syntax.
I don't know what you're doing to run the program, so just run it on the command line and you should get a traceback.
There are SyntaxError in both in print and write.
As mention bye ndc85430 Traceback should show this.
Error:
λ python tk_test.py File "G:\div_code\tk_test.py", line 9 (labnamevalue.get(),patientnamevalue.get(), workvalue.get(), toothnumbervalue.get(), materialvalue.get(), shadevalue.get(), labservicevalue.get(),orderdatevalue.(dt.datetime.now():%a, %b %d %Y)) ^ SyntaxError: f-string: invalid syntax
Can no do this orderdatevalue. is a attribute access and call it with tuple with date object inside will never work.
Also can not use tuple around date object inside a f-string.
>>> import datetime as dt
>>> 
>>> d = f'{(dt.datetime.now():%a, %b %d %Y)}'
  File "<interactive input>", line 1
    ((dt.datetime.now():%a, %b %d %Y))
                       ^
SyntaxError: f-string: invalid syntax

# Fix
>>> d = f'{dt.datetime.now():%a, %b %d %Y}'
>>> d
'Sat, Jul 31 2021
Test increment with smaller step as you new to this,also line of length 210 character is way to long.
thanks a lot snippsat. It worked.
(Jul-31-2021, 12:39 PM)snippsat Wrote: [ -> ]There are SyntaxError in both in print and write.
As mention bye ndc85430 Traceback should show this.
Error:
λ python tk_test.py File "G:\div_code\tk_test.py", line 9 (labnamevalue.get(),patientnamevalue.get(), workvalue.get(), toothnumbervalue.get(), materialvalue.get(), shadevalue.get(), labservicevalue.get(),orderdatevalue.(dt.datetime.now():%a, %b %d %Y)) ^ SyntaxError: f-string: invalid syntax
Can no do this orderdatevalue. is a attribute access and call it with tuple with date object inside will never work.
Also can not use tuple around date object inside a f-string.
>>> import datetime as dt
>>> 
>>> d = f'{(dt.datetime.now():%a, %b %d %Y)}'
  File "<interactive input>", line 1
    ((dt.datetime.now():%a, %b %d %Y))
                       ^
SyntaxError: f-string: invalid syntax

# Fix
>>> d = f'{dt.datetime.now():%a, %b %d %Y}'
>>> d
'Sat, Jul 31 2021
Test increment with smaller step as you new to this,also line of length 210 character is way to long.