For my application GUI (tkinter), I need a 'bookkeeping' variable that holds the current row number for a UI widget. I prefer not to hard code the row number because inserting a widget would involve renumbering all row numbers following it. In this situation, an increment operator (x++
, similar to C++) would come in very handy, because incrementing the variable when necessary would require only two additional characters. However, Python does not support increment operators.
To avoid increment statement between elements that should go on different rows, I came up with the following solution but I am wondering what an alternative might be:
rownums = list(range(100))
rowstay = lambda: rownums[0] # Return current row number and stay on row.
rowmove = lambda: rownums.pop(0) # Return current row number and move on to next.
print(f'Two items: {rowstay()}, {rowmove()}')
print(f'One item: {rowmove()}')
print(f'Two items: {rowstay()}, {rowmove()}')
For example (tkinter):
# Create some buttons.
mybutton1 = tkinter.button(master, 'Button on row 0')
mybutton2 = tkinter.button(master, 'Button on row 0')
mybutton3 = tkinter.button(master, 'Button on row 1')
# Place buttons (use mimicked increment 'operator').
mybutton1.grid(row=rowstay(), column=0)
mybutton2.grid(row=rowmove(), column=1)
mybutton3.grid(row=rowmove(), column=0)
What alternative code would provide the same functionality?