3 python 中 ttk 树视图的不同问题 [英] 3 Different issues with ttk treeviews in python
问题描述
我正在使用 Python 中带有多列的 treeview
做一个聊天客户端.
I am doing a chat client using a treeview
with multiple columns in Python.
这是treeview
的代码:
chat = ttk.Treeview(height="26", columns=("Nick","Mensaje","Hora"), selectmode="extended")
chat.heading('#1', text='Nick', anchor=W)
chat.heading('#2', text='Mensaje', anchor=W)
chat.heading('#3', text='Hora', anchor=W)
chat.column('#1', stretch=NO, minwidth=0, width=130)
chat.column('#2', stretch=NO, minwidth=0, width=620)
chat.column('#3', stretch=NO, minwidth=0, width=65)
chat.column('#0', stretch=NO, minwidth=0, width=0) #width 0 to not display it
我添加了这样的项目:
chat.insert("", "end", "", values=((user, message, time)), tags=(messageid))
chat.tag_configure(messageid, foreground='#ff0000')
现在,它完美运行(这里有一个截图作为例子): 但是最后一行代码更改该行中所有 3 列的颜色.我想要的是仅更改 #2 列(仅消息)的文本颜色,不是整行(不是 Nick 或 Time 列).我尝试了很长时间,但现在是凌晨 4 点,我投降了 ☹ 有什么办法吗?
Now, that works perfectly (here's a screenshot as an example): but that last line of code changes the colour of all 3 columns in that row. What I want is to change only the colour of the text of the #2 column (just the message) and not the entire row (not Nick or Time columns). I tried for a long time now but it's 4 AM and I surrender ☹ Is there any way to do it?
两周后更新
现在我尝试做 3 个不同的树视图(每个 1 列),结果是这样的:虽然这解决了颜色问题,但我有一个新问题:滚动条.有没有办法将滚动条绑定到 3 个不同的树视图?到目前为止,我所有的尝试都失败了,我只能使用滚动条移动一个树视图.可以绑定到 3 个树视图吗?(如果是:如何?,值得吗?,我应该吗?)
Now I tried to do 3 different treeviews (1 column each) and it ends up this way: Although that fix the colour issue, i have a new issue: The scrollbar. It's there a way to bound a scrollbar to 3 different treeviews? all my attemps had failed so far and i can move only one of the treeview with a scrollbar. It's possible to bound to 3 treeviews? (If yes: how?, worth?, should i?)
还有另一个问题:在 TTK python 中所有删除树视图边框的尝试都失败了.
And also another problem: all attempts to remove treeview border have failed in TTK python.
另一个问题是现在 Mensaje 树视图只显示第一个单词.不知道为什么:\这是关于第一个单词问题的新代码.
Another problem is that now the Mensaje treeview only displays the first word. No idea why neither :\ this is the new code about the first word issue.
chat2 = ttk.Treeview(height="28", columns="Mensaje", selectmode="extended")
chat2.heading('#1', text='Mensaje', anchor=CENTER)
chat2.column('#1', stretch=NO, minwidth=400, width=620)
chat2.column('#0', stretch=NO, minwidth=0, width=0)
这是一条消息:
BotGUI.chat2.insert("", "end", iid=(idmensajeactual), values=mensaje, tags=(messageid))
try:
BotGUI.chat2.tag_configure(messageid, foreground='#'+colorfuente) #tfl
except TclError:
print("[Error02] - can't assign colour of "+ usuario +".")
推荐答案
1.第一个问题:滚动条
一个解决方案包括创建一个顶级 ttk.Treeview 对象,并为每一列创建另一个树.滚动条激活链接到顶级树视图.这比在同一个树对象中包含三列要麻烦一些,但它有效:
1. First Question : Scrollbar
A solution consist of creating a top-level ttk.Treeview object, and another tree for every column. The scrollbars activations are linked to the top-level tree view. It's a bit more cumbersome than having the three columns in the same tree object, but it works :
# Top level Treeview object
bot = ttk.Treeview( Tkinter.Tk() )
# Columns (treeview objects also)
columns = create_columns( bot)
################################
## Scrollbars
vsb = ttk.Scrollbar( bot,
orient="vertical",
command = bot.yview
)
hsb = ttk.Scrollbar( bot,
orient="horizontal",
command = bot.xview
)
## Link scrollbars activation to top-level object
bot.configure( yscrollcommand=vsb.set,
xscrollcommand=hsb.set)
## Link scrollbar also to every columns
map ( lambda col : col.configure( yscrollcommand=vsb.set,xscrollcommand=hsb.set), columns )
第二个问题:边界/山脊
使用样式配置对象
Second Question : The border/ridge
Use the style configuration object
ttk.Style().configure( '.', # every class of object
relief = 'flat', # flat ridge for separator
borderwidth = 0, # zero width for the border
)
但是它在 Windows 上不起作用:这是一个错误(或功能 :p ).
However it will not work on Windows : it is a bug (or a feature :p ).
Windows 完全忽略 -borderwidth
参数.(有关 comp.lang.tcl
邮件列表的更多信息:http://coding.derkeiler.com/Archive/Tcl/comp.lang.tcl/2007-11/msg00923.html )
Windows completely ignores the -borderwidth
parameter. (more infos on the comp.lang.tcl
mailing list : http://coding.derkeiler.com/Archive/Tcl/comp.lang.tcl/2007-11/msg00923.html )
这是最简单的问题:参数 -values
期望一个 iterable
for 应用于每一列.例如:
That's the easiest question : the parameter -values
expect an iterable
for to apply to each columns. Ex:
for (col, value) in zip( tree.columns(), values ) :
col.insert(value)
这就是错误所在:一个 string
也是一个 iterable
!(它实际上是一个字符列表)所以当您尝试使用消息 "This is a message"
调用 insert
时,ttk 将应用 "This"
到第一列,"is"
到第二列,依此类推...要强制执行消息应该作为一个整体应用的事实,只需在末尾添加一个逗号:(idmensajeactual,)
That's where the bug is : a string
is also an iterable
! (it is literally a list of char) so when you try to call insert
with the message "This is a message"
, ttk will apply "This"
to the first column, "is"
to the second, and so on ...
To enforce the fact that the message should be applied as a whole, just add a coma at the end : (idmensajeactual,)
此代码有效:
chat2.insert("", "end", iid=(idmensajeactual,) , values=mensaje, tags=(messageid))
终于
我已经上传了我的存根作为 github gist.您可以在此处查看并根据您的需要进行调整:https://gist.github.com/lucasg/7643411
输出:
这篇关于3 python 中 ttk 树视图的不同问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!