1

我有一个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', pady = (0,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, font = ('Roboto', 12), 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, pady = (0, 10), fill = 'x')
inst_vsb.pack(side = 'right', fill = 'y', padx = (0, 10), pady = (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插入的 s 在滚动时超出了边界。

任何帮助将不胜感激。

4

1 回答 1

1

采用:

sample_frame = Frame(inst_text, height = 100, width = 500, bg = 'black') # inst_text instead of inst_frame
于 2020-09-08T08:57:29.253 回答