Skip to content

Instantly share code, notes, and snippets.

@nerun
Last active July 24, 2023 19:58
Show Gist options
  • Save nerun/17e12f382f7d326f7699f123cb0b31dd to your computer and use it in GitHub Desktop.
Save nerun/17e12f382f7d326f7699f123cb0b31dd to your computer and use it in GitHub Desktop.
Calculadora de Data Juliana em Python 3 + tkinter + tkcalendar
#!/usr/bin/env python3
# CALCULADORA DE DATA JULIANA
# por Daniel Dias Rodrigues em 2459259.7188310185
# Usando algoritmos da Wikipédia.
# Agradecimentos a "Jc3s5h" da Wikipedia por me ajudar a corrigir o algoritmo.
################################################################################
# This source code is released under the license
# Creative Commons CC0 1.0 Universal Public Domain Dedication
# <https://creativecommons.org/publicdomain/zero/1.0>
################################################################################
import math, time
from tkinter import *
from tkcalendar import Calendar, DateEntry
# pip install tkcalendar
def Converter():
# GREGORIANO >>> NDJ
entResG.configure(state='normal')
entResG.delete(0, 'end')
try:
D = int(dd.get())
M = int(mm.get())
Y = int(yy.get()) * aC.get()
h = int(hh.get())
m = int(mi.get())
s = int(ss.get())
Ju = (1461 * (Y + 4800 + (M - 14)//12))//4 +(367 * (M - 2 - (12 * ((M - 14)//12))))//12 - (3 * ((Y + 4900 + (M - 14)//12)//100))//4 + D - 32075
Jfr = ((h - 12)/24) + (m/1440) + (s/86400)
NDJu = Ju + Jfr
entResG.insert(0,str(NDJu))
except ValueError:
entResG.insert(0,"Entre apenas números!")
entResG.configure(state='disabled')
# NDJ >>> GREGORIANO
entResJ.configure(state='normal')
entResJ.delete(0, 'end')
try:
Jfloat = float(NDJ.get())
J = int(Jfloat)
Jfrac = Jfloat - J
except ValueError:
J = Ju
Jfrac = Jfr
try:
# DATE
y = 4716
j = 1401
m = 2
n = 12
r = 4
p = 1461
v = 3
u = 5
s = 153
w = 2
B = 274277
C = -38
f = J + j + (((4 * J + B)//146097) * 3)//4 + C
e = (r * f) + v
g = math.fmod(e, p)//r
h = (u * g) + w
D = (math.fmod(h, s)//u) + 1
M = math.fmod(((h//s) + m), n) + 1
Y = (e//p) - y + (n + m - M)//n
# HOUR
H1 = Jfrac * 24
if H1 >= 12:
print(H1)
H1 -= 12
D += 1
else:
H1 += 12
H2 = int(H1)
M1 = (H1 - H2) * 60
M2 = int(M1)
S2 = round((M1 - M2) * 60)
#segundos
if S2 <= 9:
Sf = "0"+str(S2)
elif S2 == 60:
M2 += 1
S2 = 0
Sf = "00"
else:
Sf = str(S2)
# minutos
if M2 <= 9:
Mf = "0"+str(M2)
elif M2 == 60:
H2 += 1
M2 = 0
Mf = "00"
else:
Mf = str(M2)
# horas
if H2 <= 9:
Hf = "0"+str(H2)
elif H2 == 24:
Hf = "00"
else:
Hf = str(H2)
entResJ.insert(0,"%d/%d/%d, %s:%s:%s"% (D-2, M, Y, Hf, Mf, Sf))
except ValueError:
entResJ.insert(0,"Entre apenas números!")
entResJ.configure(state='disabled')
def DatePicker():
dateForm = Tk()
dateForm.title("Escolha mês e dia")
dateForm.geometry('305x255+370+330') # tamanho da janela (largura-altura-x-y)
dateForm.resizable(width=False, height=False)
calpicker = Calendar(dateForm, font="Arial 12", selectmode='day', cursor="hand2", year=int(yy.get()))
calpicker.grid(row=0, column=0, padx=5, pady=5)
def gotDate():
try:
gD = int(calpicker.get_date()[:2])
gM = int(calpicker.get_date()[3:5])
gY = int(calpicker.get_date()[6:])
entD.delete(0, 'end')
entM.delete(0, 'end')
entY.delete(0, 'end')
entD.insert(0, gD)
entM.insert(0, gM)
entY.insert(0, gY)
dateForm.destroy()
except ValueError:
dateForm.destroy()
btVoltar = Button(dateForm, text = 'Atualizar e Voltar', command=gotDate)
btVoltar.grid(row=1, column=0, pady=4)
def Sair():
quit()
root = Tk() # Cria um objeto janela de background
root.title("Calculadora de Data Juliana") # título da janela
window_width = 300
window_height = 258
screen_max_width = root.winfo_screenwidth()
screen_max_height = root.winfo_screenheight()
center_x = int(screen_max_width/2 - window_width/2)
center_y = int(screen_max_height/2 - window_height/2)
# tamanho e posição da janela (largura-altura-x-y)
root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
root.resizable(width=False, height=False) # impedir redimensionar
# define uma imagem
img = PhotoImage(file='icon.png')
# chama a imagem como ícone
root.iconphoto(True, img)
# Labels
labGreg = Label(root, justify=LEFT, anchor='w', text = 'Calendário Gregoriano para Data Juliana:')
labGreg.grid(row=0, column=0, padx=2, pady=2, sticky = W)
labD = Label(root, justify=LEFT, anchor='w', text = 'Dia:')
labD.grid(row=2, column=0, padx=2, pady=2, sticky = W)
labM = Label(root, justify=LEFT, anchor='w', text = 'Mês:')
labM.grid(row=2, column=0, padx=80, pady=2, sticky = W)
labY = Label(root, justify=LEFT, anchor='w', text = 'Ano:')
labY.grid(row=2, column=0, padx=162, pady=2, sticky = W)
labHoraUTC = Label(root, justify=LEFT, anchor='w', text = 'Hora UTC (Brasília +3):')
labHoraUTC.grid(row=3, column=0, padx=2, pady=2, sticky = W)
labHH = Label(root, justify=LEFT, anchor='w', text = 'h')
labHH.grid(row=3, column=0, padx=194, pady=2, sticky = W)
labMi = Label(root, justify=LEFT, anchor='w', text = 'm')
labMi.grid(row=3, column=0, padx=237, pady=2, sticky = W)
labSS = Label(root, justify=LEFT, anchor='w', text = 's')
labSS.grid(row=3, column=0, padx=284, pady=2, sticky = W)
labResG = Label(root, justify=LEFT, anchor='w', text = 'Data Juliana:')
labResG.grid(row=4, column=0, padx=16, pady=2, sticky = W)
labJul = Label(root, justify=LEFT, anchor='w', text = 'Data Juliana para Calendário Gregoriano:')
labJul.grid(row=5, column=0, padx=2, pady=2, sticky = W)
labNDJ = Label(root, justify=LEFT, anchor='w', text = 'Data Juliana:')
labNDJ.grid(row=6, column=0, padx=16, pady=2, sticky = W)
labResJ = Label(root, justify=LEFT, anchor='w', text = 'Data Gregoriana:')
labResJ.grid(row=7, column=0, padx=16, pady=2, sticky = W)
# Variáveis das Entries
dd = StringVar(root)
mm = StringVar(root)
yy = StringVar(root)
hh = StringVar(root)
mi = StringVar(root)
ss = StringVar(root)
aC = IntVar(root)
NDJ = StringVar(root)
# Entries, Checkboxes, Spinboxes
entD = Spinbox(root, justify=LEFT, bg = "white", textvariable=dd, width=3, from_=1, to=31) # aqui não precisa do anchor
entD.grid(row=2, column=0, padx=34, pady=2, sticky = W)
entD.delete(0, 'end')
entD.insert(0, time.localtime()[2])
entM = Spinbox(root, justify=LEFT, bg = "white", textvariable=mm, width=3, from_=1, to=12)
entM.grid(row=2, column=0, padx=116, pady=2, sticky = W)
entM.delete(0, 'end')
entM.insert(0, time.localtime()[1])
entY = Spinbox(root, justify=LEFT, bg = "white", textvariable=yy, width=4, from_=1, to=9999)
entY.grid(row=2, column=0, padx=198, pady=2, sticky = W)
entY.delete(0, 'end')
entY.insert(0, time.localtime()[0])
aC_checkbutton = Checkbutton(root, text='a.C.',variable=aC, onvalue=-1, offvalue=1) # não precisa "command", o botão converter chama
aC_checkbutton.grid(row=2, column=0, padx=246, pady=2, sticky = W) # a variável aC (-1 se selecionada, 1 se não)
aC.set(1)
entHH = Spinbox(root, justify=LEFT, bg = "white", textvariable=hh, width=2, from_=0, to=23)
entHH.grid(row=3, column=0, padx=162, pady=2, sticky = W)
entHH.delete(0, 'end')
entHH.insert(0, time.localtime()[3])
entMi = Spinbox(root, justify=LEFT, bg = "white", textvariable=mi, width=2, from_=0, to=59)
entMi.grid(row=3, column=0, padx=205, pady=2, sticky = W)
entMi.delete(0, 'end')
entMi.insert(0, time.localtime()[4])
entSS = Spinbox(root, justify=LEFT, bg = "white", textvariable=ss, width=2, from_=0, to=59)
entSS.grid(row=3, column=0, padx=252, pady=2, sticky = W)
entSS.delete(0, 'end')
entSS.insert(0, time.localtime()[5])
entResG = Entry(root, justify=LEFT, bg = "white", state='disabled', width=20)
entResG.grid(row=4, column=0, padx=110, pady=2, sticky = W)
entNDJ = Entry(root, justify=LEFT, bg = "white", width=20, textvariable=NDJ)
entNDJ.grid(row=6, column=0, padx=110, pady=2, sticky = W)
entResJ = Entry(root, justify=LEFT, bg = "white", state='disabled', width=17)
entResJ.grid(row=7, column=0, padx=134, pady=2, sticky = W)
Converter()
entNDJ.delete(0, 'end')
entNDJ.insert(0, entResG.get())
entResG.delete(0, 'end')
entResG.configure(state='disabled')
entResJ.delete(0, 'end')
entResJ.configure(state='disabled')
# botão: janela, texto do botão, comando (função) a executar
btDatePicker = Button(root, text = 'Escolha uma Data', command=DatePicker)
btDatePicker.grid(row=1, column=0, pady=2, padx=75, sticky=W)
btConverter = Button(root, text = 'Converter', command=Converter)
btConverter.grid(row=8, column=0, pady=2, padx=72, sticky=W)
btSair = Button(root, text = 'Sair', command=Sair)
btSair.grid(row=8, column=0, pady=2, padx=172, sticky=W)
# invoca o evento principal de loop
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment