【问题标题】:How to prevent the widgets inside text from moving beyond the boundary? Tkinter如何防止文本内的小部件超出边界?特金特
【发布时间】:2020-09-05 19:30:14
【问题描述】:

我有一个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_frames 在滚动时超出了边界。

任何帮助将不胜感激。

【问题讨论】:

  • 使插入的小部件成为 Text 本身的子级,而不是封闭的 Frame。
  • 非常感谢您的快速回复。它现在按预期工作。
  • @jasonharper:请用该信息回答。评论不会被谷歌索引。做出答案会让人们更容易找到它。

标签: python python-3.x tkinter scroll tkinter-text


【解决方案1】:

用途:

sample_frame = Frame(inst_text, height = 100, width = 500, bg = 'black') # inst_text instead of inst_frame

【讨论】:

    猜你喜欢
    • 2022-09-27
    • 2017-04-13
    • 2013-02-12
    • 2012-08-29
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    相关资源
    最近更新 更多