簡體   English   中英

調用 function 調整 Python Z6F8BBEA5A81184EBA08B78919F51A

[英]Call function on resizing of window in Python Tkinter

我希望根據框架的寬度更改小部件在框架中的放置位置。 我有一個 function 可以執行此操作,但我希望它在幀大小發生變化時運行。 現在,如果我按下一個按鈕,小部件會重新組織,但我當然正在尋找這個 function 在調整框架大小時運行,而不是在按下按鈕時運行。

此示例中的框架被粘在其父 window 上並填充它,因此檢測 window 調整大小或調整框架大小將是運行 function 的可接受觸發器。

我已經使用智能感知尋找事件並在互聯網上進行了一段時間的搜索,但還沒有找到解決方案。

這是我的代碼:

from tkinter import *

def reorganizeButtons():
    # if the widths of the buttons exceed width of frame, stack vertically
    # else stack horizontally
    if button1.winfo_width()+button2.winfo_width()>frame.winfo_width():
        button1.grid(row=0,column=0, sticky=NSEW)
        button2.grid(row=1,column=0,sticky=NSEW)
    else:
        button1.grid(row=0,column=0,sticky=NSEW)
        button2.grid(row=0,column=1,sticky=NSEW)
    frame.update_idletasks()
    print ()
    print ("button1 width: ", button1.winfo_width())
    print ("button2 width: ", button2.winfo_width())
    print ("frame width: ", frame.winfo_width())

def main():
    global root
    global frame
    global button1
    global button2
    root = Tk()
    root.maxsize(400,200) # sets a limit on how big the frame can be
    root.title("Enlarge Window, then Press a Button")

    frame=Frame(root)
    frame.grid(row=0, column=0, sticky=NSEW)
    root.columnconfigure(0,weight=1)
    root.rowconfigure(0,weight=1)

    button1=Button(frame, text="---Button1---", command=reorganizeButtons)
    button2=Button(frame, text="---Button2---", command=reorganizeButtons)

    reorganizeButtons()

    root.mainloop()

main()

感謝 TheLizzard 的建議,我根據您的建議找到了答案。 這是最終的代碼,效果很好。 我將閱讀有關綁定的更多信息以了解更多信息。 我使用"root.bind("<Configure>", reorganizeButtons)"將該事件綁定到我試圖運行的 function。 我還刪除了按鈕命令,因此它們不會導致小部件的重組。 這是包含解決方案的最終代碼。

from tkinter import *

def reorganizeButtons(event):
    # if the widths of the buttons exceed width of frame, stack vertically
    # else stack horizontally
    if button1.winfo_width()+button2.winfo_width()>frame.winfo_width():
        button1.grid(row=0,column=0, sticky=NSEW)
        button2.grid(row=1,column=0,sticky=NSEW)
    else:
        button1.grid(row=0,column=0,sticky=NSEW)
        button2.grid(row=0,column=1,sticky=NSEW)
    frame.update_idletasks()
    print ()
    print ("button1 width: ", button1.winfo_width())
    print ("button2 width: ", button2.winfo_width())
    print ("frame width: ", frame.winfo_width())

def main():
    global root
    global frame
    global button1
    global button2
    root = Tk()
    root.maxsize(200,200) # sets a limit on how big the frame can be
    root.title("Resize to Reorganize Buttons")

    frame=Frame(root)
    frame.grid(row=0, column=0, sticky=NSEW)
    root.columnconfigure(0,weight=1)
    root.rowconfigure(0,weight=1)

    button1=Button(frame, text="---Button1---")
    button2=Button(frame, text="---Button2---")

    root.bind("<Configure>", reorganizeButtons)

    root.mainloop()

main()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM