The 4 button is at the top left corning which using a frame named menuFrame to hold it. When the program started, it is staying at the correct position originally However. When I used the book search function, the button just went down to somewhere else.
I have totally no idea what is happening
I don't know is the way I used to change the page by different button leads to this unexpected result.
The method I used is deleted the whole ItemFrame which hold all the things in the page and create a new one
At the same item, after I delete the frame using the .destroy function, the widget in the item frame cant go to the top level of the window.
[
import tkinter as tk
root = tk.Tk()
root.title('Library System')
root.geometry('1000x600')
# create frames
menuFrame = tk.Frame(root,width = 20)
menuFrame.grid(row=0, column=0)
global itemFrame
itemFrame = tk.Frame(root)
itemFrame.grid(row=0,column=1)
def create_itemFrame():
global itemFrame
itemFrame = tk.Frame(root)
itemFrame.grid(row=0,column=1)
def clear_itemFrame():
itemFrame.destroy()
def print_records(Frame,n):
dataframe = tk.Frame(Frame)
dataframe.grid(row=1,column=1)
tk.Label(dataframe,text="Book ID", height = 1, width = 10).grid(row=1+n,column=1)
tk.Label(dataframe,text="ISBN", height = 1, width = 10).grid(row=1+n,column=2)
tk.Label(dataframe,text="Title", height = 1, width = 40).grid(row=1+n,column=3)
tk.Label(dataframe,text="Author", height = 1, width = 20).grid(row=1+n,column=4)
tk.Label(dataframe,text="Purchase Date", height = 1, width = 13).grid(row=1+n,column=5)
tk.Label(dataframe,text="Member Id", height = 1, width = 8).grid(row=1+n,column=6)
def booksearch():
def searching():
SearchKeyword = str(SearchKeyword_entry.get())
clear_itemFrame()
create_itemFrame()
SearchresultFrame = tk.Frame(itemFrame)
SearchresultFrame.grid(row=0,column=1)
tk.Label(SearchresultFrame,text="Book ID", height = 1, width = 10).grid(row=0,column=1)
tk.Label(SearchresultFrame,text="ISBN", height = 1, width = 10).grid(row=0,column=2)
tk.Label(SearchresultFrame,text="Title", height = 1, width = 40).grid(row=0,column=3)
tk.Label(SearchresultFrame,text="Author", height = 1, width = 20).grid(row=0,column=4)
tk.Label(SearchresultFrame,text="Purchase Date", height = 1, width = 13).grid(row=0,column=5)
tk.Label(SearchresultFrame,text="Member Id", height = 1, width = 8).grid(row=0,column=6)
#searching frame
create_itemFrame()
Searchingframe = tk.Frame(itemFrame)
Searchingframe.grid(row=0,column=1)
SearchKeyword_label = tk.Label(Searchingframe, text='Search Keyword')
SearchKeyword_label.grid(row=0,column=1)
SearchKeyword_entry = tk.Entry(Searchingframe)
SearchKeyword_entry.grid(row=0,column=2)
SearchKeyword_btn = tk.Button(Searchingframe, text='Search', command=searching)
SearchKeyword_btn.grid(row=0,column=3)
print_records(itemFrame,1)
def bookreturn():
def returning():
return 0
create_itemFrame()
Returnframe = tk.Frame(itemFrame)
Returnframe.grid(row=0,column=1)
SearchKeyword_label = tk.Label(Returnframe, text='Enter Book ID')
SearchKeyword_label.grid(row=0,column=1)
SearchKeyword_entry = tk.Entry(Returnframe)
SearchKeyword_entry.grid(row=0,column=2)
SearchKeyword_btn = tk.Button(Returnframe, text='Return', command=returning)
SearchKeyword_btn.grid(row=0,column=3)
# different items on different menus
def booksearch_page():
clear_itemFrame()
booksearch()
print(0)
def bookcheckout_page():
clear_itemFrame()
print(1)
def bookweed_page():
clear_itemFrame()
print(2)
def bookreturn_page():
clear_itemFrame()
bookreturn()
print(3)
# persistent menu buttons
menuButton1 = tk.Button(menuFrame, text="booksearch", height = 1, width = 15,command=booksearch_page)
menuButton1.grid(row=0, column=0)
menuButton2 = tk.Button(menuFrame, text="bookcheckout", height = 1, width = 15,command=bookcheckout_page)
menuButton2.grid(row=1, column=0)
menuButton3 = tk.Button(menuFrame, text="bookweed", height = 1, width = 15,command=bookweed_page)
menuButton3.grid(row=2, column=0)
menuButton4 = tk.Button(menuFrame, text="bookreturn", height = 1, width = 15,command=bookreturn_page)
menuButton4.grid(row=3, column=0)
root.mainloop()
After something is created in the item frame:
[