The ToolKit 5: Attack on ToolKit Chapter 1 Part 1 Finale Part 1
So, continuing where we left off last time (it was a single function that took a long while to get working)
we what we have left for the calendar is to:
- make the label change to the month of the calendar.
- output the results to the output terminal box thing.
- ????
- start word doc tool.
so pretty much after these two things the calendar will have proper functionality and later on after getting the word doc tool working, I'll spruce things up and make it look pretty.
so, let's begin.
to start I wanted to see if the calendar had text I could use, since if you go into the designer the calendar has a navigation bar that could be toggled, and the month was stated on it
and after a little searching I found that it's the selected date but set to a string, so in pyQt there is a toString function and there are various specifiers such as M which means month and the amount of M's changes the string you get back for example if you use January, one M's just gives 1, two M's gives 01, three M's give Jan, four M's give January.
so, with this new thing:
selected_date = self.calendar.selectedDate()
month_name = selected_date.toString("MMMM")
self.calendarMonth.setText(month_name)
added to our function:
def dateFinder(self):
year = int(self.yearBx.text())
month = self.monthSel.currentIndex() + 1
day = self.fDayOff.value() + self.dayAllot.value()
while day > QDate(year, month, 1).daysInMonth():
day -= QDate(year, month, 1).daysInMonth()
month += 1
if month > 12:
month = 1
year += 1
final_date = QDate(year, month, day)
self.calendar.setSelectedDate(final_date)
selected_date = self.calendar.selectedDate()
month_name = selected_date.toString("MMMM")
self.calendarMonth.setText(month_name)
we now have a label that changes with the calendar.
neat!
next is the terminal!
so, with some research I learned about various types of signals.
signals are basically a condition a piece of code sends out to interact with other code, for example we've been using clicked, activated and textchanged this whole time.
this time we are using selectionChanged as our signal.
and this is how we are doing it:
self.calendar.selectionChanged.connect()
next is the function.
Gonna start with a function called update terminal.
def updateTerminal(self):
then I will store the selected date:
calendar_Date = self.calendar.selectedDate()
next is playing with string stuff which I always enjoy(sarcasm) and love to do(sarcasm)
so, the calendar_Date is a date format and we need to make it into a string for the text box.
so:
terminal_Date = calendar_Date.toString("dd/MM/yyyy")
new variable, the date to a string and its format
the terminal which is a text box is called leaveTerm so:
self.leaveTerm.setText(terminal_Date)
Full code:
def updateTerminal(self):
calendar_Date = self.calendar.selectedDate()
terminal_Date = calendar_Date.toString("dd/MM/yyyy")
self.leaveTerm.setText(terminal_Date)
great, I think we did well enough to move onto the next part of this proj................................................................So, I was just informed that weekends aren't part of this.
*sigh*
ok so lets see.
I'm going back to the datefinder function.
so if its weekends I just need to find the index for the weekend and skip over it by like 2 days yea?
so:
if self.calendar.selectedDate().dayOfWeek() == 6:
self.dayAllot.value() + 2
day += 2
and with a quick test, I seeeee. its tuesday.
ok so:
if self.calendar.selectedDate().dayOfWeek() == 5:
self.dayAllot.value() + 2
day += 2
yay monday!
oh it went back to sunday
ok there has to be setvalue right?
self.dayAllot.setValue(self.dayAllot.value()+2)
.....it went right to 99.
*le sigh*
after some tinkering around for about 30 minutes I found the culprit:
self.dayAllot.textChanged.connect(self.dateFinder)
turns out its because everytime the text changes, the function happens and inside the same function is also something that changes the text which ends up triggering the whole function again.....whoops
so self.dayAllot.textChanged.disconnect(self.dateFinder) goes at the top of the datefinder function and self.dayAllot.textChanged.connect(self.dateFinder)goes at the bottom, neat it works.
now let's enter in a 1 for the first day and a 9 for the allotted days and we get 10.
under normal circumstances, this would be great but the weekends arent included so it's supposed to be 12 instead.
welp back to the drawing board, got to figure out how to either remove weekends from the calculation or find all of the weekends and add them to the final day.
that will be next time, however.
~~Justin Case
Comments
Post a Comment