Tkinter中具有可变行高的表

时间:2018-07-22 17:50:08

标签: python tkinter treeview tabular row-height

我正在制作一个GUI,在其中要在类似Excel的表中显示一些数据。对于某些行,只有一行文本,而对于另一些行,则会有许多行。因此,我想要一个表格,其中行的高度不相同。

我发现这篇文章描述了如何使用treeview:Wrap text inside row in tkinter treeview

但是,我无法更改特定行的高度。

任何人都知道如何做到这一点,或者知道一种更好的方法来制作一张如上所述的桌子。

1 个答案:

答案 0 :(得分:0)

好的,所以我找到了解决我问题的方法。

受此帖子启发:tkinter Canvas Scrollbar with Grid?。我想出了以下解决方案。 Python对我来说是新手,所以它可能做得更优雅:-)

我现在面临的问题是标签宽度以文本单位给出,而换行长度以屏幕单位给出。是否有找到两者之间转换系数的方法?

from tkinter import *
import math
class MyApp(Tk):
    def __init__(self):
        Tk.__init__(self)

        #self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.geometry('950x500+100+100')

        label = Label(self,text='Table with some data', font=("Arial Bold", 25))
        label.pack()

        master_frame = Frame(self, bd=3, relief=RIDGE)
        master_frame.pack(side=BOTTOM)

        # Create a frame for the canvas and scrollbar(s).
        frame2 = Frame(master_frame)
        frame2.pack(side = BOTTOM)

        # Add a canvas in that frame.
        canvas = Canvas(frame2)
        canvas.grid(row=0, column=0)

        # Create a vertical scrollbar linked to the canvas.
        vsbar = Scrollbar(frame2, orient=VERTICAL, command=canvas.yview)
        vsbar.grid(row=0, column=1, sticky=NS)
        canvas.configure(yscrollcommand=vsbar.set)

        # Create a frame on the canvas to contain the buttons.
        self.table_frame = Frame(canvas)

        #Add some info to table
        newLine = ["Step#","Process Type","Tool Info","Process details","Cost","Comments"]
        self.newRow(0,newLine)
        newLine = ["Step# Step#","Process Type","Tool Info","Process details","Cost","Comments"]
        self.newRow(1,newLine)
        newLine = ["Step# Step# Step#","Process Type","Tool Info","Process details","Cost","Comments"]
        self.newRow(2,newLine)
        newLine = ["Step# Step# Step# Step#","Process Type","Tool Info","Process details","Cost","Comments"]
        self.newRow(3,newLine)
        newLine = ["Step# Step# Step# Step# Step#","Process Type","Tool Info","Process details","Cost","Comments"]
        self.newRow(4,newLine)

        # Create canvas window to hold the buttons_frame.
        canvas.create_window((0,0), window=self.table_frame, anchor=NW)

        self.table_frame.update_idletasks()  # Needed to make bbox info available.
        bbox = canvas.bbox(ALL)  # Get bounding box of canvas with Buttons.
        #print('canvas.bbox(tk.ALL): {}'.format(bbox))

        # Define the scrollable region as entire canvas with only the desired
        # number of rows and columns displayed.
        canvas.configure(scrollregion=bbox, width=925, height=200)

    #Add a new line to the table     
    def newRow(self, rowNumber, info2Add):

        numberOfLines = []                  #List to store number of lines needed
        columnWidths = [7,20,20,40,10,30]   #Width of the different columns in the table - unit: text
        stringLength = []                   #Lengt of the strings in the info2Add list

        #Find the length of each element in the info2Add list 
        for item in info2Add:
            stringLength.append(len(item))

        #Find the number of lines needed for each column
        for index, item in enumerate(stringLength):
            #print(math.ceil(item/columnWidths[index]))
            numberOfLines.append(math.ceil(item/columnWidths[index]))

        #Find the maximum number of lines needed
        lineNumber = max(numberOfLines)

        #Define the 6 labels (columns) in each row consist of.                
        col1 = Label(self.table_frame, relief=RIDGE,text=info2Add[0], wraplength=60,bg='white')
        col1.grid(row=rowNumber, column=1, sticky='news')
        col1.config(width=7, height=lineNumber)
        col2 = Label(self.table_frame, relief=RIDGE,text=info2Add[1], wraplength=140,bg='white')
        col2.grid(row=rowNumber, column=2, sticky='news')
        col2.config(width=20, height=lineNumber)
        col3 = Label(self.table_frame, relief=RIDGE,text=info2Add[2], wraplength=140,bg='white')
        col3.grid(row=rowNumber, column=3, sticky='news')
        col3.config(width=20, height=lineNumber)
        col4 = Label(self.table_frame, relief=RIDGE,text=info2Add[3], wraplength=280,bg='white')
        col4.grid(row=rowNumber, column=4, sticky='news')
        col4.config(width=40, height=lineNumber)
        col5 = Label(self.table_frame, relief=RIDGE,text=info2Add[4], wraplength=70,bg='white')
        col5.grid(row=rowNumber, column=5, sticky='news')
        col5.config(width=10, height=lineNumber)
        col6 = Label(self.table_frame, relief=RIDGE,text=info2Add[5], wraplength=210,bg='white')
        col6.grid(row=rowNumber, column=6, sticky='news')
        col6.config(width=30, height=lineNumber)


app = MyApp()
app.mainloop()