Python Forum
Unexplained Invalid syntax Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexplained Invalid syntax Error
#1
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()
Reply
#2
Please post the error in its entirety, as it gives the line number and a pointer to what the problem is.
Reply
#3
(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.
Reply
#4
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.
Reply
#5
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.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,006 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 333 Jan-19-2024, 01:20 PM
Last Post: rob101
  error: invalid command 'egg_info' TimTu 0 906 Jul-27-2023, 07:30 AM
Last Post: TimTu
  Syntax error while executing the Python code in Linux DivAsh 8 1,450 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,134 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  print(data) is suddenly invalid syntax db042190 6 1,119 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  syntax error question - string mgallotti 5 1,248 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,194 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,637 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  Syntax error tibbj001 2 847 Dec-05-2022, 06:38 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020