0

Whenever I run this code I keep on receiving a syntax error regarding this code line:

btn = ttk.Button(window, text="Submit", command=lambda: removeQuestion(DeleteQuestion))

I have looked at changing the parameters, variable names and more yet I still keep on getting this error and I am not sure what is wrong.

Full code:

import sqlite3
from tkinter import ttk
import tkinter
from tkinter import messagebox

with sqlite3.connect("question.db") as db:
    cursor = db.cursor()

cursor.execute("""CREATE TABLE IF NOT EXISTS game (
                questionID integer PRIMARY KEY AUTOINCREMENT,
                question text,
                answer text
                )""")

#SQL QUESTION ADD/REMOVE/GET
def insert_question(emp):
    c.execute("INSERT INTO game VALUES (?, ?, ?)", (emp))
    conn.commit()

def get_question():
    c.execute("SELECT * FROM game")
    return c.fetchall()

def remove_question(emp):
    c.execute("DELETE from game WHERE question = ?", [emp])
    conn.commit()

#Tkinter
def showInstructions():
    messagebox.showinfo("Instructions", """You are a treasure hunter, your goal is to collect atleast 100
gold by the end of the game from treasure chests randomly scattered across the grid.There are 10 chests within a grid and
each treasure chest is worth 10 gold but can only be reclaimed 3 times before it is replaced by a bandit.
Landing on a bandit will cause you to lose all of your
gold and if all the chests have been replaced by bandits and you have less then 100 gold this means you lose!

Press enter to continue...""")#messagebox used for more simple functions (showing messages)

def showLeaderboard():
    messagebox.showinfo("Leaderboard", "shows leaderboard")
    window.destroy()

def showQuestions():
    emps = get_question()
    messagebox.showinfo("List of questions/answers", emps)

def AddQuestion(mathquestion, mathanswer):
    mathquestion1 = mathquestion.get()
    mathanswer1 = mathanswer.get()
    emp_1 = (None, mathquestion1, mathanswer1)
    insert_question(emp_1)

    emps = get_question()
    print(emps)

def removeQuestion(DeleteQuestion):
    DeleteQuestion1 = DeleteQuestion.get()
    remove_question(DeleteQuestion1)

def removeQuestionTk():
    window = tkinter.Tk()
    window.title("Remove a question.")

    labelOne = ttk.Label(window, text = "Enter question to remove:")
    labelOne.grid(row = 0, column = 0) 
    DeleteQuestion = tkinter.StringVar(window)#value type is classified as a string
    questionEntry = ttk.Entry(window, width = 30, textvariable = DeleteQuestion)
    questionEntry.grid(row = 1, column = 0

    btn = ttk.Button(window, text="Submit", command=lambda: removeQuestion(DeleteQuestion))
    btn.grid(row = 1, column = 1)

def QuestionMenu():
    with sqlite3.connect("question.db") as db:
        c = db.cursor()

    window = tkinter.Tk()
    window.title("Treasure Hunt Game!")

    labelOne = ttk.Label(window, text = """ ~~~~~~~~~~~~~ GAME MENU ~~~~~~~~~~~~~
    """)#label displays instruction
    labelOne.grid(row = 0, column = 0)#places label in a grid

    btn = ttk.Button(window, text = "View instructions", command = showInstructions)
    btn.grid(row = 1, column = 0)#places button in a grid

    btn = ttk.Button(window, text = "View leaderboard", command = showLeaderboard)
    btn.grid(row = 2, column = 0)

    btn = ttk.Button(window, text = "View all questions", command = showQuestions)
    btn.grid(row = 3, column = 0)

    btn = ttk.Button(window, text = "Continue", command = showLeaderboard)
    btn.grid(row = 4, column = 0)

    labelTwo = ttk.Label(window, text = "Enter a math question:")
    labelTwo.grid(row = 5, column = 0)
    mathquestion = tkinter.StringVar()#value type is classified as a string
    userEntryQ = ttk.Entry(window, width = 30, textvariable = mathquestion)
    userEntryQ.grid(row = 6, column = 0)

    labelTwo = ttk.Label(window, text = "Enter the answer to this question:")
    labelTwo.grid(row = 7, column = 0)
    mathanswer = tkinter.StringVar()
    userEntryQ = ttk.Entry(window, width = 30, textvariable = mathanswer)
    userEntryQ.grid(row = 8, column = 0)

    btn = ttk.Button(window, text = "Submit", command=lambda: AddQuestion(mathquestion, mathanswer))
    btn.grid(row = 8, column = 1)

    btn = ttk.Button(window, text = "Remove a question", command = removeQuestionTk)
    btn.grid(row = 9, column = 0)#places button in a grid

menu()
4
  • Yeah I tried it without the lambda expression and I still got the same error. Commented Mar 19, 2019 at 20:13
  • @amanb: The lambda is correct. command=removeQuestion(DeleteQuestion) would not pass a function; it would attempt to call removeQuestion immediately and pass the return value as command. Commented Mar 19, 2019 at 20:50
  • You've posted way too much code. Please try to condense it down to a minimal reproducible example. Often, that effort is enough to help you find the problem for yourself. Commented Mar 19, 2019 at 21:52
  • There error is obviously at line questionEntry.grid(row = 1, column = 0 where you missed a ). When you get syntax error on a line that seems perfect, it means that the line above the shown line has some error. Commented Jan 17, 2021 at 16:35

2 Answers 2

1

You are missing a parenthesis on:

questionEntry.grid(row = 1, column = 0

it should be:

questionEntry.grid(row = 1, column = 0)
Sign up to request clarification or add additional context in comments.

Comments

0

There is two error in your code and its a simple error: 1- questionEntry.grid(row = 1, column = 0)

2- In the end line you have to call the funtion named as QuestionMenu()

and i am giving the whole code with proper solution and runnable code:

import sqlite3
from tkinter import ttk
import tkinter
from tkinter import messagebox

with sqlite3.connect("question.db") as db:
    cursor = db.cursor()

cursor.execute("""CREATE TABLE IF NOT EXISTS game (
                questionID integer PRIMARY KEY AUTOINCREMENT,
                question text,
                answer text
                )""")

#SQL QUESTION ADD/REMOVE/GET
def insert_question(emp):
    c.execute("INSERT INTO game VALUES (?, ?, ?)", (emp))
    conn.commit()

def get_question():
    c.execute("SELECT * FROM game")
    return c.fetchall()

def remove_question(emp):
    c.execute("DELETE from game WHERE question = ?", [emp])
    conn.commit()

#Tkinter
def showInstructions():
    messagebox.showinfo("Instructions", """You are a treasure hunter, your goal is to collect atleast 100
gold by the end of the game from treasure chests randomly scattered across the grid.There are 10 chests within a grid and
each treasure chest is worth 10 gold but can only be reclaimed 3 times before it is replaced by a bandit.
Landing on a bandit will cause you to lose all of your
gold and if all the chests have been replaced by bandits and you have less then 100 gold this means you lose!

Press enter to continue...""")#messagebox used for more simple functions (showing messages)

def showLeaderboard():
    messagebox.showinfo("Leaderboard", "shows leaderboard")
    window.destroy()

def showQuestions():
    emps = get_question()
    messagebox.showinfo("List of questions/answers", emps)

def AddQuestion(mathquestion, mathanswer):
    mathquestion1 = mathquestion.get()
    mathanswer1 = mathanswer.get()
    emp_1 = (None, mathquestion1, mathanswer1)
    insert_question(emp_1)

    emps = get_question()
    print(emps)

def removeQuestion(DeleteQuestion):
    DeleteQuestion1 = DeleteQuestion.get()
    remove_question(DeleteQuestion1)

def removeQuestionTk():
    window = tkinter.Tk()
    window.title("Remove a question.")

    labelOne = ttk.Label(window, text = "Enter question to remove:")
    labelOne.grid(row = 0, column = 0) 
    DeleteQuestion = tkinter.StringVar(window)#value type is classified as a string
    questionEntry = ttk.Entry(window, width = 30, textvariable = DeleteQuestion)
    questionEntry.grid(row = 1, column = 0)

    btn = ttk.Button(window, text="Submit", command=lambda: removeQuestion(DeleteQuestion))
    btn.grid(row = 1, column = 1)

def QuestionMenu():
    with sqlite3.connect("question.db") as db:
        c = db.cursor()

    window = tkinter.Tk()
    window.title("Treasure Hunt Game!")

    labelOne = ttk.Label(window, text = """ ~~~~~~~~~~~~~ GAME MENU ~~~~~~~~~~~~~
    """)#label displays instruction
    labelOne.grid(row = 0, column = 0)#places label in a grid

    btn = ttk.Button(window, text = "View instructions", command = showInstructions)
    btn.grid(row = 1, column = 0)#places button in a grid

    btn = ttk.Button(window, text = "View leaderboard", command = showLeaderboard)
    btn.grid(row = 2, column = 0)

    btn = ttk.Button(window, text = "View all questions", command = showQuestions)
    btn.grid(row = 3, column = 0)

    btn = ttk.Button(window, text = "Continue", command = showLeaderboard)
    btn.grid(row = 4, column = 0)

    labelTwo = ttk.Label(window, text = "Enter a math question:")
    labelTwo.grid(row = 5, column = 0)
    mathquestion = tkinter.StringVar()#value type is classified as a string
    userEntryQ = ttk.Entry(window, width = 30, textvariable = mathquestion)
    userEntryQ.grid(row = 6, column = 0)

    labelTwo = ttk.Label(window, text = "Enter the answer to this question:")
    labelTwo.grid(row = 7, column = 0)
    mathanswer = tkinter.StringVar()
    userEntryQ = ttk.Entry(window, width = 30, textvariable = mathanswer)
    userEntryQ.grid(row = 8, column = 0)

    btn = ttk.Button(window, text = "Submit", command=lambda: AddQuestion(mathquestion, mathanswer))
    btn.grid(row = 8, column = 1)

    btn = ttk.Button(window, text = "Remove a question", command = removeQuestionTk)
    btn.grid(row = 9, column = 0)#places button in a grid

QuestionMenu()

output

4 Comments

It is not recommended to use more than one instance of Tk(), recommend replacing it with Toplevel().
what exactly you want to say ? can u please elaborate ?
You used tkinter.Tk() twice, which is not recommended.
Yes ,the line tkinter.tk can be removed and it can't affect the code but thanks @CoolCloud , and actually i copy the code from the upside for solving the basic error ,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.