1

I got this code from a book called "Python for Kids," by Jason Briggs. This code was ran in Python 3.4.3. I don't have any outside coding experience outside from this book. I tried multiple possibilities to fix this and looked online, but I couldn't find anything that would work or help my problem. If you have new code or edits for this code, that would be helpful to me continuing to learn Python.

from tkinter import *  
import random  
import time  

class Game:
    def __init__(self):
       self.tk = Tk()  
       self.tk.title("Mr. Stick Man Races for The Exit")  
       self.tk_resizable(0, 0)  
       self.tk.wm_attributes("-topmost", 1)  
       self.canvas = Canvas(self.tk, width=500, height=500,          highlightthickness=0)      
       self.canvas.pack()  
       self.tk.update()  
       self.canvas_height = 500  
       self.canvas_width = 500  
       self.bg = PhotoImage(file="Wallpaper.gif")  
       w = self.bg.width()  
       h = self.bg.height()  
       for x in range(0, 5):
           for y in range(0, 5):
               self.canvas.create_image(x * w, y * h, image=self.bg,    anchor='nw')  
       self.sprites = []  
       self.running = True  

   def mainloop(self):
       while 1:
           if self.running == True:
               for sprite in self.sprites:
                   sprites.move()  
           self.tk.update_idletasks()  
       self.tk.update()  
       time.sleep(0.01)  
       g = Game()  
       g.mainloop()  

This code was supposed to make a window with a wallpaper I created in Gimp to fill the window. When I ran the code, nothing happened and no errors appeared. What I need help on is making a window with my wallpaper appear. If you can help, can you give me an explanation with code. I'm sorry if my mistakes are obvious.

1
  • 1
    Is that the full code? because if so, I can see so many issues with it Commented Dec 10, 2015 at 6:41

1 Answer 1

2

These two statements need to be all the way to the left, with no indentation:

g = Game()
g.mainloop()

The code class Game: creates a class, which can be thought of as a recipe for how to create a game. It does not actually create the game, it only provides code to create the game.

In order to actually create the game -- called instantiatiation -- you need to call Game as if it was a function. When you do g = Game() you are actually creating the game object and saving a reference to it. Unless you do this, the game will never be created. Thus, to create a instance of the game, you must define it in one step (class Game()) and create it in another (g = Game())


Warning, there are other problems in the code. This answers your specific question, but you need to fix the indentation of the def mainloop statemen, and there may be other issues.

The biggest problem is that this simply isn't the right way to do animation in Tkinter. It might be ok as a learning tool, but ultimately this is simply not proper usage of tkinter. I don't know if this code is straight from the book or if this is code you're trying on your own, but tkinter simply isn't designed to work this way. You shouldn't be creating your own mainloop function because tkinter has one that is built in.

To fix the mainloop issue, remove the existing mainloop function, and add this in its place (properly indented under class Game():

def mainloop(self):
    # start the animation
    self.animate()

    # start the event loop
    self.tk.mainloop()

def animate(self):
    if self.running == True:
        for sprite in self.sprites:
            sprites.move()  
        self.tk.after(10, self.animate)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I appreciate you taking time to read over the code to tell me the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.