0

I am trying to run a python tkinter GUI on my raspberry pi screen. Right now, I only have one HDMI port on the raspberry pi being used, that being the one for my screen. Whenever I run my code, I get an error

_tkinter.TclError: no display name and no $DISPLAY environment variable

Note that I execute that command on an ssh terminal on my laptop.

I have looked at previous answers to this question, such as local machine _tkinter.TclError: couldn't connect to display ":0" _tkinter.TclError: no display name and no $DISPLAY environment variable

but none seem to resolve the issue.

Tkinter GUI

#!/usr/bin/env python
try:
    import Tkinter as tk
except:
    import tkinter as tk
from PIL import Image, ImageTk


root = tk.Tk()
root.attributes('-fullscreen', True)
root.geometry("640x480")

#Define Canvas
canvas = tk.Canvas(root, width=1920, height=1080)
canvas.grid(row=0,column=0)



# translates an rgb tuple of int to a tkinter friendly color code
def _from_rgb(rgb):
    return "#%02x%02x%02x" % rgb

# Called when user presses View Log button
def viewLogRaise():
    #Hide Previous Windows
    canvas.itemconfigure(logButtonWindow, state="hidden")
    canvas.itemconfigure(titleLabelWindow, state="hidden")
    #Open Closed Windows
    canvas.itemconfigure(backButtonWindow, state="normal")
    canvas.itemconfigure(logTextWindow, state="normal")
    quote = """HAMLET: To be, or not to be--that is the question:
    Whether 'tis nobler in the mind to suffer
    The slings and arrows of outrageous fortune
    Or to take arms against a sea of troubles
    And by opposing end them. To die, to sleep--
    No more--and by a sleep to say we end
    The heartache, and the thousand natural shocks
    That flesh is heir to. 'Tis a consummation
    Devoutly to be wished."""
    logText.insert(tk.END, quote)

def backToMenu():
    #Hide Previous Windows
    canvas.itemconfigure(backButtonWindow, state="hidden")
    canvas.itemconfigure(logTextWindow, state="hidden")
    #Open Closed Windows
    canvas.itemconfigure(logButtonWindow, state="normal")
    canvas.itemconfigure(titleLabelWindow, state="normal")

# Background
pathToGif = "redpoly4.jpg"
# red_background=Image.open("redBackground.gif")
backgroundImage = ImageTk.PhotoImage(file=pathToGif)
canvas.background = backgroundImage
bg = canvas.create_image(0, 0, anchor=tk.NW, image=backgroundImage)
titleLabel = tk.Label(root,fg="white", text="FUZE",borderwidth=2,relief="solid", bg=_from_rgb((239, 36, 37)), font=("Courier", 100))
titleLabelWindow = canvas.create_window(320,120,window=titleLabel)
logButton = tk.Button(root,fg="white",text="View Log",command=viewLogRaise,borderwidth=2,relief="raised",bg=_from_rgb((239, 36, 37)), font=("Courier", 46))
logButtonWindow = canvas.create_window(320,350,window=logButton)
backButton = tk.Button(root,fg="white",text="Back",command=backToMenu,borderwidth=2,relief="raised",bg=_from_rgb((239, 36, 37)),font=("Courier", 20))
backButtonWindow = canvas.create_window(70,440,window=backButton)
canvas.itemconfigure(backButtonWindow, state="hidden")
logText=tk.Text(root,bg="white",height=22,width=55,borderwidth=2,relief="solid")
logTextWindow = canvas.create_window(350,220,window=logText)
canvas.itemconfigure(logTextWindow, state="hidden")
root.mainloop()
Lyra Orwell
  • 1,048
  • 4
  • 17
  • 46
  • 1
    You say other answers don't resolve the issue. Why not? What happens when you try them? Do you get a different error? The same error? Can you show the exact and complete error? You you run other x11-based programs on your pi? For example, xclock or xterm? – Bryan Oakley Jun 14 '20 at 13:10
  • Are you running the script in a SSH session? If yes, you need to set the environment `DISPLAY` before running the script. – acw1668 Jun 15 '20 at 08:25

0 Answers0