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
  • 5
  • 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? – Dmitry Grigoryev Jun 17 '19 at 09:03
  • @DmitryGrigoryev do you have any ideas for me? – Shalomi90 Jun 17 '19 at 10:23
  • Its connected with LVDS not HDMI – Shalomi90 Jun 17 '19 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 Jun 17 '19 at 13:02
  • this question is not related to the RPi .... it is a linux or python programming question – jsotola Jun 17 '19 at 13:17
  • I was able to get this all resolved following these steps: https://raspberrypi.stackexchange.com/a/118928/60683 — My program is now launching right as the pi boots up, full screen, before desktop or anything else appears. – Albert Renshaw Dec 05 '20 at 10:56

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
  • 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". – Roger Jones Jun 18 '19 at 08:10
  • 1
    I tried this solution, and It said that it "cannot connect to display 0:0" – Legacy Coding Jun 17 '21 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()
Brian Lee
  • 21
  • 1