首页 > 解决方案 > Python:使用 .grid() 用 5 列填充整个框架宽度

问题描述

我有一个 tkinter 应用程序,它在 GUI 中显示某些信息,并且有 5 列显示这些信息。要放置这些元素,我使用 .grid() 命令。问题是它实际上并没有跨越框架的整个宽度。这就是它现在给我的(突出显示的红色以显示列的位置):

预期的列名称是“组件”“目录”“文件名”“覆盖的行”和“覆盖的条件”。我希望这可以跨越白色框架的整个宽度,每列(其中 5 个)具有相等的间距。我怎样才能解决这个问题??

一些代码我必须填写数据:

def populate_metric_frame(self, frame, files_changed, utests_changed):

    tk.Label(frame, text="Component",  font=("Courier", 8), bg=Window.det_uncov_color).grid(
        sticky="NESW", row=0, column=0)

    tk.Label(frame, text="Directory",  font=("Courier", 8), bg=Window.det_uncov_color).grid(
        sticky="NESW", row=0, column=1)

    tk.Label(frame, text="File name",  font=("Courier", 8), bg=Window.det_uncov_color).grid(
        sticky="NESW", row=0, column=2)

    tk.Label(frame, text="Lines Covered",  font=("Courier", 8), bg=Window.det_uncov_color).grid(
        sticky="NESW", row=0, column=3)

    tk.Label(frame, text="Conditionals Covered",  font=("Courier", 8), bg=Window.det_uncov_color).grid(
        sticky="NESW", row=0, column=4)

    tk.Label(frame, text="----------------------",  font=("Courier", 8), bg=Window.det_uncov_color).grid(
        sticky="NESW", row=1, column=0, columnspan=5)


    #Many more items down here

我必须制作框架的一些代码:

    # Create and place the title for the "Developer Feedback" frame
    self.feedback_title = tk.Label(
        self.feedback_frame, text="Metrics", bg=Window.frame_color1, font=("Courier", 11))
    self.feedback_title.place(relwidth=1, anchor="nw")

    # Create and place the frame to hold the canvas for "Developer Feedback"
    self.metric_list_frame = tk.Frame(
        self.feedback_frame, bg=Window.frame_color1)
    for i in range(5):
        self.metric_list_frame.grid_columnconfigure(i, weight=1)
    self.metric_list_frame.place(rely=0.1, relwidth=1, relheight=0.9)

    # Create and place the canvas
    self.metric_list_canvas = tk.Canvas(
        self.metric_list_frame, bg=Window.det_text_color, bd=0)
    for i in range(5):
        self.metric_list_canvas.grid_columnconfigure(i, weight=1)
    self.metric_list_canvas.place(relwidth=1, relheight=1)

    # Create and place the frame to hold the text
    self.metric_list_canvas_frame = tk.Frame(
        self.metric_list_canvas, bg=Window.det_text_color)
    for i in range(5):
        self.metric_list_canvas_frame.grid_columnconfigure(i, weight=1)
    self.metric_list_canvas_frame.bind("<Configure>", lambda event,
                                       canvas=self.metric_list_canvas: self.onFrameConfigure(self.metric_list_canvas))

    # Create and place the scrollbar
    self.metric_list_scrollbar = tk.Scrollbar(
        self.metric_list_canvas, orient="vertical", command=self.metric_list_canvas.yview)
    self.metric_list_scrollbar.pack(side="right", fill="y")

    # Define how the scrollbar works
    self.metric_list_canvas.configure(
        yscrollcommand=self.metric_list_scrollbar.set)
    self.metric_list_canvas.create_window(
        (1, 1), window=self.metric_list_canvas_frame, anchor="nw")

    # Fill the table with a list of changed files
    self.populate_metric_frame(
        self.metric_list_canvas_frame, files_changed, utests_changed)

PS我很抱歉代码格式化的麻烦,但我还在开始,所以我还在学习最好的格式化方法。感谢您的任何帮助

编辑:建议的链接只解释了如何拉伸“------”,这也是我想要的。但我也需要列标题

标签: pythonpython-3.xuser-interfacetkintertkinter-canvas

解决方案


推荐阅读