0

Hi guys I want to create a basic GUI with TKinter.

This is my code

from tkinter import*
import tkinter.font

win =Tk()
win.title("Test")
myFont = tkinter.font.Font( family = 'Helvetica', size = 12, weight = 'bold')

def ledToggle():

        print("Hello World")


button1 = Button(win,text ='Test', font = myFont,command = ledToggle, bg='bisque2$
button1.grid(row=0,column=1)

I get this error message:

   Traceback (most recent call last):
  File "gui.py", line 4, in <module>
    win =Tk()
  File "/usr/lib/python3.5/tkinter/__init__.py", line 1880, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
Shalomi90
  • 121
  • 1
  • 1
  • 5

2 Answers2

5

this error usually happens when you access the RPI via SSH, you could run:

export DISPLAY=0:0

or add it to your bashrc if you want it to be perminantly excuted on every therminal run and ssh connection.

EDIT (a great suggestion by roger-jones):

you can also set the variable by prefixing it to the command: DISPLAY=0:0 python gui.py.

If you SSH with PuTTY you can also add the DISPLAY to the environment at "Connection">"Data">"Environment Variables"

if your not satisfied, check out similar errors as yours: link

aa-samad
  • 166
  • 2
2

I've come up with a solution!

import Tkinter
import sys
import os

# check if 
if os.environ.get('DISPLAY','') == '':
    print('no display found. Using :0.0')
    os.environ.__setitem__('DISPLAY', ':0.0')

master = Tkinter.Tk()
#master.title("tester")
#master.geometry("300x100")
# Lay out label
#label.pack()

# Run forever!
master.mainloop()
Brian Lee
  • 21
  • 1