【问题标题】:How to insert a separator Frame between two other Frames如何在其他两个框架之间插入分隔框架
【发布时间】:2019-02-11 13:39:34
【问题描述】:

我正在尝试将两个框架与第三个框架分开,它应该看起来像一条垂直线。使用包装管理器,无论我如何将包装顺序和/或side 打乱为'left''right',它总是显示在最左侧或右侧。当我使用网格时,它根本不显示。下面是我的代码: 编辑:

我添加了 Import/Export Section 定义,所以代码是完整的工作示例。

class ImportSection(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)

        self.lbl_import = tk.Label(self, text='IMPORT', width=20)
        self.lbl_import.grid()


class ExportSection(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)

        self.lbl_export = tk.Label(self, text='EXPORT', width=20)
        self.lbl_export.grid()

class Main(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.import_section = ImportSection(self)
        self.export_section = ExportSection(self)
        self.sep = tk.Frame(width=2, bd=1, relief='sunken')

        # I tried to shuffle the order and experimented with left/right with no luck.
        # the line is always on the very right or left
        # self.import_section.pack(side='left', padx=5, pady=5, anchor='n')
        # self.export_section.pack(side='left', padx=5, pady=5, anchor='n')
        # self.sep.pack(side='left', fill='y', padx=5, pady=5)

        # another attempt with grid, but the line does not show at all
        self.import_section.grid(row=0, column=0, padx=5, pady=5, sticky='n')
        self.sep.grid(           row=0, column=1, padx=5, pady=5, sticky='ns')
        self.export_section.grid(row=0, column=2, padx=5, pady=5, sticky='n')

if __name__ == '__main__':
    root = tk.Tk()
    app = Main(root)
    # app.pack(side='top', fill='both', expand=True) - I used this version with pack
    app.grid()
    root.mainloop()

【问题讨论】:

  • 请添加所有导入的模块,以及自定义函数的函数定义。
  • 我不想粘贴太多代码,只限于相关部分。 ImportSectionExportSection 是包含几个按钮的框架。所以我看到的奇怪行为是垂直线永远不会放在这两个框架之间,而是在左/右(带注释的代码)或不显示在(带网格)。我将编辑问题以添加这两个类,以便代码示例完整并且可以运行。
  • 您不要将分隔符放在与ImportSectionExportSection 相同的框架中,因为您没有指定父级。因此,它被放在根窗口中。这就是为什么pack 不起作用(self.sep 已经放入rootgrid)以及为什么它不在其他两个帧之间。将self.sep = tk.Frame(width=2, bd=1, relief='sunken') 更改为self.sep = tk.Frame(self, width=2, bd=1, relief='sunken')
  • eeeh,我错过了self...如果您发布它,这将是一个可接受的答案。谢谢!

标签: python python-3.x tkinter


【解决方案1】:

你也许可以使用 ttk.Separator:

import tkinter as tk
from tkinter import ttk

class Main(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.import_section = tk.Frame(self)
        tk.Canvas(self.import_section, width=200, height=400, bg='cyan').grid(column=0, row=0)
        self.export_section = tk.Frame(self)
        tk.Canvas(self.export_section, width=200, height=400, bg='lightgreen').grid(column=0, row=0)

        self.sep = ttk.Separator(self, orient=tk.VERTICAL)

        self.import_section.grid(row=0, column=0, padx=5, pady=5, sticky='n')
        self.sep.grid(           row=0, column=1, padx=5, pady=5, sticky='ns')
        self.export_section.grid(row=0, column=2, padx=5, pady=5, sticky='n')

if __name__ == '__main__':
    root = tk.Tk()
    app = Main(root)
    app.grid()
    root.mainloop()

【讨论】:

  • 感谢您的建议。我会查看ttk.Separator,但我真正感兴趣的是为什么我发布的代码不起作用,而不是如何绘制垂直线,这可能可以通过多种方式完成。
【解决方案2】:

问题是您尝试用作分隔符的框架与ImportSectionExportSection 不在同一个框架中,因为您没有指定其父级。当您不指定父级时,tkinter 将使小部件成为根窗口的子级。这也是你不能packapp进入根窗口的原因:self.sep已经被grid放入根窗口。

改变

self.sep = tk.Frame(width=2, bd=1, relief='sunken')

self.sep = tk.Frame(self, width=2, bd=1, relief='sunken')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 2010-10-16
    • 2010-12-11
    • 2013-02-10
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多