简体   繁体   English

Tkinter Canvas将不会通过itemconfig更新颜色更改

[英]Tkinter Canvas will not update color change via itemconfig

I am working on a project which deals with a kind of a map which has some lines as paths. 我正在一个项目中,该项目处理一种地图,其中有一些线作为路径。 These paths can have two states, active and inactive with different colors. 这些路径可以具有两种状态,即活动状态和非活动状态以及不同的颜色。 There is a method which should change the fill-color. 有一种方法应该更改填充颜色。 I can't figure out why my canvas lines won't update. 我不知道为什么我的画布行不会更新。 Here's my simplified code: 这是我的简化代码:

import Tkinter

inactive = "#385c61"
active = "#7dd5cd"

map = Tkinter.Canvas()

class GuiPart(object):

    def __init__(self, master):
        #some code

    def initGui(self, master):
        map = Tkinter.Canvas(left, width=660, height=600, bg="#35424b", bd="0", highlightthickness="0")
        map.pack()
        global inactive, active
        pathA = map.create_line(135, c, 135, e, fill=inactive, width=w)

    def setActivePath(self):
        global active, inactive
        map.itemconfig(pathA, fill=active)
        map.update_idletasks()
        map.update()

root = Tkinter.Tk()
gui = GuiPart(root)
gui.initGui(root)

gui.setActivePath()

root.mainloop()

If I change the color with itemconfig inside initGui it works like it should, but later I have to call setActivePath from another class, so it has to be a own method. 如果我改变颜色与itemconfiginitGui它像它应该,但后来我有打电话给setActivePath从另一个类,所以它必须是一个自己的方法。 (I know I call map = Tkinter.Canvas(...) twice, but otherwise I get an error, have to fix that too) (我知道我两次调用map = Tkinter.Canvas(...) ,但是否则我得到一个错误,也必须修复该错误)

Any ideas? 有任何想法吗?

You need to make map and pathA attributes of the class so that setActivePath can reference it. 您需要创建pathA mappathA属性,以便setActivePath可以引用它。 This is why you were getting the error where you had to create two canvases. 这就是为什么在必须创建两个画布的地方出现错误的原因。

import Tkinter

inactive = "#385c61"
active = "#7dd5cd"

class GuiPart(object):

    def __init__(self, master):       
        #some code

    def initGui(self, master):                     
        self.map = Tkinter.Canvas(left, width=660, height=600, bg="#35424b", bd="0", highlightthickness="0")
        self.map.pack()    
        global inactive, active
        self.pathA = map.create_line(135, c, 135, e, fill=inactive, width=w)

    def setActivePath(self):
        global active, inactive
        self.map.itemconfig(self.pathA, fill=active)
        self.map.update_idletasks()
        self.map.update()

root = Tkinter.Tk()
gui = GuiPart(root)            
gui.initGui(root)

gui.setActivePath()

root.mainloop()

Everything else looks OK to me, though you are saying that this has not resolved your issue. 其他一切对我来说似乎都不错,尽管您说这还不能解决您的问题。 As far as I can see, self.map.itemconfig(self.pathA, fill=active) is correct use of this method (see here and here ) so it is possible that your problem is outside of the code you have posted. 据我self.map.itemconfig(self.pathA, fill=active)self.map.itemconfig(self.pathA, fill=active)是此方法的正确用法(请参见此处此处 ),因此您的问题很可能超出了已发布的代码范围。 I can't test the code properly as you have not included the code in the __init__ method and because on the computer I am using at sixth form only Python 3.4.1 is installed. 我无法正确测试代码,因为您尚未在__init__方法中包括该代码,因为在我使用的是第六种形式的计算机上,仅安装了Python 3.4.1。 Please edit your question to include the code in the __init__ method. 请编辑您的问题,以将代码包含在__init__方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM