glory9211
glory9211

Reputation: 843

Change turtle.home() location in the canvas

I am making a GUI similar to paint using turtle and tkinter. I am making a button to take the cursor to the center of the screen using turtle.home() but the TurtleScreen's home is not at the centre ( it goes to midway top left corner)

import tkinter as tk
import Pmw
from tkinter import ttk
from turtle import TurtleScreen, RawTurtle


class TestApp():
    def __init__(self, root):
        self.root = root
        self.create_screen_turtle()

    def create_screen_turtle(self):
        self.canvas_frame = ttk.Frame(self.root)
        self.canvas = Pmw.ScrolledCanvas(self.canvas_frame,
                                         borderframe=1,
                                         labelpos='n',
                                         label_text='Drawing Board',
                                         usehullsize=1,
                                         hull_width=700,
                                         hull_height=600,
                                         )
        self.screen = TurtleScreen(self.canvas.interior())
        self.yertle = RawTurtle(self.screen)
        self.canvas.pack(anchor=tk.CENTER, padx=5,
                         pady=5, fill='both', expand=1)
        self.canvas_frame.pack(side=tk.LEFT, padx=0,
                               pady=5, fill='both', expand=1)

        # Go to home
        self.yertle.home()


if __name__ == '__main__':

    root = tk.Tk()
    app = TestApp(root)
    root.geometry('1000x600')
    root.mainloop()

Is this a problem of how I am using it inside the scrolled window or something else ??

Thank you in advance

Upvotes: 0

Views: 440

Answers (1)

cdlane
cdlane

Reputation: 41925

It's due to your use of Pmw -- if we replace it's ScrolledCanvas with the one that comes with turtle, then yertle starts out in the middle as expected:

import tkinter as tk
from turtle import TurtleScreen, RawTurtle, ScrolledCanvas

class TestApp():
    def __init__(self, root):
        self.root = root
        self.create_screen_turtle()

    def create_screen_turtle(self):
        self.canvas_frame = tk.Frame(self.root)
        self.canvas = ScrolledCanvas(self.canvas_frame)
        self.screen = TurtleScreen(self.canvas)
        self.yertle = RawTurtle(self.screen)

        self.canvas.pack(anchor=tk.CENTER, padx=5, pady=5, fill=tk.BOTH, expand=tk.YES)
        self.canvas_frame.pack(side=tk.LEFT, padx=0, pady=5, fill=tk.BOTH, expand=tk.YES)

if __name__ == '__main__':

    root = tk.Tk()
    app = TestApp(root)
    root.geometry('1000x600')
    root.mainloop()

It doesn't seem to be the arguments you supply to Pmw.ScrolledCanvas(). It also doesn't appear to be due to using ttk.Frame instead of tk.Frame. If we remove them both and simply do:

self.canvas_frame = tk.Frame(self.root)
self.canvas = ScrolledCanvas(self.canvas_frame)

in your orginal code, the problem persists. Perhaps it can be fixed by an argument you're not supplying. I would have suggested screen.setworldcoordinates() as a workaround but it doesn't appear to play well with Pmw.ScrolledCanvas either.

Upvotes: 1

Related Questions