Search code examples
tkintersyntax-errorvar

i am getting syntax error in variables while making calculator i dont know why?



well i am startup python by making calculator and not able to understand error my code goes like this
import tkinter as Tk

root = Tk()
root.Geometry("250*400+300+300")
root.resizeable(0,0)
root.title("Calculator")

btnrow1 = Frame(root , bg="#00000")
btnrow1.Pack(expand = True, fill = "both",)

btnrow2 = Frame(root)
btnrow2.Pack(expand = True, fill = "both",)

btnrow3 = Frame(root)
btnrow3.Pack(expand = True, fill = "both",)

btnrow4 = Frame(root)
btnrow4.Pack(expand = True, fill = "both",)

btn1 = Button(
     btnrow1,
     text = "1",
     font = ("verdana",22)
)
btn1.Pack(side = LEFT , expand = True,)

root.mainloop()

and it throws the syntax error in btnrow2

(base) C:\Users\hp\Documents\datascience>python -u "c:\Users\hp\Documents\calculator\calculatormadebyshaan.py"
  File "c:\Users\hp\Documents\calculator\calculatormadebyshaan.py", line 11
    btnrow2 = Frame(root)
          ^
SyntaxError: invalid syntax

may be it give error later also on btnrow3

please help me i am new to python
thanks


Solution

  • Ultimately, your SyntaxError is because you are putting commas at the end of your pack calls. Correct by simply removing these (e.g. btnrow1.Pack(expand = True, fill = "both",) becomes btnrow1.Pack(expand = True, fill = "both")). Once you have overcome this problem, you will face a couple of other problems, which are as follows:

    1. You are importing tkinter using import tkinter as Tk but then reference the classes using just their names (i.e. as if you imported using from tkinter import *). To resolve this, either change your references to tkinter classes from just their names to Tk.<name> (e.g. Tk() becomes Tk.Tk() and Frame becomes Tk.Frame) or change the import statement to from tkinter import *.
    2. You are trying to call class methods with a uppercase letter at the start (notably for Geometry and Pack). These should be completely lowercase (geometry and pack).

    BTW, for the calculator buttons I would recommend you look into the grid manager instead of pack as this (the 3x3 grid of buttons numbered 1-9) is a textbook example of when it should be used.