0

I'm new to Python. Trying to create a background image. When I run the code all that appears is an empty Tkinter window. My source file and code are in the same directory. Thoughts?

from tkinter import *
import random
import time


Tk()
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="background.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.running = True
    def _mainloop(self):
        while 1:
            if self.running == True:
                for sprite in self.sprites:
                    sprite.move()
                self.tk.update_idletasks()
                self.tk.update()
                time.sleep(0.01)
                g = Game()
                g.mainloop()
Jkru
  • 1
  • 1
  • 2
    Calling `Tk()` multiple times never ends well. I think your PhotoImage is being created inside the first instance by default, but you're trying to display it in the second instance. – jasonharper Feb 22 '19 at 14:23
  • Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) – stovfl Feb 22 '19 at 15:24
  • That helped me get the tk window title, but the background is still the same. Any additional ideas on how to get the background to appear? Thanks! – Jkru Feb 22 '19 at 22:42

0 Answers0