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
asked Jun 17, 2019 at 8:30
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? Commented Jun 17, 2019 at 10:23
  • Its connected with LVDS not HDMI 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... Commented Jun 17, 2019 at 13:02
  • this question is not related to the RPi .... it is a linux or python programming question 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

answered Jun 17, 2019 at 19:44
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()
answered Jan 2, 2020 at 17:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.