Skip to main content
 首页 » 编程设计

python之在 Canvas 上创建一个可滚动的表格

2024年11月24日11findumars

我是 tkinter 的新手,我还不太了解它。我想使用网格中的标签在 Canvas 上创建一个可滚动的表格。我已经看到一些使用 pack-manager 的例子,这导致我的 ui 更加困惑,所以我想我宁愿坚持使用网格。但是,无论我选择 Canvas 多少个单元格, 框架相应地展开以显示所有单元格,因此不留下可滚动区域。我该如何处理这个问题?

非常感谢任何帮助。非常感谢。

import tkinter as tk 
 
def start_gui(): 
    main_window = MainWindow() 
    main_window.set_grid(20,4) 
    main_window.root.mainloop() 
 
class MainWindow: 
    def __init__(self, root = tk.Tk()): 
        self.root = root 
        self.root.title('Some Table') 
 
        self.frame = tk.Frame(root, width=100, height=100) 
        self.frame.grid(row = 0, column = 0) 
 
        self.canvas = tk.Canvas(self.frame, width = 100, height = 100) 
        self.canvas.configure(scrollregion=(0, 0, 0, 1000)) 
        self.canvas.grid(row = 0, column = 0) 
 
        self.vbar = tk.Scrollbar(self.frame, orient = 'vertical', command= self.canvas.yview) 
        self.vbar.grid(row = 0, column = 1, sticky = 'ns') 
 
        self.canvas.config(yscrollcommand = self.vbar.set) 
 
    def set_grid(self, rows, columns): 
        for i in range(rows): 
            for j in range(columns): 
                label = tk.Label(self.canvas, text = 'some label', relief = 'solid', width = 20) 
                label.grid(row = i, column = j) 
 
if __name__ == '__main__': 
    start_gui() 

请您参考如下方法:

欢迎使用 stackoverflow。

你不远了。我修改了您的代码,使 Canvas 和 Scrollbar 都成为 root 的子级。 Frame 是 canvas 的子对象,但显示为 Canvas 窗口对象,而不是使用网格。

配置回调绑定(bind)到 Frame 配置事件以将 Canvas 滚动区域设置为 Frame 大小,因为 Frame 随其内容改变大小。

import tkinter as tk 
 
def start_gui(): 
    main_window = MainWindow() 
    main_window.set_grid(20,4) 
    main_window.root.mainloop() 
 
class MainWindow: 
    def __init__(self, root = tk.Tk()): 
        self.root = root 
        self.root.title('Some Table') 
        root.columnconfigure( 0, weight=1 ) # Stretch Column 0 to fit width. 
        root.rowconfigure( 0, weight=1 ) # Stretch row 0 to fit height. 
 
        self.canvas = tk.Canvas(root) 
        self.canvas.grid(row = 0, column = 0, sticky = 'nsew') 
        # Make canvas fit the whole of root. Useful to play with sizes. 
 
        self.frame = tk.Frame(self.canvas) 
        self.canvas.create_window( 0, 0, window = self.frame, anchor=tk.NW ) 
        # Makes frame an object in canvas 
 
        self.vbar = tk.Scrollbar(root, orient = 'vertical', command= self.canvas.yview) 
        # The scrollbar is a child of root. 
        self.vbar.grid(row = 0, column = 1, sticky = 'ns') 
 
        self.canvas.config(yscrollcommand = self.vbar.set) 
 
        self.frame.bind('<Configure>', self.on_config)  
        # Bind on_config to a Frame config event. 
 
    def on_config( self, e ): 
        # print(e.widget, e) 
        # Set the canvas scrollregion to fit the whole of frame. 
        self.canvas.configure(scrollregion=(0, 0, e.width, e.height)) 
 
    def set_grid(self, rows, columns): 
        for i in range(rows): 
            for j in range(columns): 
                tk.Label(self.frame, text = str(i)+' : '+str(j), width = 20).grid(row = i, column = j) 
 
if __name__ == '__main__': 
    start_gui()