python - Need explanation for use of '%' and '/' to grid buttons with Tkinter -
the code below used in simple calculator made using tkinter implement simple gui. new python, , first programming language. first stab @ creating gui. calculator works fine, almost. buttons supposed far. question this: in code below have made row = index%3 , column = index/3. places buttons in nice 3 x 3 block. however, used snippet without understanding it. found online. find can tinker desired results, i'm not entirely clear why works way does. suppose math question really. clarification appreciated though. sorry if it's structured oddly, i'm not used forum formatting business.
self.operators = ['+', '-', '*', '/','%','^','c','m','m+'] index in range(9): button(self.opframe, relief=groove, bg="light yellow", text=self.operators[index], width=3, height=1, command=lambda arg=self.operators[index], arg2=self.num_dict, arg3=self.num_list, arg4=self.count : self.buttonclick(arg,arg2,arg3,arg4)).grid(padx=2,pady=2,row=index%3,column=index/3)
the %
operator yields remainder division of index three, in 9 iterations yield 0, 1, 2, 0, 1, 2, 0, 1, 2 in order. in other words, "loops" on values 0, 1 , 2 each iteration.
with /
, calculate floor division of index three: 0, 0, 0, 1, 1, 1, 2, 2, 2. way move next column each 3 iterations.
it easier see if change order of operators , arrange self.operator
this:
self.operators = ['+', '/', 'c', '-', '%', 'm', '*', '^', 'm+'] index in range(9): button(...).grid(padx=2,pady=2,row=index/3,column=index%3)
Comments
Post a Comment