gpt4 book ai didi

python - Tkinter TreeView 小部件未正确对齐/在小部件之间添加空间

转载 作者:行者123 更新时间:2023-12-01 01:40:12 28 4
gpt4 key购买 nike

我正在 tkinter 中处理这个由一堆 Treeveiw 小部件制成的表。我的想法是得到一个表格,我可以在其中添加行、选择行并编辑它们。在下面的代码中,您可以通过按按钮向表格添加行。我现在想通过配置样式来控制每一行的高度。但是,当我使用 style 时, TreeView 小部件的对齐方式变得困惑,请参阅附图。有什么建议如何解决这个问题吗?

编辑:问题是小部件之间增加的空间。

该表的代码是:

from tkinter import *
from tkinter import ttk


class MyApp(Tk):
def __init__(self):
super(MyApp, self).__init__()

self.geometry('950x500+100+100')
self.NewTree = []

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

self.addLine()

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.
self.canvas = Canvas(frame2)
self.canvas.grid(row=0, column=0)

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

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

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

def addLine(self):
#Make button for adding step
bt = Button(self,text='Add Line',command=lambda: self.addLineMethod())
bt.config(width=9, height=1)
bt.pack()


def addLineMethod(self):


lineNumber = int(len(self.NewTree)/5)


for index in range(5):

s = ttk.Style()
s.configure('MyStyle.Treeview', rowheight=25)
self.NewTree.append(ttk.Treeview(self.table_frame, height=1,show="tree",columns=("0"),style='MyStyle.Treeview'))

#Works fine when using this line instead of those above
#self.NewTree.append(ttk.Treeview(self.table_frame, height=1,show="tree",columns=("0")))

self.NewTree[index+5*lineNumber].grid(row=lineNumber, column=index+1)
self.NewTree[index+5*lineNumber]['show'] = ''

item = str(index+5*lineNumber)
self.NewTree[index+5*lineNumber].column("0", width=180, anchor="w")
self.NewTree[index+5*lineNumber].insert("", "end",item,text=item,values=('"Text text text"'))


self.table_frame.update_idletasks() # Needed to make bbox info available.
bbox = self.canvas.bbox(ALL) # Get bounding box of canvas with Buttons.

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


app = MyApp()
app.mainloop()

她是一张带有一些线条的 table 的照片。

enter image description here

最佳答案

将样式配置放在__init__()函数中,效果就会消失。我不清楚为什么这有效。

def __init__(self):
...
s = ttk.Style()
s.configure('MyStyle.Treeview', rowheight=20)

关于python - Tkinter TreeView 小部件未正确对齐/在小部件之间添加空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51975723/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com