【发布时间】: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()
【问题讨论】:
-
请添加所有导入的模块,以及自定义函数的函数定义。
-
我不想粘贴太多代码,只限于相关部分。
ImportSection和ExportSection是包含几个按钮的框架。所以我看到的奇怪行为是垂直线永远不会放在这两个框架之间,而是在左/右(带注释的代码)或不显示在(带网格)。我将编辑问题以添加这两个类,以便代码示例完整并且可以运行。 -
您不要将分隔符放在与
ImportSection和ExportSection相同的框架中,因为您没有指定父级。因此,它被放在根窗口中。这就是为什么pack不起作用(self.sep已经放入root和grid)以及为什么它不在其他两个帧之间。将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