'I'm not sure how to add this image to the screen (turtle)

I entered this turtle code to print a cookie on the screen, but the cookie doesn't actually show up.

import turtle

wn = turtle.Screen()
wn.title("Cookie Clicker")
wn.bgcolor("black")

wn.mainloop()

cookie = "cookie.gif"
turtle.register_shape(cookie)
turtle.shape(cookie)

Anyone know how to fix this? By the way I'm using PyCharm and 'cookie.gif' is in the project folder.



Solution 1:[1]

Assuming this isn't an animated GIF, the problem is you've put wn.mainloop() in the wrong place in your code. Generally, it's the last thing you do in a turtle program:

from turtle import Screen, Turtle

cookie = "cookie.gif"

screen = Screen()
screen.title("Cookie Clicker")
screen.bgcolor('black')

screen.register_shape(cookie)

turtle = Turtle()
turtle.shape(cookie)

screen.mainloop()

Solution 2:[2]

You could add an eventlistener, such as "mouseenter" and then console log every time the mouse hovers over an element.

If you test out this code, you will see that the number of times you have hovered over the list items will appear in the console.

here is a link to "mouseover" event documentation, which I used and edited to help simplify my answer.

https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseover_event

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>


  <ul id="test">
    <li>Check if item has been hovered over</li>
    <li>item 2</li>
  </ul>


  <script>
    let test = document.getElementById("test");
    // This handler will be executed only once when the cursor moves over the unordered list
    test.addEventListener("mouseenter", function(event) {
      // highlight the mouseenter target
      console.log("purple has been hovered over")
      event.target.style.color = "purple";
      // reset the color after a short delay
      setTimeout(function() {
        event.target.style.color = "";
      }, 500);
    }, false);
  </script>

</body>

</html>

Solution 3:[3]

Unfortunately, you can't do it via jQuery like that. You probably need to get the stylesheets for the document and manually iterate through them to find matches to your selector.

function getCss(cssSelector){
    var styleSheets = document.styleSheets;
    var matchedRules = [];
    for(var i = 0; i < styleSheets.length; i++) {
        var rules = styleSheets[i].rules || sheets[i].cssRules;
        for (var j = 0; j < rules.length; j++) {
            if (rules[j].selectorText === cssSelector) {
                matchedRules.push(rules[j].cssText);
            }
        }
    }
    return matchedRules;
}

You can then call the function to obtain the matched rules. For example:

getCss("a:hover");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cdlane
Solution 2
Solution 3