问题描述
我有一个Text
小部件,其中有一堆作为窗口插入的小部件。每当我滚动时,小部件都会移动到Text
边界之外,直到它们的末端交叉为止。这是产生类似症状的示例代码。
from tkinter import *
root = Tk()
inst_frame = Frame(root,highlightthickness = 1,highlightbackground = 'black',bg = 'white')
inst_frame.pack(padx = 10,pady = (0,10),side = 'bottom',anchor = 'w')
lab = Label(inst_frame,text = 'Stack of widgets',bg = 'RoyalBlue',font = ('Roboto',12),fg = 'white')
lab.pack(side = 'top',anchor = 'nw',fill = 'x',10))
disp_inst_frame = Frame(inst_frame,bg = 'white')
disp_inst_frame.pack(side = 'bottom',fill = 'both')
inst_text_frame = Frame(disp_inst_frame,width = 820,height = 200,bg = 'white')
inst_text_frame.pack(side = 'left',expand = False,fill = 'x')
inst_text_frame.pack_propagate(False)
inst_text = Text(inst_text_frame,wrap = 'word',bd = 0)
inst_vsb = Scrollbar(disp_inst_frame,command = inst_text.yview)
inst_text.configure(yscrollcommand = inst_vsb.set)
inst_text.pack(padx = 10,fill = 'x')
inst_vsb.pack(side = 'right',fill = 'y',padx = (0,10))
for _ in range(0,5): #Inserts blank frames
sample_frame = Frame(inst_frame,height = 100,width = 500,bg = 'black')
inst_text.window_create(END,window = sample_frame)
inst_text.insert(END,'\n'*5)
root.mainloop()
在这里您可以注意到,滚动时插入的sample_frame
超出了范围。
任何帮助将不胜感激。
解决方法
使用:
sample_frame = Frame(inst_text,height = 100,width = 500,bg = 'black') # inst_text instead of inst_frame