首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTML,CSS,Python数字时钟

HTML,CSS,Python数字时钟
EN

Stack Overflow用户
提问于 2017-06-29 12:15:39
回答 1查看 1.5K关注 0票数 1

我不知道如何处理这件事。

我有一个服务器运行在我的Raspberry Pi 3通过Python瓶。这将与HTML文件和CSS文件进行通信。

我希望能够有一个时钟在HTML网页上显示当前的时间和日期,并不断更新它。我已经这样做了(后面显示的代码)。

我面临的主要困境是,我希望时钟在网页上并进行更新,但是需要能够使用PYTHON读取HTML,并根据HTML页面的时间执行一些代码。那么我该怎么做呢?需要将变量从HTML传递到Python吗?谢谢

更新时钟的代码(在HTML文件中):

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
<script>
function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('txt').innerHTML =
    h + ":" + m + ":" + s;
    var t = setTimeout(startTime, 500);
}
function checkTime(i) {
    if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
    return i;
}
</script>
</head>

<body onload="startTime()">

<div id="txt"></div>

</body>
</html>

<!-- Reference to code for clock: https://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_clock -->

EN

回答 1

Stack Overflow用户

发布于 2017-06-29 15:13:57

你可以使用php。使用html中的表单,使用php将输入字段的时间值写入文本文件,然后使用python读取输出文件的内容。

您的.py文件看起来应该是这样的(理想情况下,您的时间变量应该是一个变量、小时、秒等,但是您可能有更多,所以我已经放了一个循环)。

代码语言:javascript
复制
from time import sleep

def read():
    timevars = ['your variables here']
    while True:
        file=open("timedata.txt","r")
        data = file.read()
        for i in range(len(timevars)):
            if data == inlist[i]:
                print(data)
                file=open("timedata.txt","w")
                file.write("")
        file.close()
        sleep(0.01)     
read() #you will be able to see the vars in the python file now, but you 
       #need to adjust so it only prints out the most recent value

php已经足够基本了--只要打开文件,就可以写出来。

代码语言:javascript
复制
    <?php 
    $name = $_POST['writetime'];
    $fptime = fopen("timedata.txt", "w");
    fwrite($fptime, "");
    $savestring = $name;
    fwrite($fptime, $savestring);
    fclose($fptime);
    ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44824877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档