J.Peggy
J.Peggy

Reputation: 87

Python Tkinter button invalid syntax error

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()

Upvotes: 0

Views: 2393

Answers (2)

1966bc
1966bc

Reputation: 1308

You are missing a parenthesis on:

questionEntry.grid(row = 1, column = 0

it should be:

questionEntry.grid(row = 1, column = 0)

Upvotes: 1

Zeeking786
Zeeking786

Reputation: 195

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

Upvotes: 0

Related Questions