python - Tkinter 回调中的异常:未定义名称“selected_book”
问题描述
我正在尝试构建一个简单的 GUI 应用程序,但出现一个错误,提示“未定义名称 'selected_book'”,但它是一个全局变量。我正在尝试将变量值传递给delete_book()
函数,但此函数在另一个脚本中。有人请帮帮我。
这是我的第一个脚本:
from tkinter import *
from prog2 import *
window = Tk()
def get_selected_id(event) :
global selected_book
id = list1.curselection()[0]
selected_book = list1.get(id)[0]
def view_all_cmd() :
list1.delete(0, END)
for row in view_all() :
list1.insert(END, row)
def search_book_cmd() :
list1.delete(0, END)
for row in search_book(title_entry.get(), author_entry.get(), year_entry.get(), book_ID_entry.get()) :
list1.insert(END, row)
def add_book_cmd() :
list1.delete(0, END)
add_book(title_entry.get(), author_entry.get(), year_entry.get(), id_entry.get())
list1.insert(END, (title_entry.get(), author_entry.get(), year_entry.get(), bookID_entry.get()))
def delete_book_cmd() :
view_all_cmd()
delete_book(selected_book)
l1 = Label(window, text = "Title")
l1.grid(row = 0, column = 0)
l1 = Label(window, text = "Author")
l1.grid(row = 0, column = 2)
l1 = Label(window, text = "Year")
l1.grid(row = 1, column = 0)
l1 = Label(window, text = "Book ID")
l1.grid(row = 1, column = 2)
title_entry = StringVar()
e1 = Entry(window, textvariable = title_entry)
e1.grid(row = 0, column = 1)
author_entry = StringVar()
e2 = Entry(window, textvariable = author_entry)
e2.grid(row = 0, column = 3)
year_entry = StringVar()
e3 = Entry(window, textvariable = year_entry)
e3.grid(row = 1, column = 1)
bookID_entry = StringVar()
e4 = Entry(window, textvariable = bookID_entry)
e4.grid(row = 1, column = 3)
list1 = Listbox(window, height = 7, width = 35)
list1.grid(row = 2, column = 0,rowspan = 7, columnspan = 2)
sb1 = Scrollbar(window)
sb1.grid(row = 2 ,column = 2, rowspan = 6)
list1.configure(yscrollcommand = sb1.set)
sb1.configure(command = list1.yview)
list1.bind('<<ListboxSelect>>', get_selected_id)
b1 = Button(window, text = "View All", width = 15, command = view_all_cmd)
b1.grid(row = 2, column = 3)
b2 = Button(window, text = "Search Book", width = 15, command = search_book_cmd)
b2.grid(row = 3, column = 3)
b3 = Button(window, text = "Add Book", width = 15, command = add_book_cmd)
b3.grid(row = 4, column = 3)
b4 = Button(window, text = "Update Book", width = 15, command = update_book_cmd)
b4.grid(row = 5, column = 3)
b5 = Button(window, text = "Detele Book", width = 15, command = delete_book_cmd)
b5.grid(row = 6, column = 3)
b6 = Button(window, text = "Close", width = 15)
b6.grid(row = 7, column = 3)
window.mainloop()
这是我的第二个脚本:
import sqlite3
conn = sqlite3.connect("Books.db")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS books(id INTEGER PRIMARY KEY, title TEXT,author TEXT, year INTEGER, bookID INTEGER)")
def add_book(title, author, year, bookID) :
conn = sqlite3.connect("Books.db")
cur = conn.cursor()
cur.execute("INSERT INTO books VALUES(NULL, ?, ?, ?, ?)", (title, author, year, bookID))
conn.commit()
conn.close()
def search_book(title = "", author = "", year = "", bookID = "") :
conn = sqlite3.connect("Books.db")
cur = conn.cursor()
cur.execute("SELECT * FROM books WHERE title = ? OR author = ? OR year = ? OR bookID = ?", (title, author, year, bookID))
data = cur.fetchall()
conn.close()
return data
def delete_book(id) :
conn = sqlite3.connect("Books.db")
cur = conn.cursor()
cur.execute("DELETE FROM books WHERE id = ?", (id))
conn.commit()
conn.close()
def update_book(id, title, author, year, bookID) :
conn = sqlite3.connect("Books.db")
cur = conn.cursor()
cur.execute("UPDATE books SET title = ?, author = ?, year = ?, bookID = ? WHERE id =?", (title, author, year, bookID, id))
conn.commit()
conn.close()
def view_all() :
conn = sqlite3.connect("Books.db")
cur = conn.cursor()
cur.execute("SELECT * FROM books")
data = cur.fetchall()
conn.close()
return data
这是我的错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\ashak\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File ".\prog1.py", line 31, in delete_book_cmd
delete_book(selected_book)
NameError: name 'selected_book' is not defined
有人能帮我吗?
解决方案
推荐阅读
- c# - 如何在 C# 数组中查找和显示特定值
- c# - 进程 Azure AD B2C 自定义策略用于散列数据
- r - check_rhub 正在尝试安装 BiocManager 失败
- spring-boot - 如何将流量重新路由到 ip 到特定端口以显示网站
- reactjs - Jest Globals 收到“不是函数”错误,没有 Globals 可以正常工作
- python - 在字符串中执行python代码并将其存储到变量中
- node.js - Docusign - 如何为多个用户集成 docusign,每个用户都有不同的 docusign 凭据和多个客户
- javascript - 如何在基于反应js的id中获取api中的数据
- r - 从 FTP 到 R 损坏的文件
- flutter - 在小部件定义中插入 if-else 语句