【发布时间】:2020-11-26 20:25:54
【问题描述】:
我刚开始学习编程。我正在尝试在 python 中定义一个函数,这样我就不必在我的程序中重复代码。该程序从数据库中搜索条目并显示所有条目或包含关键字的条目。这是我针对这个问题的上下文而精简的程序,它按我的意图工作:
from tkinter import *
import sqlite3
from tkinter import ttk
root = Tk()
root.title('Program')
def search():
search_window = Toplevel(root)
search_window.title('Program - Search')
def search_all():
searchresult_window = Tk()
searchresult_window.title('Program - Searchresult')
#Checks the number of searchresults
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('SELECT COUNT() FROM entry_table')
number = int(str(c.fetchone()).replace(",","").replace("(","").replace(")",""));
#Gets data from database
c.execute('SELECT * FROM entry_table')
data = c.fetchall()
row_count = 0
# Scrollbar
main_frame = Frame(searchresult_window)
main_frame.pack(fill=BOTH, expand=1)
my_canvas = Canvas(main_frame)
my_canvas.pack(side=LEFT, fill=BOTH, expand=1)
my_scroblbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=my_canvas.yview)
my_scroblbar.pack(side=RIGHT, fill=Y)
my_canvas.configure(yscrollcommand=my_scroblbar.set)
my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion=my_canvas.bbox("all")))
seccond_frame = Frame(my_canvas)
my_canvas.create_window((0, 0), window=seccond_frame, anchor="nw")
# Displays searchresults on screen
for _ in range(number):
searchresult_frame = LabelFrame(seccond_frame, pady=5, bg='yellow1')
searchresult_frame.pack()
data_label = Label(hakutulos_frame, text=data[row_count][1], borderwidth=5, relief="groove", width=10)
data_label.grid(row=0, column=0)
row_count = row_count + 1
c.close
conn.close()
search_window.destroy()
searchresult_window.lift()
def search_by_keyword():
searchresult_window = Tk()
searchresult_window.title('Program - Searchresult')
#Checks the number of searchresults
given_keyword = keyword.get()
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("SELECT COUNT() FROM entry_table WHERE subject LIKE '%'||?||'%' OR header LIKE '%'||?||'%' OR entry LIKE '%'||?||'%'",
(given_keyword, given_keyword, given_keyword))
number = int(str(c.fetchone()).replace(",","").replace("(","").replace(")",""));
#Gets data from database
c.execute("SELECT * FROM entry_table WHERE subject LIKE '%'||?||'%' OR header LIKE '%'||?||'%' OR entry LIKE '%'||?||'%'",
(given_keyword, given_keyword, given_keyword))
data = c.fetchall()
row_count = 0
#Scrollbar
main_frame = Frame(searchresult_window)
main_frame.pack(fill=BOTH, expand=1)
my_canvas = Canvas(main_frame)
my_canvas.pack(side=LEFT, fill=BOTH, expand=1)
my_scroblbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=my_canvas.yview)
my_scroblbar.pack(side=RIGHT, fill=Y)
my_canvas.configure(yscrollcommand=my_scroblbar.set)
my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion = my_canvas.bbox("all")))
seccond_frame = Frame(my_canvas)
my_canvas.create_window((0,0), window=seccond_frame, anchor="nw")
# Displays searchresults on screen
for _ in range(number):
searchresult_frame = LabelFrame(seccond_frame, pady=5, bg='yellow1')
searchresult_frame.pack()
data_label = Label(searchresult_frame, text=data[row_count][1], borderwidth=5, relief="groove", width=10)
data_label.grid(row=0, column=0)
c.close
conn.close()
search_window.destroy()
searchresult_window.lift()
#Show all button
all_btn = Button(search_window, text="Show all entries", pady=5, bg='yellow1', command=search_all)
all_btn.grid(row=0, column=0)
#Search by keyword frame (including: entry field and search button)
search_by_keyword_frame = LabelFrame(search_window, text="Search by keyword", pady=5, bg='yellow1')
search_by_keyword_frame.grid(row=1, column=0)
keyword = Entry(search_by_keyword_frame)
keyword.delete(0, END)
keyword.insert(END, str("Give Keyword"))
keyword.grid(row=0, column=0)
hae = Button(search_by_keyword_frame, text="SEARCH", pady=5, bg='yellow1', command=search_by_keyword)
hae.grid(row=1, column=0)
return_btn = Button(search_window, text="Return", pady=5, bg='yellow1', command =search_window.destroy)
return_btn.grid(row=2, column=0, pady=5)
search_btn = Button(root, text="Search", pady=5, bg='yellow1', command=search)
search_btn.grid(row=2, column=0, pady=5)
root.mainloop()
我意识到屏幕上的#Scrollbar 和#Displays 搜索结果部分是完全相同的,我想调用一个插入它的函数。然而,似乎无论我在哪里定义函数(用于滚动条和显示),我总是会出错。这是我尝试过的一个例子:
from tkinter import *
import sqlite3
from tkinter import ttk
root = Tk()
root.title('Program')
def search():
search_window = Toplevel(root)
search_window.title('Program - Search')
def function1():
# Scrollbar
main_frame = Frame(searchresult_window)
main_frame.pack(fill=BOTH, expand=1)
my_canvas = Canvas(main_frame)
my_canvas.pack(side=LEFT, fill=BOTH, expand=1)
my_scroblbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=my_canvas.yview)
my_scroblbar.pack(side=RIGHT, fill=Y)
my_canvas.configure(yscrollcommand=my_scroblbar.set)
my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion=my_canvas.bbox("all")))
seccond_frame = Frame(my_canvas)
my_canvas.create_window((0, 0), window=seccond_frame, anchor="nw")
# Displays searchresults on screen
for _ in range(number):
searchresult_frame = LabelFrame(seccond_frame, pady=5, bg='yellow1')
searchresult_frame.pack()
data_label = Label(hakutulos_frame, text=data[row_count][1], borderwidth=5, relief="groove", width=10)
data_label.grid(row=0, column=0)
row_count = row_count + 1
def search_all():
searchresult_window = Tk()
searchresult_window.title('Program - Searchresult')
#Checks the number of searchresults
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('SELECT COUNT() FROM entry_table')
number = int(str(c.fetchone()).replace(",","").replace("(","").replace(")",""));
#Gets data from database
c.execute('SELECT * FROM entry_table')
data = c.fetchall()
row_count = 0
function1()
c.close
conn.close()
search_window.destroy()
searchresult_window.lift()
def search_by_keyword():
searchresult_window = Tk()
searchresult_window.title('Program - Searchresult')
#Checks the number of searchresults
given_keyword = keyword.get()
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("SELECT COUNT() FROM entry_table WHERE subject LIKE '%'||?||'%' OR header LIKE '%'||?||'%' OR entry LIKE '%'||?||'%'",
(given_keyword, given_keyword, given_keyword))
number = int(str(c.fetchone()).replace(",","").replace("(","").replace(")",""));
#Gets data from database
c.execute("SELECT * FROM entry_table WHERE subject LIKE '%'||?||'%' OR header LIKE '%'||?||'%' OR entry LIKE '%'||?||'%'",
(given_keyword, given_keyword, given_keyword))
data = c.fetchall()
row_count = 0
function1()
c.close
conn.close()
search_window.destroy()
searchresult_window.lift()
#Show all button
all_btn = Button(search_window, text="Show all entries", pady=5, bg='yellow1', command=search_all)
all_btn.grid(row=0, column=0)
#Search by keyword frame (including: entry field and search button)
search_by_keyword_frame = LabelFrame(search_window, text="Search by keyword", pady=5, bg='yellow1')
search_by_keyword_frame.grid(row=1, column=0)
keyword = Entry(search_by_keyword_frame)
keyword.delete(0, END)
keyword.i
nsert(END, str("Give Keyword"))
keyword.grid(row=0, column=0)
hae = Button(search_by_keyword_frame, text="SEARCH", pady=5, bg='yellow1', command=search_by_keyword)
hae.grid(row=1, column=0)
return_btn = Button(search_window, text="Return", pady=5, bg='yellow1', command =search_window.destroy)
return_btn.grid(row=2, column=0, pady=5)
search_btn = Button(root, text="Search", pady=5, bg='yellow1', command=search)
search_btn.grid(row=2, column=0, pady=5)
root.mainloop()
【问题讨论】:
-
您能发布您遇到的错误吗?
-
这是错误:Tkinter 回调回溯中的异常(最近一次调用最后一次):文件“C:\Users\Name\AppData\Local\Programs\Python\Python38-32\lib\tkinter_init_.py”,第 1883 行,在 call 中返回 self.func(*args) 文件“C:/Users/Name/PycharmProjects/test/venv/Testi.py” ,第 49 行,在 search_all function1() 文件“C:/Users/Name/PycharmProjects/test/Testi.py”,第 14 行,在 function1 main_frame = Frame(searchresult_window) NameError: name 'searchresult_window' is not defined
-
请提供minimal reproducible example,以及整个错误输出。
标签: python tkinter methods nested