Skip to content

Instantly share code, notes, and snippets.

@jglee72
Created September 6, 2018 22:51
Show Gist options
  • Save jglee72/4f071865f5dcd777b0433884737315cc to your computer and use it in GitHub Desktop.
Save jglee72/4f071865f5dcd777b0433884737315cc to your computer and use it in GitHub Desktop.
calView3.py
# coding: utf-8
# https://gist.github.com/Phuket2/1430ac7f8eba11fdaff5
# https://forum.omz-software.com/topic/2953/calendar-view-class/2
import calendar
import datetime as dt
import ui
class CalendarView(ui.View): #here
''' See above references for original code.
Modified to 'return' a value via reference passing. View (GUI) is closed from calling function.
'''
def calendar_action(sender):
sender.datePushed=sender.caldate
def __init__ (self,fldname,dateval,action=calendar_action, *args, **kwargs): #here
ui.View.__init__(self, *args, **kwargs) # here
self.datePushed = 'init'
calendar.setfirstweekday(calendar.SUNDAY)
self.days = calendar.weekheader(3).split()
#self.width,self.height = ui.get_screen_size() #here
cv = ui.View(name=fldname)
cv.frame = (0,95,self.width,255)
cv.background_color = 'yellow'
cv.border_color = 'yellow'
cv.border_width = 2
self.view = cv
self.action = action
prv_mth = ui.Button(title='<')
prv_mth.frame = (5,5,50,25)
prv_mth.action = self.prev_pressed
self.day_color = prv_mth.tint_color
self.view.add_subview(prv_mth)
nxt_mth = ui.Button(title='>')
nxt_mth.frame = (56,5,50,25)
nxt_mth.action = self.next_pressed
self.view.add_subview(nxt_mth)
label = ui.Label(name='caltitle')
self.caldate = dateval #dt.datetime.strptime(dateval,'%d/%m/%Y')
self.curdate = curdate = dt.datetime.today()
label.text = str(self.caldate.strftime('%B %Y'))
label.frame = (107,5,200,25)
label.alignment = ui.ALIGN_CENTER
self.view.add_subview(label)
today_btn = ui.Button(title='Today')
today_btn.frame = (self.width-60,5,50,25)
today_btn.action = self.today_pressed
self.view.add_subview(today_btn)
self.firstdate = dt.date(self.caldate.year,self.caldate.month,1)
self.create_buttons()
self.draw_calendar()
def create_buttons(self):
for i in range(49):
daytitle = self.days[i] if i<7 else ''
button = ui.Button(name='day'+str(i),title=daytitle)
if i>=7:
button.action = self.button_pressed
button.frame = (5+(i%7)*51,31+(i/7)*31,50,30)
button.border_color = '#dadada'
button.border_width = 1
button.background_color = 'white' if i%7 else '#fff5f5'
self.view.add_subview(button)
def draw_calendar(self):
self.lastdate = self.last_day_of_month(self.firstdate)
self.firstweekday = self.firstdate.weekday()
self.firstweekday = (self.firstweekday + 1) % 7
last_day = self.lastdate.day
self.view['caltitle'].text = str(self.firstdate.strftime('%B %Y'))
for i in range(7,49):
dy = i-6-self.firstweekday
if (self.firstweekday+7<=i) and dy<=last_day:
strtitle = str(dy)
else:
strtitle = ''
self.view['day'+str(i)].title = strtitle
if (self.firstdate.year == self.curdate.year) and (self.firstdate.month == self.curdate.month) and (self.curdate.day == dy):
self.view['day'+str(i)].tint_color = 'red'
elif (self.firstdate.year == self.caldate.year) and (self.firstdate.month == self.caldate.month) and (self.caldate.day == dy):
self.view['day'+str(i)].tint_color = 'black'
else:
self.view['day'+str(i)].tint_color = self.day_color
def button_pressed(self,sender):
self.caldate = dt.date(self.firstdate.year,self.firstdate.month,int(sender.title))
if self.action:
(self.action)(self)
def prev_pressed(self,sender):
if self.firstdate.month == 1:
self.firstdate = dt.date(self.firstdate.year-1,12,1)
else:
self.firstdate = dt.date(self.firstdate.year,self.firstdate.month-1,1)
self.draw_calendar()
def next_pressed(self,sender):
if self.firstdate.month == 12:
self.firstdate = dt.date(self.firstdate.year+1,1,1)
else:
self.firstdate = dt.date(self.firstdate.year,self.firstdate.month+1,1)
self.draw_calendar()
def today_pressed(self,sender):
self.firstdate = dt.date(self.curdate.year,self.curdate.month,1)
self.draw_calendar()
def last_day_of_month(self,date):
if date.month == 12:
return date.replace(day=31)
return date.replace(month=date.month+1, day=1) - dt.timedelta(days=1)
'''if __name__ == '__main__':
vw = CalendarView('Calendar',dt.datetime.today(),calendar_action, frame = (0,0, 600, 800)) #here
vw.view.present('sheet')'''
# coding: utf-8
import datetime as dt
from calView3 import *
from textField import *
import console, time
import sys
def main():
'''Moneist: Money Atheist: monitor your daily balance based on instantaneous and sliding days. Balances are 'real', 'theoretical extended', and 'real extended'
'''
# month advance bill theoretical. static across all accounts
extend_month = 3
console.clear()
cv = ui.View(name='Moniest')
cv.hidden
# cv.frame = (0,10,375,280)
cv.background_color = .85, .93, 1.0,0.1
# cv.border_color = '#1b1b00'
# cv.border_width = 2
# Date picker Class Call for entry or ?
def calendar_call():
vw = CalendarView('Calendar',dt.datetime.today(), frame = (0,0, 600, 500))
vw.view.present('sheet')
while (vw.datePushed=='init'):
time.sleep(0.2)
print('Date Pushed: ',vw.datePushed)
# store local slide date (and reset to 'hello'?) and close view
slide_date = vw.datePushed
vw.close()
# vw.view.close()
# calendar_call()
ac = accountField(frame_loc=(20,380))
# ac.add_subview(cv)
# ac.bring_to_front
# ac.present('sheet')
cv.add_subview(ac)
# cv.bring_to_front
cv.present('sheet')
if __name__=='__main__':
main()
# coding: utf-8
import ui
class accountField (ui.View):
'''Each account display of current balance. associated button for adding transactions. editable field for balance update
'''
def __init__ (self,frame_loc=(0,0)):
self.acc_field= ui.TextView(frame=frame_loc+(150,64),bg_color=(1.0, .0, .0,0.5),text_color=('white'),font=('AmericanTypewriter-Bold',17), text=('text'),border_color=('#412190'),border_width=3,border_radius=20,alignment=ui.ALIGN_LEFT,alpha=0.5,selected=(False),editable=False)
#add items to the View
# self.add_subview(self.acc_field)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment