Tkinter display error on docker

I am new to docker and I’m having trouble using tkinter on docker. Whenever I try to run my image I get

Traceback (most recent call last):
File “./StayAlive.py”, line 114, in
root = tkinter.Tk()
File “/usr/local/lib/python3.6/tkinter/init.py”, line 2017, in init
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

The python code is:

          import tkinter

          okToPressReturn = True

          hunger = 100
          day = 0



          def startGame(event):

              global okToPressReturn

              if okToPressReturn == False:
                  pass

              else:
   
                  startLabel.config(text="")
    
                  updateHunger()
                  updateDay()
                  updateDisplay()

                  okToPressReturn = False



          def updateDisplay():


              global hunger
              global day

              if hunger <= 50:
                  catPic.config(image = hungryphoto)
              else:
                  catPic.config(image = normalphoto)


              hungerLabel.config(text="Hunger: " + str(hunger))


              dayLabel.config(text="day: " + str(day))   

  
              catPic.after(100, updateDisplay)



          def updateHunger():


              global hunger


              hunger -= 1

              if isAlive():
 
                  hungerLabel.after(500, updateHunger)


          def updateDay():


              global day


              day += 1

              if isAlive():
   
                  dayLabel.after(5000, updateDay)



          def feed():

              global hunger

              if isAlive():
                  if hunger <= 95:
                      hunger += 20
                  else:
                      hunger -=20
    


          def isAlive():

              global hunger

              if hunger <= 0:
                 
                  startLabel.config(text="GAME OVER! YOU KILLED HIM/HER/IT!")     
                  return False
              else:
                  return True
    




          root = tkinter.Tk()

          root.title("Stay Alive!")

          root.geometry("500x300")

          startLabel = tkinter.Label(root, text="Press 'Return' to start!", font=('Helvetica', 12))
          startLabel.pack()


          hungerLabel = tkinter.Label(root, text="Hunger: " + str(hunger), font=('Helvetica', 12))
          hungerLabel.pack()

          dayLabel = tkinter.Label(root, text="Day: " + str(day), font=('Helvetica', 12))
          dayLabel.pack()

          hungryphoto = tkinter.PhotoImage(file="hungry.gif")
          normalphoto = tkinter.PhotoImage(file="normal.gif")


          catPic = tkinter.Label(root, image=normalphoto)
          catPic.pack()

          btnFeed = tkinter.Button(root, text="Feed Me", command=feed)
          btnFeed.pack()


          root.bind('<Return>', startGame)



          root.mainloop()

And my dockerfile looks like:

          FROM python:3
          ADD StayAlive.py /
          ADD hungry.gif /
          ADD normal.gif /
          RUN apt-get update
          RUN apt-get -y install apt-utils
          RUN apt-get -y install python python-tk idle python-pmw python-imaging
          CMD export DISPLAY =":0"
          CMD [ "python", "./StayAlive.py", "hungry.gif", "normal.gif" ]

There are also 2 .gifs in the directory called hungry.gif and alive.gif

I believe the proper way to set the environment variable would be this:

ENV DISPLAY :0

Also you can check on this article.

1 Like