Faruk
Posts: 54
Joined: Sat Sep 08, 2018 7:34 am

When autostart Tkinter GUI program "Tcl error" raising

Fri Dec 28, 2018 7:50 am

Hello guys. I have a "Tkinter" program on my pi. Tkinter is run with autostart. This worked good. Then I added watchdog class on my program(second py script has watchdog class. I imported on my program ). If Tkinter program does not finished process in x second, watchdog reboot to my rpi. I have taking this error after use watchdog. Console output:

Code: Select all

Traceback (most recent call last):
	File "/home/pi/Petra/PGUI.py", line 348, in <module>
		root=Tk()
	File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1823, in __init__
		self.tk=_tkinter.create(screenName, baseName, className, interactive, wantobject, useTk,sync,use)
TclError: no display name and no $DISPLAY enviroment variable
GUI ERROR 

My code this:

Code: Select all

#! /usr/bin/python
# -*-coding: utf-8 -*-


#-Library setting-#
import RPi.GPIO as gpio
import time
from timeit import default_timer as timer 
import serial
from Tkinter import Tk, Label
from PIL import ImageTk, Image
import threading
import subprocess
import pigpio
import os
import signal
import sys
from WDT import Watchdog


pi=pigpio.pi()
print (pi.connected)


gpio.setmode(gpio.BCM)
gpio.setwarnings(False)
gpio.setup(33, gpio.IN)
gpio.setup(30, gpio.OUT)
gpio.setup(39, gpio.OUT)


#-Buffer definition-#
dataarray=''
data=''
uartData=''
shadowdata=[]
dstflg=False
dspflg=False
dataflg=False
testflg=False
floorinfoflg=False
screenflg=False
soundflg=False
doorflg=False
control_count=0


#-Serial port config-#
try:
    stmSer=serial.Serial(port='/dev/ttyAMA0', baudrate=115200,
                  parity=serial.PARITY_NONE,
                  stopbits=serial.STOPBITS_ONE,
                  bytesize=serial.EIGHTBITS,
				  timeout=0.01     
                  )
except serial.SerialException:
    import traceback
    traceback.print_exc()
    print "Stmser except error"
    
def serialflush():
    stmSer.flushInput()
    stmSer.reset_input_buffer()

serialflush()

def screenopen():
    global screenflg

##    screenflg=False
    print "screen open start"
    
    gpio.setwarnings(False)
    gpio.output(39,1)
    pi.set_PWM_dutycycle(30,255)
    try:
        for i in range(0,155,30):
            pi.set_PWM_frequency(30,i)
            time.sleep(0.01)
        print "screen open end"
        
    except:
        import traceback
        traceback.print_exc()
        prgloop()

def screenclose():
    global screenflg

    
##    screenflg=True
    print "screenclose start"
    
    gpio.setwarnings(False)
    gpio.output(39,0)
    
    pi.set_PWM_frequency(30,255)
    try:
        for i in range(155,0,-5):
            pi.set_PWM_dutycycle(30,i)
            time.sleep(0.03)
        print "screenclose end"
        
        
    except:
        import traceback
        traceback.print_exc()
        prgloop()
    


#-GUI update according to data of 'CARSTAT'-#
def updateGUI(data):
    global left_character
    global right_character
    global call_up
    global call_down
    global travel_up
    global travel_down
    global chime
    global floorinfoflg
    global control
    global shadowdata
    global car_light
    global screenflg
    global soundflg
    global doorflg
    
    watchdog.reset()
    

    try:
        left_character=(data[10:12]).decode("hex")
        #print "left_character:"+left_character
        right_character=(data[12:14]).decode("hex")
        #print"right_character:"+right_character

        petra_status_1=format(ord(data[14:16].decode("hex")),'08b')
        #print "status:"+petra_status_1

        petra_status_2=format(ord(data[16:18].decode("hex")),'08b')
        

##        katno.config(text=left_character+right_character, font=("Arial", 300))
        
        #print "UpdaaeGUI work"

       
       

        screencl=threading.Thread(target=screenclose)
        screenop=threading.Thread(target=screenopen)
        if shadowdata!=data:

            chime=petra_status_1[7]
            travel_down=petra_status_1[4]
            travel_up=petra_status_1[3]
            car_light=petra_status_1[2]

            doorAopen=petra_status_2[3]
            doorAclose=petra_status_2[4]
            doorBopen=petra_status_2[5]
            doorBclose=petra_status_2[6]


            if car_light=='0' and screenflg==False :
                #screenclose()
                screenflg=True
                screencl.start()
            elif car_light=='1' and screenflg==True:
                #screenopen()
                screenflg=False
                screenop.start()

            #print "carlight"+car_light
            
            if travel_up=="1":
                image=Image.open("/home/pi/Petra/upArrow2.png")
                photo=ImageTk.PhotoImage(image)
                label.config(image=photo)
                label.photo_ref=photo

                if left_character=="1" or left_character=="2":
                    katno.config(text=left_character+right_character, font=("Arial", 210))
                    katno.place(x=0,y=40)
                else:
                    katno.config(text=left_character+right_character, font=("Arial", 300))
                    katno.place(x=-90,y=-30)
                    
            elif travel_down=="1":
                image=Image.open("/home/pi/Petra/downArrow2.png")
                photo=ImageTk.PhotoImage(image)
                label.config(image=photo)
                label.photo_ref=photo
                
                if left_character=="1" or left_character=="2":
                    katno.config(text=left_character+right_character, font=("Arial", 210))
                    katno.place(x=0,y=40)
                else:
                    katno.config(text=left_character+right_character, font=("Arial", 300))
                    katno.place(x=-90,y=-30)
                    
            elif travel_up=="0" and travel_down=="0":
                image=Image.open("/home/pi/Petra/upArrow2.png")
                photo=ImageTk.PhotoImage(image)
                label.config(image='')
                label.photo_ref=photo
                if left_character=="0":
                    katno.config(text=left_character+right_character, font=("Arial", 300))
                    katno.place(x=-150, y=-30)
                elif left_character=="1" or left_character=="2":
                    katno.config(text=left_character+right_character, font=("Arial", 300))
                    katno.place(x=-150, y=-30)
                else:
                    katno.config(text=left_character+right_character, font=("Arial", 300))
                    katno.place(x=-190, y=-30)
                #time.sleep(0.01)    
                
            else:
                print "travel conditions ERROR"

            
                
        else:
            time.sleep(0.01)
        #print "control GUI"
        dataarray=''
        root.after(20,loop)
       
    except:
        import traceback
        traceback.print_exc()
        print "UpdateGUI Error"
        prgloop()


#-Reset-#
def prgloop():
    print "reset loop"
    testflg=False
    root.after(20,loop)

    
###-Data Read -#
def readuart():
    global dataread
    global datalist
    global dataarray
    global dstflg
    global dspflg
    global testflg
    global stmSer
    
    while True:
        try:
            if stmSer.inWaiting()>0:
                dataread=stmSer.read(1)
                
                
                if testflg==True:
                    dstflg=True
                    dspflg=True
                
                if dataread=='@' and dstflg==False:
                   dstflg=True
                   dspflg=False
                   dataarray=''
                elif dataread=='$' and dspflg==False:
                    dspflg=True
                    dstflg=False
                    #print "dataarray: "+dataarray
                    return dataarray
                    import contr 
                
                else:
                    if dstflg==True and dspflg==False:
                        dataarray=dataarray+''.join(dataread)
                        
                    elif dstflg==True and dspflg==True:
                        print "Reset loop"
                        dataarray=''
                        uartData=''
                        testflg=False
                        dstflg=False
                        dspflg=False
                        root.after(50,readuart)
                        
                    elif dstflg==False and dspflg==False:
                        print "Reset loop"
                        dataarray=''
                        uartData=''
                        testflg=False
                        dstflg=False
                        dspflg=False
                        root.after(50,readuart)
                        
                    else:
                        print "ERROR!!"
                        
                    input_value = gpio.input(33)    ##Button Pgm_DW
                    if input_value == True:
                        testflg=True
                        dataarray=''
                        uartData=''
                        print('The button has been pressed...')
            else:
                time.sleep(0.02)
                #data gelmediği zaman program burada uykuya girmeli
                #print "Data bekleniyor."
        except:
            import traceback
            traceback.print_exc()
            print "readUart Error"
            prgloop()
            

    
#-Furcation to update function  -#
def loop():
    global dataread
    global datalist
    global dataarray
    global dstflg
    global uartData
    global dspflg
    global data
    global start_time
    global end_time
    global control_count

    start_time=time.time()
    
    dspflg=False

    try:
        uartData=readuart() 

        
    
        if uartData[:8]=='CARSTAT,':
            updateGUI(uartData)
            #print "data: "+uartData
            end_time=time.time()
            
##            print end_time-start_time
        else:
            #print "İnvalid Data"
            root.after(20,loop)
    
            
    except:
        print "Update ERROR"
        import traceback
        traceback.print_exc()
        prgloop()
    
#-Start first screen-#
screenOpBoot=threading.Thread(target=screenopen())
screenOpBoot.start()

#-Watchdog control-#
def myHandler():
    print "Watchdog expired"
    os.system('sudo reboot')
   
watchdog=Watchdog(5,myHandler) #Watchdog time 5 second


#-Graphical Design-#    
try:
    root=Tk()
    root.title('V0.8')
    root.overrideredirect(0)




    katno=Label(root, text='1', font=("Arial", 300), width=2, fg='white', bg='#2080FF')
    katno.place(x=-90, y=-40)
    katno.config(padx=170)
    

    image=Image.open('/home/pi/Petra/upArrow2.png')
    photo=ImageTk.PhotoImage(image)
    label=Label(image=photo, bg="#2080FF")
    label.image=photo
    label.place(x=30, y=50)

    image2=Image.open('/home/pi/Petra/image_new.jpg')
    photo2=ImageTk.PhotoImage(image2)
    label2=Label(image=photo2, bg="#142F5E")
    label2.image=photo2
    label2.config(height=375, width=480)
    label2.place(x=0, y=430)

        
    root.configure(background="#2080FF")
    root.geometry('480x400+0+0')

    
    root.after(30,loop)
    root.mainloop()

    
except:
    import traceback
    traceback.print_exc()
    print "GUI ERROR"
I don't know any relationship, I runned LCD screen on DPI. Also I connect via ssh to my pi. I searched this error but I could not fix it. How I fix this error? Thanks for helping.

Return to “Troubleshooting”