Tkinter:在画布内制作一个框架,宽度为画布的宽度

时间:2019-11-21 18:45:56

标签: python canvas tkinter

我在画布内使用框架,因此可以在使用网格布局时添加滚动条。 我使用的是网格布局,因为显示的信息(结果)宽度并不完全相同,但是我希望列的尺寸相同。

但是,我希望框架(蓝色)通过在列之间平均分配所有空间来占用画布(黄色)的所有空间: image here

这是我上面显示的图片的代码:

import tkinter as tk

class SeeTransactions():

    def showInterface(self):

        self.frameCanvas = tk.Frame(window)
        self.frameCanvas.grid(row=0, column=0, pady=(5, 0), sticky='news')
        self.frameCanvas.grid_rowconfigure(0, weight=1)
        self.frameCanvas.grid_columnconfigure(0, weight=1)
        self.frameCanvas.grid_propagate(False)

        self.canvas = tk.Canvas(self.frameCanvas, bg = 'yellow')
        self.canvas.grid(row=0, column=0, sticky="news")

        self.frameDatabase = tk.Frame(self.canvas, bg = 'blue')
        self.frameDatabase.grid(row = 0, column = 0, sticky = 'news')

        self.canvas.create_window((0, 0), window=self.frameDatabase, anchor='nw')

        self.vsb = tk.Scrollbar(self.frameCanvas, orient="vertical", command=self.canvas.yview)
        self.vsb.grid(row=0, column=1, sticky='ns')
        self.canvas.configure(yscrollcommand=self.vsb.set)

        self.showDatabase()


    def showDatabase(self):

        result = >some list

        for row in range(len(result)):

            for column in range(1, 5):

                tk.Label(self.frameDatabase, text = result[row][column]).grid(row = row, column = column - 1)

                self.frameDatabase.columnconfigure(column, weight = 1, uniform = 'all')

            tk.Label(self.frameDatabase, text = result[row][5]).grid(row = row, column = 4, columnspan = 2, sticky = 'w')

        self.frameDatabase.columnconfigure(4, weight = 1, uniform = 'all')

        self.frameDatabase.update_idletasks()

        self.frameCanvas.config(width=891, height=400)

        self.canvas.config(scrollregion=self.canvas.bbox("all"))



window = tk.Tk()
window.title('MyBudget')
window.geometry('891x531')

seeTransactions = SeeTransactions()

seeTransactions.showInterface()

window.mainloop()

有人知道如何解决吗?如果是这样,请告诉我。 (使用python 3)

1 个答案:

答案 0 :(得分:0)

您需要将宽度规范添加到标签实例化,以及 在column configure语句中填充以扩展列以适合可用空间。

宽度规格可确保所有标签的尺寸均相同。

代码: 将tkinter导入为tk

class SeeTransactions():

    def showInterface(self):

        self.frameCanvas = tk.Frame(window)
        self.frameCanvas.grid(row=0, column=0, pady=(5, 0), sticky='news')
        self.frameCanvas.grid_rowconfigure(0, weight=1)
        self.frameCanvas.grid_columnconfigure(0, weight=1)
        self.frameCanvas.grid_propagate(False)

        self.canvas = tk.Canvas(self.frameCanvas, bg = 'yellow')
        self.canvas.grid(row=0, column=0, sticky="news")

        self.frameDatabase = tk.Frame(self.canvas, bg = 'blue')
        self.frameDatabase.grid(row = 0, column = 0, sticky = 'news')

        self.canvas.create_window((0, 0), window=self.frameDatabase, anchor='nw')

        self.vsb = tk.Scrollbar(self.frameCanvas, orient="vertical",     command=self.canvas.yview)
        self.vsb.grid(row=0, column=1, sticky='ns')
        self.canvas.configure(yscrollcommand=self.vsb.set)
        self.showDatabase()

def showDatabase(self):
    result = [[f'line{i}{j}' for i in range(10)] for j in range(20)]
    for row in range(len(result)):
        for column in range(1, 5):
            tk.Label(self.frameDatabase, text = result[row][column], width = 15).grid(row = row, column = column - 1)

            self.frameDatabase.columnconfigure(column, weight = 1, uniform = 'all',pad = 100)

        tk.Label(self.frameDatabase, text = result[row][5], width = 15).grid(row = row, column = 4, columnspan = 2, sticky = 'w')

    self.frameDatabase.columnconfigure(4, weight = 1, uniform = 'all',pad = 100)

    self.frameDatabase.update_idletasks()

    self.frameCanvas.config(width=891, height=400)

    self.canvas.config(scrollregion=self.canvas.bbox("all"))



window = tk.Tk()
window.title('MyBudget')
window.geometry('891x531')

seeTransactions = SeeTransactions()

seeTransactions.showInterface()