0

I wrote a python-cgi script (raspberry) that shows a page in the browser with some data. I will enter in this page from an other pc (linux, mac, etc.) entering usual address (//localhost/cgi-bin/myscript.py) in the browser. The page will be shown in fullscreen mode. I would have a button on the page that, as pushed, the page will close and exit the browser. I tried with pygame button and with submit (post or get method) button but with no success (I'm not really expert!). Anybody can help me? Thanks

#!/usr/bin/python
import os.path, sys, datetime, time
import cgi, cgitb
import os
import RPi.GPIO as io
import pygame, random, pygame.font, pygame.event, pygame.draw, string

import common
pygame.init()
io.setmode(io.BCM)
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

ctime = datetime.datetime.now()

clock = ctime.strftime("%H:%M")
date = ctime.strftime("%A %d")

form = cgi.FieldStorage()

fileobj = open('/sys/bus/w1/devices/28-0000065fb5c1/w1_slave','r')
lines1 = fileobj.readlines()
fileobj.close()
Temp2 = int(5)

Temp= form.getvalue(Temp2)

print "Content-type: text/html\n\n"

def main():

        print "<html>"
        print "<head>"
        print "<meta http-equiv='refresh' content='5' >"

        print "</head>"
        print "<body>"
        print "<body bgcolor=""#000000"">"

        fileobj = open('/sys/bus/w1/devices/28-0000065fb5c1/w1_slave','r')
        lines1 = fileobj.readlines()
        fileobj.close()
        Temp2 = int(5)
        if (lines1[0][-4])=='Y':
                pos1=lines1[1].index('t=')
                Temp2 = int(lines1[1][pos1+2:])/1000
                Temp2=round(Temp2,1)
                time.sleep(1)

        print "<table style=""width:1920px"">"
        print "<tr height=""60"">"
        print "</tr>"
        print "<tr height=""400"">"

        print "<td width=""442px"" >"
        print "</td>"
        if Temp2 <= 32:

            print "<td width=""345""  background=""//192.168.10.32/dashblue.png"" >" 
            print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
            print "<p align=""center"">"
            print "<b>"
            print Temp2
            degree = chr(176)
            print degree
            print "</b>"
            print "</p>"
            print "</font>"
            print "</td>"
        elif Temp2 > 32 and Temp2 <= 37:
            print "<td width=""345""  background=""//192.168.10.32/dashgreen.png"" >" 
            print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
            print "<p align=""center"">"
            print "<b>"
            print Temp2
            degree = chr(176)
            print degree
            print "</b>"
            print "</p>"
            print "</font>"
            print "</td>"
        if Temp2 > 37:
            print "<td width=""345""  background=""//192.168.10.32/dashred.png"" >" 
            print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
            print "<p align=""center"">"
            print "<b>"
            print Temp2       
            degree = chr(176)
            print degree
            print "</b>"
            print "</p>"
            print "</font>"
            print "</td>" 
        file = open('/home/pi/Downloads/temperat.txt','r')
        lines = file.readlines()
        file.close()
        Temp = float(lines[0][:4])
        Temp=round(Temp,1)
        if Temp <= 22:

            print "<td width=""345""  background=""//192.168.10.32/dashblue.png"" >" 
            print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
            print "<p align=""center"">"
            print "<b>"
            print Temp
            degree = chr(176)
            print degree
            print "</b>"
            print "</p>"
            print "</font>"
            print "</td>"
        elif Temp > 22 and Temp <= 26:
            print "<td width=""345""  background=""//192.168.10.32/dashgreen.png"" >" 
            print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
            print "<p align=""center"">"
            print "<b>"
            print Temp
            degree = chr(176)
            print degree
            print "</b>"
            print "</p>"
            print "</font>"
            print "</td>"
        if Temp > 27:
            print "<td width=""345""  background=""//192.168.10.32/dashred.png"" >" 
            print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
            print "<p align=""center"">"
            print "<b>"
            print Temp       
            degree = chr(176)
            print degree
            print "</b>"
            print "</p>"
            print "</font>"
            print "</td>" 

        Hum=int(lines[1])
        print "<td width=""345""  background=""//192.168.10.32/dashblue.png"" >" 
        print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
        print"<p align=""center"">"
        print "<b>"
        print Hum
        degree = chr(37)
        print degree
        print "</b>"
        print "</p>"
        print "</td>"
        print "<td width=""6%"" >"
        print "</font>"
        print "</td>"

        print "</tr>"


        print "<tr height=""80"">"
        print "<td width=""6%"" >"
        print "</td>"
        print "<td width=""345"">" 
        print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
        print "</p>"
        print "<p align=""center"">"
        print "Water Temperature"
        print "</p>"
        print "</font>"
        print "</td>"   
        print "<td width=""345"" >"
        print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
        print "<p align=""center"">"
        print "Bath Temperature"
        print "</p>"
        print "</font>"
        print "</td>"       

        print "<td width=""345"" >"
        print "<font size=""7"" color=""#00FFF"" face=""sans-serif"" >"
        print"<p align=""center"">"
        print "Bath Humidity"
        print "</p>"
        print "</font>"
        print "</td>"
        print "<td width=""442px"" >"
        print "</td>"

        print "</tr>"

        print "</table>"

        print "<font size=""10"" color=""#00FFF"" face=""sans-serif"" >"
        print "<h1 align=""center"">now..%s&nbsp&nbsp&nbsp&nbsp%s</h1>" % (date,clock)

        print "</font>"
        print "</body>"
        print "</html>"
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_F6):
                pygame.quit()
                sys.exit()


if (__name__=='__main__'):
        main()
user3072083
  • 85
  • 1
  • 3
  • 12
  • Possible duplicate: http://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window – Robᵩ Aug 15 '15 at 18:42

1 Answers1

0

Your problem is not a Python problem, it's a JavaScript problem, and like @rob said, it's a duplicate of this question: stackoverflow.com/questions/2076299/...

And to summarize it for you; you'd need JavaScript to close the window, but as it happens JavaScript is sandboxed in a way that prevents it from closing a window that was not opened by the same script on the page, so if you open a tab or a window in your browser and navigate to a page, that page cannot kill itself nor can it kill the browser, it can only close and kill any windows it launches by JavaScript.

The only thing you can do is to navigate to a different page like executing window.location = 'A DIFFERENT URL' in JavaScript; you can even navigate to about:blank which shows a blank page on most browsers.

Community
  • 1
  • 1
mpcabd
  • 1,813
  • 15
  • 20
  • I tried the solution with javascript in the post but as I got the "close" ref on the page, clicking it nothing happens. I put, above, my code (not completed yet, the quit function at the end has to be removed, for instance). As you can see I didn't use javascript (I do not know anything about it!) neither for getting the page dynamic (I refresh the page every 5 seconds, simply). I don't think, as per you answer, it is possible to get what I want. – user3072083 Aug 16 '15 at 07:33