r/learnpython • u/L_Y_H_ • 3d ago
What is the point of getcanvas()? And the method get()
Hi, I'm currently on an assignment that requires me to use this function, but not only have we never seen it in class but all the research I've done haven't help me at all. My teacher also mentions the method get() in the instruction and I'm also confused by it.
Edit More details
Long story short, my assignement is to create a "forest". For that I created functions to draw the trees but I still have to create a window and a button that allows the user to choose how many trees will be drawn in it. It's at that point that I have to use the "getcanvas()" to supposedly make a window and the "get()" in a function that will be used as a command in the button.
from random import*#Used in functions
from turtle import*
from tkinter import*
from math import* #Used in functions
master= getcanvas().master #Exact line my teacher wants us to use
screen = Screen()
screen.setup(800,500)
colormode(255)
screen.bgcolor(0, 46, 122)
speed(2000)
#What I'd like to do with it
def nbreSapin(): #Function in which I have to use 'get()' to get the number of trees
nb= int(input("How many trees:"))
if nb<100 and nb>0:
getsapin(nb)
else:
pass
4
u/Fun-Sandwich-2667 3d ago
Under the hood, Python's
turtlemodule draws inside atkinterwindow. Because you want to add a GUI text box and button to that same window instead of using terminalinput(), you need access to the underlying Tkinter window:getcanvas().master:getcanvas()grabs the Tkinter canvas that the turtle draws on, and.mastergives you the main parent window. Once you havemaster, you can attach Tkinter widgets (EntryandButton) directly to it..get(): This is a method on a TkinterEntry(input box) widget, not a standalone function. Instead ofinput("How many trees:")(which blocks the GUI), you read the user's input withentry.get().Here is how to wire it into your code:
3
u/BenjaminGal 3d ago
Can you tell us more about the context? Like what is the goal of your assignment and what packages/libraries are you using?