2015-08-15 51 views
0

我写了一个python-cgi脚本(树莓),在浏览器中用一些数据显示页面。我将从浏览器中输入常用地址(//localhost/cgi-bin/myscript.py)的其他pc(linux,mac等)进入此页面。该页面将以全屏模式显示。我会在页面上有一个按钮,按下后,页面将关闭并退出浏览器。我试着用pygame按钮和submit(post或get method)按钮,但没有成功(我不是真正的专家!)。 有人可以帮助我吗? 感谢如何从cgi退出?

#!/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() 
+0

可能的重复:http://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window –

回答

0

你的问题不是一个Python的问题,这是一个JavaScript的问题,像@rob说,这是这个问题的一个副本:stackoverflow.com/questions/2076299/...

,总结它为您;您需要使用JavaScript来关闭窗口,但正如其发生的情况一样,JavaScript以沙盒方式阻止其关闭未由相同脚本在页面上打开的窗口,因此如果在您的窗口中打开一个选项卡或窗口浏览器,并导航到一个页面,该页面不能自杀,也不能杀死浏览器,它只能关闭并杀死它通过JavaScript启动的任何窗口。

你唯一能做的就是导航到不同的页面,比如在JavaScript中执行window.location = 'A DIFFERENT URL';你甚至可以导航到about:blank,在大多数浏览器上显示一个空白页面。

+0

我试过在javascript中的解决方案,但因为我得到了在页面上“关闭”ref,点击它没有任何反应。我在上面写了我的代码(尚未完成,例如最后的quit函数必须被删除)。正如你所看到的,我没有使用JavaScript(我什么都不知道!),也没有让页面变得动态(我每5秒刷新一次页面,简单地说)。根据你的回答,我不认为有可能得到我想要的。 – user3072083