tkinter.Frame.Grid格式大小调整未正确显示

2024-06-09 22:57:27 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我正在制作一个表格,表格的标题为文本,用于命名列、存储数据的单元格和滚动。你知道吗

我遇到的问题是我的表显示不正确。标题应该显示在上面,下面没有空格。(除了我添加的小填充)单元格正确地显示在标题的正下方。你知道吗

在y方向滚动单元格。滚动也适用于单元格和标题的x方向。你知道吗

函数将单元格添加到框架中,只需使用网格(行、列)创建然后添加 报头只有1行,所以空白空间不应该存在。你知道吗

import tkinter as tk
import collections
from enum import Enum

window = tk.Tk() # Root (main) window

def main():
    window.title('Table')
    window.geometry("1024x600")
    window.update_idletasks()
    table_frame = tk.Frame(window, background='black')
    table_frame.pack(fill='x')
    table = Table(table_frame, 30, 15)
    print(id(table))

    window.mainloop()

class Table:

    def __init__(self, frame, rowCount, columnCount):
        self._rowCount = rowCount
        self._columnCount = columnCount

        main_frame = tk.Frame(frame, bg='blue')
        main_frame.pack(fill='both')

        self._headerCanvas = tk.Canvas(main_frame)
        self._headerCanvas.grid(row=0, column=0, pady=1, sticky='ew')

        self._cellCanvas = tk.Canvas(main_frame)
        self._cellCanvas.grid(row=1, column=0, sticky='ew')

        scroll_bar_y = tk.Scrollbar(main_frame, orient=tk.VERTICAL, command=self._cellCanvas.yview)
        scroll_bar_y.grid(row=1, column=1, padx=1, sticky='ns')

        scroll_bar_x = tk.Scrollbar(main_frame, orient=tk.HORIZONTAL, command=self.xViewScroll)
        scroll_bar_x.grid(row=2, column=0, pady=1, sticky='ew')

        main_frame.grid_columnconfigure(0, weight=1)
        main_frame.grid_rowconfigure(1, weight=1)

        self._cellCanvas.configure(xscrollcommand=scroll_bar_x.set, yscrollcommand=scroll_bar_y.set) #, width=(main_frame.winfo_width()-scroll_bar_y.winfo_width()))

        header_frame = tk.Frame(self._headerCanvas)
        self._headers = Table.ColumnHeaders(header_frame, self._columnCount)
        cell_frame = tk.Frame(self._cellCanvas)
        self._cells = Table.Cells(cell_frame, self._rowCount, self._columnCount)

        self._headerCanvas.create_window(0, 0, window=header_frame, anchor='nw')
        self._headerCanvas.update_idletasks()
        self._cellCanvas.create_window(0, 0, window=cell_frame, anchor='nw')
        self._cellCanvas.update_idletasks()
        self._headerCanvas.configure(scrollregion=self._cellCanvas.bbox("all"))
        self._cellCanvas.configure(scrollregion=self._cellCanvas.bbox("all"))

    def xViewScroll(self, *args):
        self._headerCanvas.xview(*args)
        self._cellCanvas.xview(*args)


    class Cells:

        class Types(Enum):
            Entry = 0
            Button = 1

        class Cell:
            def __init__(self, widget, text=''):
                self._text = text
                self._widget = widget


            def getWidget(self):
                return self._widget

            def setWidget(self, widget):
                self._widget = widget

            widget = property(getWidget, setWidget, "Get and set the widget of a cell.")


        def __init__(self, frame, rows, columns, cellTypes=Types.Entry):
            self._cells = [[],[]]

            for r in range(rows):
                self._cells.append([])
                for c in range(columns):
                    self._cells[r].append(c)
                    if cellTypes == Table.Cells.Types.Entry:
                        self._cells[r][c] = Table.Cells.Cell(tk.Entry(frame, width=15))
                    elif cellTypes == Table.Cells.Types.Button:
                        self._cells[r][c] = Table.Cells.Cell(tk.Button(frame, width=12))

                    self._cells[r][c].widget.grid(row=r, column=c)


        def getCell(self, row, column):
            return self._cells[row][column]

        def setCell(self, row, column, cell):
            self._cells[row][column] = cell

        cells = property(getCell, setCell, "Get and set a cell in the table.")


    class ColumnHeaders:
        def __init__(self, widget, columnCount):
            self._widget = widget
            self._columnCount = columnCount
            self._headers = Table.Cells(self._widget, 1, self._columnCount, cellTypes=Table.Cells.Types.Button)
main()

他们现在就是这样出现的。 Table not re-sized

重新调整屏幕大小后,这就是原版的外观。标题刚好在下面单元格的上方,只有填充空间。 Table re-sized

如果我再缩小一点,就会导致另一个问题滚动条和标题从视图中消失。 右边的滚动条永远不会消失。(我猜这是因为我不能再缩小屏幕了。 Table shrunk even more.

这就是main的情况_frame.grid\u行配置(1,重量=1) 它会在调整到较小的窗口后导致这种情况。 Table with main_frame.grid_rowconfigure(1, weight=1)


Tags: selfmaindeftablecolumnwidgetwindowframe
1条回答
网友
1楼 · 发布于 2024-06-09 22:57:27

Question: Frame Grid sizing not displaying correctly

您被误导了,header_frame没有使用.grid(...)布局。
使用.create_window(...将小部件添加到Canvas的固定位置0, 0, anchor='nw'。因此,“网格布局管理器”像自动调整大小一样神奇。你知道吗


为什么会有这种布局?

Note: main_frame == 'blue', _headerCanvas == 'lightgreen', _cellCanvas == 'red':

ABCD
Layout: A                             Layout: B                           Layout: C                             Layout: D

        self._headerCanvas = tk.Canvas(main_frame, bg='lightgreen')
        self._headerCanvas.grid(row=0, column=0, pady=1, sticky='ew')

        main_frame.grid_rowconfigure(1, weight=1)

        self._cellCanvas = tk.Canvas(main_frame, bg='red')
        self._cellCanvas.grid(row=1, column=0, sticky='ew')

布局:A: 使用默认值height创建两个Canvas。因此,您可以使用“网格布局管理器”获得类似的布局。你知道吗

布局:B: 将itmes添加到Canvas,这里的单元格Frame,不会改变布局中的任何内容。你知道吗


布局:C: 将Canvas height与中的header_frame height结果同步。你知道吗

  1. 使用.bind('<Configure>'

    header_frame = tk.Frame(self._headerCanvas)
    header_frame.bind('<Configure>', self.on_configure)
    
    def on_configure(self, event):
        # Sync _headerCanvas height with header_frame height
        w = event.widget
        self._headerCanvas.configure(height=w.winfo_height())
    
  2. 使用.update_idletask()

        header_frame = tk.Frame(self._headerCanvas)
        self._headers = Table.ColumnHeaders(header_frame, self._columnCount)
        root.update_idletasks()
        self._headerCanvas.configure(height=header_frame.winfo_height())
    

仍然不是你想要的?
布局:D: 您已经通过执行以下操作使.grid(row=1尽可能地增长:

    main_frame.grid_rowconfigure(1, weight=1)

但是您不允许Canvas使用sticky='ns'增长。这将导致上面/下面的'blue'空间。更改为sticky='nsew'。你知道吗

相关问题 更多 >