gpt4 book ai didi

python - Tkinter:当我在按住按钮的框架旁边创建一个框架时,按钮会上下跳跃

转载 作者:行者123 更新时间:2023-12-04 08:28:34 24 4
gpt4 key购买 nike

4 按钮位于左上角,使用名为 menuFrame 的框架来固定它。
程序启动时,原来停留在正确的位置
然而。当我使用图书搜索功能时,
按钮刚刚下降到其他地方。
我完全不知道发生了什么
我不知道是不是我以前通过不同按钮更改页面的方式导致了这种意外结果。
我用的方法是全删ItemFrame它保存页面中的所有内容并创建一个新的
在同一项目中,在我使用 .destroy 删除框架后功能,item frame中的widget不能到窗口的最上层。
[ original

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

最佳答案

您可以设置 sticky='n'grid(...)将菜单按钮放在顶部,将小部件放在 itemFrame 中在顶部:

# create frames
menuFrame = tk.Frame(root, width=20)
menuFrame.grid(row=0, column=0, sticky='n')

#global itemFrame # it is not necessary
itemFrame = tk.Frame(root)
itemFrame.grid(row=0, column=1, sticky='n')

def create_itemFrame():
global itemFrame
itemFrame = tk.Frame(root)
itemFrame.grid(row=0, column=1, sticky='n')

关于python - Tkinter:当我在按住按钮的框架旁边创建一个框架时,按钮会上下跳跃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65134427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com