小编gar*_*t73的帖子

在 Python Tkinter 中调用调整窗口大小的函数

我希望根据框架的宽度更改小部件在框架中的放置位置。我有一个函数可以执行此操作,但我希望它在帧大小发生变化时运行。现在,如果我按下按钮,小部件会重新组织,但当然我正在寻找此函数在调整框架大小时运行,而不是在按下按钮时运行。

此示例中的框架粘在其父窗口上并填充它,因此检测窗口大小调整或框架大小调整将是运行该函数的可接受的触发器。

我已经使用智能感知查找事件,也在互联网上进行了一段时间的搜索,但还没有找到解决方案。

这是我的代码:

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, …
Run Code Online (Sandbox Code Playgroud)

python events layout resize tkinter

4
推荐指数
1
解决办法
2266
查看次数

标签 统计

events ×1

layout ×1

python ×1

resize ×1

tkinter ×1