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
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 354 Oct-06-2021, 09:39 AM
Last Post: menator01
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 481 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Line 42 syntax error..Help!!1 patpython 4 604 Sep-01-2021, 03:22 PM
Last Post: deanhystad
  I have a syntax error! rohes_kaugummi 3 804 Jun-23-2021, 05:24 PM
Last Post: deanhystad
  import module with syntax error Skaperen 7 1,023 Jun-22-2021, 10:38 AM
Last Post: Skaperen
Heart near "(": syntax error Maryan 3 1,125 Jun-21-2021, 08:05 AM
Last Post: ibreeden
Exclamation getting a syntax error and i don't know why ilikedofs 10 1,536 May-13-2021, 08:52 PM
Last Post: deanhystad
  [split] SyntaxError: invalid syntax Code_X 3 858 May-04-2021, 05:15 PM
Last Post: Yoriz
  Even number code syntax error MrCeez 1 787 May-02-2021, 06:43 PM
Last Post: Larz60+
  error in your SQL syntax Xuli 10 1,442 May-02-2021, 12:27 PM
Last Post: Xuli

Forum Jump:

User Panel Messages

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