一些Tkinter的create_line命令不能画出垂直线,但其他命令可以

2024-10-02 10:31:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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的线路出现了,我不知道有什么问题。它似乎只会导致某些线条的错误,我认为这只是垂直的绘图。即使是斜线画也不行。在过去的一个月里,这一切都很正常,直到我开始修补“Unit”变量并添加更多的路由。要进行测试,请更改“routeXInst”实例化中的数字对。这些数字对是一条线的起点和终点。(对不起,如果我的声音很奇怪,我还是新的)


Tags: nameselfnumberunitroutecolorx1x2
1条回答
网友
1楼 · 发布于 2024-10-02 10:31:45

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

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

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

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

我已经屈服于复制和粘贴的危险,以节省时间,而不是花费我很多挫折。经验教训:不要匆忙地浏览代码!!!要非常慎重。仔细复习。感觉像个傻瓜:)

相关问题 更多 >

    热门问题