代码之家  ›  专栏  ›  技术社区  ›  MJB

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

  •  0
  • MJB  · 技术社区  · 6 年前

    我试着用第三个分开两帧,看起来像一条垂直线。使用packmanager,它总是显示在最左边或最右边,无论我如何改变打包顺序和/或 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 回复  |  直到 6 年前
        1
  •  1
  •   fhdrsdg    6 年前

    问题是您尝试用作分隔符的帧与 ImportSection ExportSection 因为您没有指定它的父级。当您没有指定父窗口时,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')
    
        2
  •  4
  •   Reblochon Masque    6 年前

    你可以用ttk.分离器:

    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