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

2024-10-02 02:29:40 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: textdataframedefpagecolumnwidthframelabel
1条回答
网友
1楼 · 发布于 2024-10-02 02:29:40

您可以在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')

相关问题 更多 >

    热门问题