首页 > 解决方案 > 一些 Tkinter create_line 命令不绘制垂直线,但其他命令

问题描述

我有一个 Tkinter 项目,其中包括一个用于绘制线条的类。更新:这是一个您可以运行的片段,它可以在我的计算机上重现该问题。我认为它只拒绝绘制垂直线?

import tkinter as tk
Unit=32
class Region:
    def __init__(self, name, mapWidth, mapHeight, gen):
        self.name=name
        self.mapWidth=mapWidth
        self.mapHeight=mapHeight
        self.gen=gen
        self.locations={}
        self.current_loc_area = 1
        self.current_pokemon =[]

class Route:
    thick = 24
    global Unit
    def __init__(self, x1, y1, x2, y2, color, number):
        self.x1 = (x1 * 32)- (Unit/2)
        self.y1 = (y1 * 32)- (Unit/2)
        self.x2 = (x2 * 32)- (Unit/2)
        self.y2 = (y1 * 32)- (Unit/2)
        self.color = color
        self.number = number
        self.tag = None
        self.areaData = None
        self.loc_url='None'
        self.trimColors = ['#39314B','#EEA160']

    def constructRoute(self):
        self.tag = myRegion.name + "-route-" + str(self.number)
        C.create_line(self.x1, self.y1, self.x2, self.y2, fill=self.trimColors[0], width=self.thick+8)
        C.create_line(self.x1, self.y1, self.x2, self.y2, fill=self.trimColors[1], width=self.thick)
        C.create_line(self.x1, self.y1, self.x2, self.y2, fill=self.color, width=self.thick-8, tags=self.tag)

land_color='#BF7958'
root= tk.Tk()
myRegion = Region('johto', 24, 18, 2)
#create canvas-----
C=tk.Canvas(root, width=myRegion.mapWidth * 32, height=myRegion.mapHeight * 32)
C.pack()  

#Construct Routes and Towns--------------

route29Inst = Route(17,12, 20,12, land_color, 29)
route29 = route29Inst.constructRoute()

route30Inst = Route(8,7, 8,9, land_color, 30)
route30 = route30Inst.constructRoute()

route31Inst = Route(15,7, 16,7, land_color, 31)
route31 = route31Inst.constructRoute()

route32Inst = Route(14,8, 14,14, land_color, 32)
route32 = route32Inst.constructRoute()

route33Inst = Route(13,15, 14,15, land_color, 33)
route33 = route33Inst.constructRoute()

route34Inst = Route(11,15, 10,15, land_color, 34)
route34 = route34Inst.constructRoute()

root.mainloop()

当我对某些线路执行此操作时,路线 29 - 34,其中大约 1/2 出现了,我无法弄清楚它有什么问题。它似乎只会对某些线条造成错误,我认为这只是垂直图纸。即使是对角线图也不起作用。在过去的一个月里,这一切都很好,直到我开始修改“单位”变量并添加更多路线。要进行测试,请更改“routeXInst”实例中的数字对。这些数字对是一条线的起点和终点。(对不起,如果我的词汇很奇怪,我还是新手)

标签: pythonuser-interfacetkinterline

解决方案


问题是我的类定义中的 1 个字符:

self.y2 = ( y1 * 32) - 单位/2

这将导致第二个垂直点与第一个垂直点完全相同。

它显然必须是: self.y2 = ( y2 * 32) - Unit/2

我已经屈服于复制和粘贴的危险以节省时间,而不是让我感到很沮丧。经验教训:不要急于编写代码!!!要非常刻意。仔细审查。感觉像个笨蛋:)


推荐阅读