繁体   English   中英

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

[英]Tkinter in python- why can I not create text?

我有一个在上面绘制图形的画布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不起作用

在这行上:

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

暂无
暂无

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

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