tk root resize 事件触发窗扇运动

问题描述

(1) 我试图理解为什么我的 root.bind("<Configure>",resize) 行在我左右拖动窗格窗扇时会触发?这不会更改根窗口的宽度或高度或 x 或 y 值。

(2) 在调整大小事件中使用 root.update(),我可以左右移动窗扇,最终使 editText 无法展开和填充(见附件)。如果我注释掉这个 update() ,那么我就无法做到这一点。

from tkinter import *
import tkinter as tk

def resize(event):
    root.update()
    print("width",event.width,"height",event.height,"x",event.x,"y",event.y)

root = Tk()
root.title("Test")
root.geometry('600x400+400+350')

pw = tk.PanedWindow(root,background="cyan",bd=4,orient=HORIZONTAL,sashrelief=RAISED,sashwidth=4,# 2 default
                            showhandle=FALSE,sashpad=5,# thickness of the sash background band
                            sashcursor="sb_h_double_arrow")

pw.pack(fill=BOTH,expand=True)

frame1 = tk.Frame(pw)
frame2 = tk.Frame(pw)

pw.add(frame1)
pw.add(frame2)

# create scrollbars
xscrollbar = Scrollbar(frame2,orient='horizontal')  # create X axis scrollbar and assign to frame2
yscrollbar = Scrollbar(frame2,orient='vertical')  # create Y axis scrollbar and assign to frame2
xscrollbar.pack( side = BottOM,fill=X )  # bottom side horizontal scrollbar
yscrollbar.pack( side = RIGHT,fill=Y )  # right side vertical scrollbar

t5 = Text(frame2,wrap = 'none',xscrollcommand = xscrollbar.set,yscrollcommand = yscrollbar.set)
for line in range(50):
   t5.insert(END,str(line+1) + " Now is the time for all good men to come to the aid of their party. Now is the time for all good men to come to the aid of their party.\n")
t5.pack(expand=True,fill='both') # fill frame with Text widget

pw.update()
#print(pw.sash_coord(0))  # This method returns the location of a sash
# Use this method to reposition the sash selected by index
print(pw.sash_place(0,90,4))  # 90 moves the sash left/right

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

root.mainloop()

enter image description here

解决方法

我想了解为什么我的 root.bind("",resize) 行在我左右拖动窗格窗扇时触发?

当您向根窗口添加绑定时,该绑定会添加到每个小部件中。这是因为您实际上并未绑定到小部件 you are binding to a bind tag

当您移动窗框时,会导致 <Configure> 事件被发送到窗框。并且因为绑定,函数被调用。

在 resize 事件中使用 root.update(),...

您永远不应该在事件的处理程序中调用 updateThis can have a lot of unexpected side effects