简体   繁体   English

如何使Tkinter python脚本每X秒获取一次变量?

[英]How to make Tkinter python script take variable every X seconds?

First of excused my poor coding skills. 首先,请原谅我糟糕的编码技能。 Just started learning about a week ago 大约一周前才开始学习

Anyway, I'm trying to right now create a GUI for this weather/clock thing for a project. 无论如何,我现在正在尝试为此项目的天气/时钟事件创建一个GUI。 Right now I need it so that the GUI, I was thinking of using Tkinter, updates every second and basically pulled strings from this program. 现在我需要它,以便我在考虑使用Tkinter的GUI每秒更新一次,并且基本上从该程序中提取字符串。 Currently, the program calculates the time of sunrise and sunset and lunar cycle. 当前,该程序将计算日出和日落时间以及农历周期。

I tried two ways so far, the first method, running the entire program under one script, but it doesn't work due to the mainloop() prevent the calculation from repeating itself for the next day. 到目前为止,我尝试了两种方法,第一种方法是在一个脚本下运行整个程序,但是由于mainloop()阻止了第二天重复进行计算,因此无法正常工作。

So now I was hoping to separate the program into two parts. 所以现在我希望将程序分为两部分。 One with the calculations and the other as the main GUI program. 一个带有计算,另一个作为主GUI程序。 So what I did was basically. 所以我所做的基本上是。

Simplified Calculation Script (Its very long so I write the important part): 简化的计算脚本(很长,所以我写了重要的部分):

its day.py 它的day.py

import datetime
import math
import schedule
import astral

a = Astral()

def tick():
    d = datetime.datetimee.today()
    second = d.second
    minutes = d.minute
    hour = d.hour
    month = d.month
    print(str(hour) + ":" + str(minute) + ":" + str(second))

def mooninfo():
    global percent_illuminate
    global moon_phase
    moon_phase = a.moon_phase(date=datetime.date(year,month,day)
    percent_illuminate = VERY LONG EQUATIONS
    print("Moon is at... " + str(moon_phase))
    print(percent_illuminate)

schedule.every(1).second.do(tick)
schedule.every().day.do(mooninfo)

while True:
    schedule.run_pending()

And for the tkinter script I tried 对于tkinter脚本,我尝试了

from day import moon_phase

Instead of just importing the moon_phase it started to run the program printing out the time of day nonstop. 它不只是导入moon_phase,而是开始运行程序,打印出一天中的不间断时间。

So if anyone could help me 所以如果有人可以帮助我

  • Let the tkinter script update everytime the different variable, second, minutes, hour, day, month, and moon_phase changed. 每次更改不同的变量(秒,分钟,小时,天,月和moon_phase)时,让tkinter脚本进行更新。
  • Or is there a way to get around the mainloop() of tkinter and just make it run while True: loop too? 还是有办法绕过tkinter的mainloop()并使它在True:loop时运行?
  • Or any other better way to do it. 或任何其他更好的方法。

I saw that you can let Tkinter track the time making a digital clock of the sort but I need the calculations and also I am also sending variables over to an Arduino too (But I already figured that out). 我看到您可以让Tkinter跟踪制作数字时钟的时间,但是我需要计算,而且我也将变量发送到Arduino(但是我已经知道了)。

Any help is greatly appreciated, thanks 任何帮助,不胜感激,谢谢

You're importing a variable from a function. 您正在从函数中导入变量。 I think you should be importing the class (restructure day.py as a class first), then calling the mooninfo() method to return the value of the moon_phase variable. 我认为您应该导入该类(首先将day.py重组为一个类),然后调用mooninfo()方法以返回moon_phase变量的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM