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
6
  • Why do you have to run startx manually, is your desktop environment broken or incomplete? And what kind of display we are talking about? HDMI? Commented Jun 17, 2019 at 9:03
  • @DmitryGrigoryev do you have any ideas for me?
    – Shalomi90
    Commented Jun 17, 2019 at 10:23
  • Its connected with LVDS not HDMI
    – Shalomi90
    Commented Jun 17, 2019 at 11:05
  • How are you launching this program? Seems like you're doing it from a non-graphical context where there is no display defined...
    – Brick
    Commented Jun 17, 2019 at 13:02
  • this question is not related to the RPi .... it is a linux or python programming question
    – jsotola
    Commented Jun 17, 2019 at 13:17

2 Answers 2

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

2
  • For completeness you may want to add that you can also set the variable by prefixing it to the command: DISPLAY=:0 python gui.py. If SSHing in with PuTTY you can also add the DISPLAY to the environment at "Connection">"Data">"Environment Variables". Commented Jun 18, 2019 at 8:10
  • 1
    I tried this solution, and It said that it "cannot connect to display 0:0" Commented Jun 17, 2021 at 14:00
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()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.