tkinter 中 Text() 内的可滚动 Frame()

发布于 2024-12-02 19:16:25 字数 2144 浏览 1 评论 0原文

我已经在 2 列中嵌入了 100 行。每行都位于一个框架内,并且全部打包在一个文本小部件内。我正在尝试添加滚动条,但它仅滚动文本,而列不会移动。

    frame_fields = tk.Text(self.window)
    frame_fields.pack_propagate(0)
    tf = {}
    text_labels = tk.Text(frame_fields)
    text_labels.pack(side='left', expand='yes', fill='both')

    scrollbar = tk.Scrollbar(frame_fields)
    scrollbar.config(command=text_labels.yview)
    scrollbar.pack(side='right', fill='y')
    text_labels.config(yscrollcommand=scrollbar.set)

    for f in range(100):
        tf[f] = tk.Frame(text_labels)
        e_find = tk.Entry(tf[f])
        e_replace = tk.Entry(tf[f])
        e_find.pack(side='left')
        e_replace.pack(side='left')
        tf[f].pack()
    frame_fields.pack()

我应该如何更改此代码,以便 text_labels 滚动?

解决方案

下面的代码创建可滚动单元格:

def create_cells(self):
    """Create cells for exception text"""
    # ----------------------------------------------
    frame_fieldsscroll = tk.Text(self.main, relief='flat')
    text_fields = tk.Text(frame_fieldsscroll, relief='flat')
    frame_fieldsscroll.window_create('insert', window=text_fields)

    tf = {}
    for f in range(31):

        e_find = tk.Entry(text_fields, width=16, relief='flat')
        e_replace = tk.Entry(text_fields, width=16, relief='flat')

        e_find.insert(0, 'find'+str(f))
        e_replace.insert(0, 'replace'+str(f))

        e_find.grid(row=0, column=0)
        e_replace.grid(row=0, column=1)

        text_fields.window_create('insert', window=e_find)
        text_fields.window_create('insert', window=e_replace)

        text_fields.insert('end', '\n')

    scrollbar = tk.Scrollbar(frame_fieldsscroll, width=15)
    scrollbar.config(command=text_fields.yview)
    text_fields.config(yscrollcommand=scrollbar.set)

    frame_fieldsscroll.pack()
    scrollbar.pack(side='right', fill='y')
    text_fields.pack(fill='both')

    text_fields.configure(state='disabled')
    frame_fieldsscroll.configure(state='disabled')

I've embedded 100 rows in 2 columns. Each row is inside a single frame, and all packed inside a text widget. I'm trying to add a scroll bar, but it scrolls only text, while the columns won't move.

    frame_fields = tk.Text(self.window)
    frame_fields.pack_propagate(0)
    tf = {}
    text_labels = tk.Text(frame_fields)
    text_labels.pack(side='left', expand='yes', fill='both')

    scrollbar = tk.Scrollbar(frame_fields)
    scrollbar.config(command=text_labels.yview)
    scrollbar.pack(side='right', fill='y')
    text_labels.config(yscrollcommand=scrollbar.set)

    for f in range(100):
        tf[f] = tk.Frame(text_labels)
        e_find = tk.Entry(tf[f])
        e_replace = tk.Entry(tf[f])
        e_find.pack(side='left')
        e_replace.pack(side='left')
        tf[f].pack()
    frame_fields.pack()

How should I change this code, so text_labels scroll?

A Solution

The code below creates the scrollable cells:

def create_cells(self):
    """Create cells for exception text"""
    # ----------------------------------------------
    frame_fieldsscroll = tk.Text(self.main, relief='flat')
    text_fields = tk.Text(frame_fieldsscroll, relief='flat')
    frame_fieldsscroll.window_create('insert', window=text_fields)

    tf = {}
    for f in range(31):

        e_find = tk.Entry(text_fields, width=16, relief='flat')
        e_replace = tk.Entry(text_fields, width=16, relief='flat')

        e_find.insert(0, 'find'+str(f))
        e_replace.insert(0, 'replace'+str(f))

        e_find.grid(row=0, column=0)
        e_replace.grid(row=0, column=1)

        text_fields.window_create('insert', window=e_find)
        text_fields.window_create('insert', window=e_replace)

        text_fields.insert('end', '\n')

    scrollbar = tk.Scrollbar(frame_fieldsscroll, width=15)
    scrollbar.config(command=text_fields.yview)
    text_fields.config(yscrollcommand=scrollbar.set)

    frame_fieldsscroll.pack()
    scrollbar.pack(side='right', fill='y')
    text_fields.pack(fill='both')

    text_fields.configure(state='disabled')
    frame_fieldsscroll.configure(state='disabled')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若能看破又如何 2024-12-09 19:16:25

要使窗口中的对象与文本一起滚动,您必须使用 window_create 方法,而不是使用 packgridplace

For objects in a window to be scrollable along with the text you must add them with the window_create method of the text widget instead of using pack or grid or place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文