Table 在 tkinter 中具有可变行高
Table with variable row height in tkinter
我正在制作一个 GUI,我想在其中以 Excel 形式显示一些数据 table。对于某些行,只有一行文本,而对于其他行,会有多行。因此,我想要一个行高不相同的 table 。
我发现这个 post 描述了如何使用 treeview 制作带有换行文本的 table:
但是,我无法更改特定行的高度。
任何人都知道如何做到这一点或知道更好的方法来制作如上所述的 table。
好的,所以我找到了解决问题的方法。
受此 post 启发:。我想出了以下解决方案。 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()
我正在制作一个 GUI,我想在其中以 Excel 形式显示一些数据 table。对于某些行,只有一行文本,而对于其他行,会有多行。因此,我想要一个行高不相同的 table 。
我发现这个 post 描述了如何使用 treeview 制作带有换行文本的 table:
但是,我无法更改特定行的高度。
任何人都知道如何做到这一点或知道更好的方法来制作如上所述的 table。
好的,所以我找到了解决问题的方法。
受此 post 启发:
我现在面临的问题是标签宽度以文本为单位给出,而环绕长度以屏幕为单位。有没有办法找到两者之间的换算系数?
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()