Zoey Schlemper
Zoey Schlemper

Reputation: 21

Some Tkinter create_line commands are not drawing vertical lines, but others do

I have a Tkinter project that includes a class for drawing lines. UPDATE: Here is a snippet you can run that reproduces the problem on my computer. I think it only refuses to draw vertical lines?

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()

and when I do this for some lines, route 29 - 34, about 1/2 of them are appearing, and I can't figure what is wrong with it. It seems to only cause error for certain lines, and I think it is only vertical drawings. Even diagonal drawings dont work. This was all working fine for the past month, until I started tinkering with the 'Unit' variable and adding a few more routes. To test, change the number pairs in the 'routeXInst' instantiation. Those number pairs are the start and end point of a line. (sorry if my vocab is strange, I am still new)

Upvotes: 1

Views: 343

Answers (1)

Zoey Schlemper
Zoey Schlemper

Reputation: 21

The problem is 1 character in my Class definition:

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

This would cause the second vertical point to be exactly the same as the first.

it obviously has to be: self.y2 = (y2 * 32) - Unit/2

I have succumbed to the dangers of copy and pasting to save time, instead costing me lots of frustration. Lesson learned: Don't rush through your code!!! Be very deliberate. Review it carefully. Feeling like a dunce :)

Upvotes: 1

Related Questions