Python Forum
TKinter coordinate system (canvas)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TKinter coordinate system (canvas)
#1
Hi.
I'm new to TKinter and I'm having trouble understanding the coordinate system being used. I can obviously use trial and error to draw whatever I want, but I prefer the system to make some sense to me so that I can make understandable code.

Anyway, for starters, I created a code snipet:
class Gui:
    def __init__(self, root):
        width, height = root.winfo_screenwidth(), root.winfo_screenheight()
        self.enter_fullscreen(root, width, height)

        self.canvas = Canvas(root)
        # self.canvas.config(highlightthickness=0)
        self.canvas.pack(fill=BOTH, expand=YES)

        self.screen = TurtleScreen(self.canvas)
        self.screen.clearscreen()
        self.screen.bgcolor("black")

        self.writer = RawTurtle(self.screen)
        self.writer.reset()
        self.writer.speed(0)

        self.draw_circle(180, "red", "blue")

    def enter_fullscreen(self, root, width, height):
        root.overrideredirect(True)
        root.geometry('%dx%d+0+0' % (width, height))

    def draw_circle(self, radius, bg_color, border_color):
        circle_writer = RawTurtle(self.screen)
        circle_writer.ht()
        self.canvas.create_oval(-radius/2, -radius/2, radius/2, radius/2, outline=border_color,
                           fill=bg_color, width=4)

def main():
    root = Tk()
    Gui(root = root)
    root.mainloop()

if __name__ == '__main__':
    main()
The image was expected to be generated in the middle or be cropped bottom left. Instead, I got an image on the top right-ish (full image) (the image might be inverted (left to right)). May I please be adviced on how the cavas coordinates work? If I use ScrolledCanvas, the coordinates seem to work as expected, but I wish to use Canvas instead.

Never mind, I found the problem was due to self.canvas.pack(fill=BOTH, expand=YES). On removing this line and setting Canvas dimensions when setting the canvas (self.canvas = Canvas(root, width = width, height = height)), it works perfectly now.
Reply


Messages In This Thread
TKinter coordinate system (canvas) - by Bhoot - Mar-23-2020, 12:47 PM
RE: TKinter coordinate system (canvas) - by Larz60+ - Mar-23-2020, 04:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter.TclError: can't invoke "canvas" command cybertooth 8 5,960 Feb-23-2023, 06:58 PM
Last Post: deanhystad
  [Tkinter] Clickable Rectangles Tkinter Canvas MrTim 4 8,846 May-11-2021, 10:01 PM
Last Post: MrTim
  [Tkinter] Draw a grid of Tkinter Canvas Rectangles MrTim 5 7,896 May-09-2021, 01:48 PM
Last Post: joe_momma
Thumbs Up tkinter canvas; different page sizes on different platforms? philipbergwerf 4 4,111 Mar-27-2021, 05:04 AM
Last Post: deanhystad
  how to resize image in canvas tkinter samuelmv30 2 17,745 Feb-06-2021, 03:35 PM
Last Post: joe_momma
  how to rotate lines in tkinter canvas helpmewithpython 1 3,415 Oct-06-2020, 06:56 PM
Last Post: deanhystad
  Tkinter function to clear old canvas and start a fresh "operation" zazas321 5 9,429 Oct-01-2020, 04:16 AM
Last Post: zazas321
  question on tkinter canvas PhotoImage gr3yali3n 1 2,124 Sep-05-2020, 12:18 PM
Last Post: Larz60+
  tkinter canvas help pythonprogrammer 5 3,332 Dec-23-2019, 08:32 AM
Last Post: Malt
  [Tkinter] Scrollbar doesn't work on Canvas in Tkinter DeanAseraf1 3 9,355 Sep-19-2019, 03:26 PM
Last Post: joe_momma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020