如何在其他两个框架之间插入分隔框架

时间:2018-09-06 10:02:11

标签: python python-3.x tkinter

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

我添加了“导入/导出节”定义,因此代码是完整的工作示例。

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()

2 个答案:

答案 0 :(得分:2)

您可以使用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()

enter image description here

答案 1 :(得分:1)

问题是您要用作分隔符的框架与ImportSectionExportSection不在同一框架中,因为您未指定其父框架。如果不指定父项,则tkinter将使小部件成为根窗口的子级。这也是为什么您无法pack app进入根窗口的原因:self.sep已被grid置于根目录。

更改

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

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