每当我运行此代码时,我都会不断收到有关此代码行的语法错误:
btn = ttk.Button(window, text="Submit", command=lambda: removeQuestion(DeleteQuestion))
我已经研究过更改参数,变量名等等,但是我仍然继续收到此错误,并且不确定什么地方出错了。
完整代码:
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()
答案 0 :(得分:1)
它在
上缺少括号questionEntry.grid(row = 1, column = 0
应该是
questionEntry.grid(row = 1, column = 0)
答案 1 :(得分:0)
您的代码中有两个错误,这是一个简单的错误:
1- questionEntry.grid(row = 1, column = 0)
2- 在最后一行,您必须调用名为 QuestionMenu()
我给整个代码提供了适当的解决方案和可运行的代码:
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()