python:tkinter treeview颜色没有更新 [英] python : tkinter treeview colors are not updating

查看:368
本文介绍了python:tkinter treeview颜色没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,如果对格式有误,请原谅,如果需要,我会很乐意更改。

This is my first post, please excuse me if I made mistake in the format, I will gladly change it if required.

我正在为使用Tkinter进行科学数据分析。对于分子列表,可以在单独的图中表示四个。在侧面,我使用Treeview来显示有关所有分子的一些数字。 (不仅仅是显示的)当树状视图行与显示的图有关时,我希望该行的文本具有相同的颜色。

I'm creating an interface for scientific datas analysis using Tkinter. For a list of molecules, four can be represented in separate plots. On the side, I use a Treeview to show some numbers about all molecules. (not just the displayed ones) When a treeview row is about a displayed plot, i want that row's text to be the same color.

对于每个显示的图形,我放置在行上使用不同的标签表示它,然后使用tag方法将前景色更改为绘图的颜色。

For each displayed graph, I place a different tag on the row that represents it and then use the tag method to change the foreground color to the plot's color.

该代码以前可以正常工作,但是现在已停止工作,未对我的代码进行任何更改。使用标记设置前景色不会改变颜色。几行之后,我还使用该方法将行更改为粗体,并且可以正常工作。

The code used to work fine, but now it has stopped working without any changes to the my code. The setting of the foreground color with the tags doesn't change the color. A few lines later, I also use that method to change a row to be bold and it works fine.

我设法确认代码行已正确读取:如果我将颜色设置为无法识别的值,则按预期执行时会出现tkinter错误。此外,使用一些打印,if / elif在正确的时间按预期执行(逻辑测试中没有错误)。

I managed to confirm that the lines of code are read correctly : if i set the color to a value that is unrecognized, i get a tkinter error when executing as expected. Furthermore, using some prints, the if/elif are executed as expected at the correct moment (no error in the logic tests).

该代码在另一台计算机上运行良好我相信某些python软件包存在问题。两台计算机具有相同的ttk版本(0.3.1),在注意到该问题以确保它不是过时的软件包后,我更新了所有模块。

The code works fine on another computer leading me to believe there is a problem with some python packages. The two computers have the same ttk version (0.3.1) and I updated all my modules after noticing the problem to be sure it is not an outdated package.

对计算机所做的更改是现在已删除并重新安装anaconda和环境,并在使用的环境中添加了pyinstaller的安装(带有pip)(当我在原始环境中安装pyinstaller时,我已经修改了其他重要软件包

The only change that was made to the computer is the removal and re-installation of anaconda and the environment now with the added installation (with pip) of pyinstaller in the used environment (when I installed pyinstaller in the original environment, I had modified other important package by mistake and had to reinstall anaconda from scratch to have it work again)

我尝试创建另一个没有pyinstaller模块的相同环境,但得到的结果相同。

I have tried creating another identical environment without the pyinstaller module and I get the same result.

p>

我已经失去了卸载和重新安装anaconda修复问题的次数。如果可能的话,我真的不想不必重新安装它。

I have lost count of how many times I have uninstalled and reinstalled anaconda to fix problems. If possible, I would really like not to have to reinstall it all over again.

我已经隔离了构成树视图对象的接口代码。经过测试之后,下面的代码片段给了我同样的问题。

I have isolated the piece of the interface's code that makes the treeview object. After testing, the snip of code bellow gives me the same issue.

import tkinter as tk
from tkinter import ttk
import numpy as np

class Testy():
    def __init__(self, root):

        #Values set in other part of the interface
        self.Classes = ['Molecule1','Molecule2','Molecule3','Molecule4',
                        'Molecule5','Molecule6']
        self.Single_Kinetic_Menu_Var = [tk.StringVar(value = 'Molecule1'), 
                                        tk.StringVar(value = 'Molecule3'),
                                        tk.StringVar(value = 'Molecule4'), 
                                        tk.StringVar(value = 'Molecule5')]
        self.Experiment_Count = np.zeros([len(self.Classes),2])

        #Treeview widget making
        Tree = ttk.Treeview(root)
        Tree.grid(column = 0, row = 0)

        Headings = ('first count','second count')
        Tree['column'] = Headings

        Tree.column("#0", width=100, minwidth=100)
        Tree.heading("#0",text="Class")

        for i in range(len(Headings)) :
            Tree.column(Headings[i])
            Tree.heading(Headings[i], text = Headings[i])

        #Insert all classes and their counts
        Empty = []
        Total = []
        Total = list(Total)
        for Idx, Class in enumerate(self.Classes) :
            Values = []
            if Idx == len(self.Classes)-1 :
                for Number in self.Experiment_Count[Idx,:] :
                    Values.append(str(Number))
                    Empty.append('-')
                    Total.append(0)
            else :
                for Number in self.Experiment_Count[Idx,:] :
                    Values.append(str(Number))
            Values = tuple(Values)

            if Class == self.Single_Kinetic_Menu_Var[0].get() :
                Tree.insert("", Idx, text = Class, values=Values, tags = ('BLUE'))
                Tree.tag_configure('BLUE', foreground = 'blue')
            elif Class == self.Single_Kinetic_Menu_Var[1].get() :
                Tree.insert("", Idx, text = Class, values=Values, tags = ('RED'))
                Tree.tag_configure('RED', foreground = 'red')
            elif Class == self.Single_Kinetic_Menu_Var[2].get() :
                Tree.insert("", Idx, text = Class, values=Values, tags = ('GREEN'))
                Tree.tag_configure('GREEN', foreground = 'green')
            elif Class == self.Single_Kinetic_Menu_Var[3].get() :
                Tree.insert("", Idx, text = Class, values=Values, tags = ('CYAN'))
                Tree.tag_configure('CYAN', foreground = 'cyan')
            else :
                Tree.insert("", Idx, text = Class, values=Values)
        Tree.insert('', len(self.Classes), text = '-', values = tuple(Empty))
        Total = np.sum(self.Experiment_Count[:,:], axis = 0)
        Tree.insert('',len(self.Classes)+1, text = 'TOTAL', values = tuple(Total),
                    tags = ('BOLD'))
        Tree.tag_configure('BOLD', font = ('Calibri', 12, 'bold'))


def main():
    Master = tk.Tk()
    Master.title("interface")


    Testy(Master) 

    Master.mainloop()


if __name__ == '__main__' :
    main()

您可能会通过运行代码看到,我希望分子1、3、4和5的文本分别被着色为蓝色,红色,绿色和青色。但是,我只能看到它们是黑色的。

As you might see by running the code, I expect the text of molecules 1, 3, 4 and 5 to be colored blue, red, green and cyan respectively. However, I can only see them in black.

推荐答案

-您在cmd中使用的是什么版本的python?

-what version of python are you using (python -V) in cmd

-python的最后一个版本(3.7)似乎有颜色标记错误

-the last version(3.7) of python seems like it has bugs to color tags

-如果您使用的是python(3.7),则只需安装python 3.6(它也可能适用于较新的版本)

-if you are using python (3.7) just install python 3.6 (it may work with newer version also)

这篇关于python:tkinter treeview颜色没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆