python中的Tkinter-为什么我不能创建文本?

时间:2013-08-22 14:41:29

标签: python python-3.x tkinter

我有一个画布c,我画了一个图形,现在我想标记图形的点。无论出于何种原因,图表都在工作,但不是标签。

countries = starcoords.keys() #returns a list of all the countries I want to work with
for i in countries: #this part works
    (xcor1, ycor1) = starcoords[i][1] #basically, returns the coordinates of the country i'm working with
    for j in starcoords[i][2]: #starcoords[key][2] is a list of all the countries it connects to, then it goes through and draws lines to each country
        (xcor2, ycor2) = starcoords[j][1]
        if starcoords[i][0] == starcoords[j][0]:
            continent = starcoords[i][0]
            continentcolour = clustercoords[continent][0]
            c.create_line(xcor1, ycor1, xcor2, ycor2, fill=continentcolour,width=2, activefill='#900')
        else:
            c.create_line(xcor1, ycor1, xcor2, ycor2, fill="grey",width=2,activefill='#900')

for i in countries: #for whatever reason this part doesn't
    (xcor1, ycor1) = starcoords[i][1]
    print(xcor1,ycor1)
    c.create_text(xcor1, ycor1,fill='grey')

正如您所看到的,第一个for循环基本上将线条绘制到它所连接的每个国家/地区,并根据它是否属于同一个大陆来选择颜色。我不确定为什么c.create_line正在工作而c.create_text不是

1 个答案:

答案 0 :(得分:3)

在这一行:

c.create_text(xcor1, ycor1,fill='grey')

您没有指定text参数,因此不会绘制任何文本。

尝试:

for i in countries:
    (xcor1, ycor1) = starcoords[i][1]
    c.create_text(xcor1, ycor1,fill='grey', text=i) 
    #...assuming `i` is a string containing the name of the country