可以使用tkinter treeview将文本换行吗? 我试图更改行高,但文本不会自动换行。基本上我想在行中有多行文本。
这是代码:
from tkinter import*
from tkinter import ttk
myApp = Tk()
NewTree= ttk.Treeview(myApp, height=5)
NewTree['show'] = 'headings'
s = ttk.Style()
s.configure('Treeview', rowheight=40)
NewTree["columns"]=("1","2")
NewTree.column("1", width=60)
NewTree.column("2", width=60)
NewTree.heading("1", text="col a")
NewTree.heading("2", text="col b")
item = NewTree.insert("", "end", values=("i want to wrap this text","and this text"))
NewTree.grid(row=0,column=0)
myApp.mainloop()
这是代码的第二个版本(当您可以在treeview中手动添加项目时),它在我上面发布的代码之前仅具有一个重要功能。我试图在lenght
上放置不同的值,但结果是相同的(不会自动换行):
from tkinter import*
from tkinter import ttk
from tkinter import messagebox
import textwrap
def wrap(string, lenght=15):
return '\n'.join(textwrap.wrap(string, lenght))
myApp = Tk()
def editact(event):
def double(event): # funkcija koja kreira celiju kada se klikne dupli klik
try:
if NewTree.identify_region(event.x, event.y) == 'cell':
# the user clicked on a cell
def ok(event):
"""Change item value."""
NewTree.set(item, column, entry.get())
entry.destroy()
column = NewTree.identify_column(event.x) # identify column
item = NewTree.identify_row(event.y) # identify item
x, y, width, height = NewTree.bbox(item, column)
value = NewTree.set(item, column)
elif NewTree.identify_region(event.x, event.y) == 'heading':
# the user clicked on a heading
def ok(event):
"""Change heading text."""
NewTree.heading(column, text=entry.get())
entry.destroy()
column = NewTree.identify_column(event.x) # identify column
# tree.bbox work only with items so we have to get the bbox of the heading differently
x, y, width, _ = NewTree.bbox(NewTree.get_children('')[0], column) # get x and width (same as the one of any cell in the column)
# get vertical coordinates (y1, y2)
y2 = y
# get bottom coordinate
while NewTree.identify_region(event.x, y2) != 'heading':
y2 -= 1
# get top coordinate
y1 = y2
while NewTree.identify_region(event.x, y1) == 'heading':
y1 -= 1
height = y2 - y1
y = y1
value = NewTree.heading(column, 'text')
elif NewTree.identify_region(event.x, event.y) == 'nothing':
column = NewTree.identify_column(event.x) # identify column
# check whether we are below the last row:
x, y, width, height = NewTree.bbox(NewTree.get_children('')[-1], column)
if event.y > y:
def ok(event):
"""Change item value."""
# create item
item = NewTree.insert("", "end", values=("", ""))
NewTree.set(item, column, entry.get())
entry.destroy()
y += height
value = ""
else:
return
else:
return
# display the Entry
entry = ttk.Entry(NewTree) # create edition entry
entry.place(x=x, y=y, width=width, height=height, anchor='nw') # display entry on top of cell
entry.insert(0, value) # put former value in entry
entry.bind('<FocusOut>', ok) #validate when you click on other cell
entry.focus_set()
except IndexError:
Error=messagebox.showinfo("Error!","You have 0 rows. Please add a new row.")
sys.exit() #za resavalje greske`
pass
NewTree.bind('<Double-Button-1>', double) #create new cell with double click
NewTree= ttk.Treeview(myApp, height=5)
NewTree['show'] = 'headings'
s = ttk.Style()
s.configure('Treeview', rowheight=60)
NewTree["columns"]=("1","2")
NewTree.column("1", width=100, anchor="center")
NewTree.column("2", width=100, anchor="w")
NewTree.heading("1", text="Col A")
NewTree.heading("2", text="Col B")
item = NewTree.insert("", "end", values=(wrap(""),wrap("")))
NewTree.item(item, tags=item)
NewTree.bind('<1>', editact)
NewTree.grid(row=0,column=0, columnspan=5, padx=5)
myApp.mainloop()
答案 0 :(得分:2)
答案 1 :(得分:1)
您可以使用textwrap
模块来定义此wrap
函数:
def wrap(string, lenght=8):
return '\n'.join(textwrap.wrap(string, lenght))
现在,您可以将item
替换为:
item = NewTree.insert("", "end", values=(wrap("i want to wrap this text"),
wrap("and this text")))
from tkinter import *
from tkinter import ttk
import textwrap
def wrap(string, lenght=8):
return '\n'.join(textwrap.wrap(string, lenght))
myApp = Tk()
NewTree = ttk.Treeview(myApp, height=5)
NewTree['show'] = 'headings'
s = ttk.Style()
s.configure('Treeview', rowheight=40)
NewTree["columns"] = ("1", "2")
NewTree.column("1", width=60)
NewTree.column("2", width=60)
NewTree.heading("1", text="col a")
NewTree.heading("2", text="col b")
item = NewTree.insert("", "end", values=(wrap("i want to wrap this text"),
wrap("and this text")))
NewTree.grid(row=0, column=0)
myApp.mainloop()