1

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. 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. 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.

So now I was hoping to separate the program into two parts. One with the calculations and the other as the main GUI program. So what I did was basically.

Simplified Calculation Script (Its very long so I write the important part):

its 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

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.

So if anyone could help me

  • Let the tkinter script update everytime the different variable, second, minutes, hour, day, month, and moon_phase changed.
  • Or is there a way to get around the mainloop() of tkinter and just make it run while True: loop too?
  • 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).

Any help is greatly appreciated, thanks

Kerbash
  • 43
  • 4

1 Answers1

0

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.

directive-41
  • 123
  • 10
  • 1
    Ok, I tried it out that that works now, thanks but is there any way for Tkinter to keep updating the time? Since the while True loop don't work with tkinter is there any other way I can make it so that tkinter repeats a command? Like import? – Kerbash Oct 24 '18 at 23:17
  • 1
    Tkinter includes a method called 'after' which is used to schedule a function to be called after a specified period of time. It is described on StackOverflow in many places https://stackoverflow.com/questions/25753632/tkinter-how-to-use-after-method This would be a better option than the schedule code you have right now. – scotty3785 Oct 25 '18 at 11:12
  • In Tkinter you can use the `after()` method to control a function that can do the same thing as your while loop without blocking the mainloop like a while statement does.. – Mike - SMT Oct 25 '18 at 14:50