Skip to content Skip to sidebar Skip to footer

Python Tkinter First Line Not Drawing In Correct Y Location

In another post Brian Oakley provide some code that 99% solved an issue I was having. When I went to run the code everything was running much better with one small exception. The

Solution 1:

The reason is because of this statement:

self.DrawArea.configure(scrollregion=self.DrawArea.bbox("all"))

The scrollregion defined what part of the larger virtual canvas is visible, or can be made visible by scrolling it into view. It gives the bounds of a rectangle that doesn't necessarily have to be rooted at 0,0.

The above line of code will get the bounding box ("bbox") of all of the items on the canvas. The bounding box starts with the upper-left-most item, which, in your case is at 0,250. When you create the "dummy point", the bounding box starts at 0,0*.

You can see this by adding the following line right after setting the scrollregion:

print("the scrollregion is", self.DrawArea.cget("scrollregion"))

* You might notice that the scrollregion is off by a pixel or two. On my machine it shows it as -2 248 2002 302. This is because the bbox method isn't 100% precise. It will sometimes fudge a little for the sake of performance. The official documentation simply states:

The return value may overestimate the actual bounding box by a few pixels.

If you know your points will always be in range of 0,0 to, say, 3000,3000, you can directly set the scrollregion to that value. Using the result of bbox is just a handy shortcut to mean "everything currently on the canvas".

In your case, you can change that configure statement to look like this:

self.DrawArea.configure(scrollregion=(0,0,3000,3000))

Post a Comment for "Python Tkinter First Line Not Drawing In Correct Y Location"