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

标签 python user-interface tkinter button

4 按钮位于左上角,使用名为 menuFrame 的框架来容纳它。 程序启动时,停留在原来正确的位置 然而。当我使用图书搜索功能时, 按钮刚刚落到其他地方。

我完全不知道发生了什么

我不知道是我过去通过不同按钮更改页面的方式导致了这个意外的结果。 我使用的方法是删除整个 ItemFrame ,其中包含页面中的所有内容并创建一个新的

在同一个项目中,当我使用 .destroy 函数删除框架后,项目框架中的小部件无法转到窗口的顶层。

[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

最佳答案

您可以在grid(...)中设置sticky='n',将菜单按钮放在顶部,将小部件放在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/

相关文章:

python - Jsch - 通过 SSH 连接发送命令

python - 如何检查插入或更新是否会影响选择查询的结果

python - Keras 中的 "Could not interpret activation function identifier: 256"错误

c# - 是否有任何免费的 .NET 库用于皮肤应用程序?

python-3.x - 如何通过entry.get()变量从SQL检索数据

Python-tkinter : Opening and closing dialog windows

python - 如何修复权限被拒绝的错误? (发生在任何 IDE 中)

c# - 为控制台应用程序制作 UI

java - 带标题 Pane 的自动高度

python - 同等权重的网格管理器中按钮大小不同?