在窗扇移动时触发tk根调整大小事件

2024-10-04 03:17:28 发布

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

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

(2)在resize事件中使用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


Tags: ofthetoeventupdaterootfillside
1条回答
网友
1楼 · 发布于 2024-10-04 03:17:28

I'm trying to understand why my root.bind("", resize) line fires when I drag my panedwindow sash left and right?

将绑定添加到根窗口时,该绑定将添加到每个小部件。这是因为您实际上没有绑定到小部件you are binding to a bind tag

当您移动窗框时,这将导致<Configure>事件被发布到窗框。由于绑定,函数被调用

with root.update() in the resize event, ...

决不能在事件的处理程序中调用updateThis can have a lot of unexpected side effects

相关问题 更多 >