1

I want to limit the height of a tkinter widget based on a length of a list. For this, i want to set 5 as a lower and 10 as an upper limit. The height should be 10 if my list exceeds 10 elements, 5 when there are equal or less than 5 elements, and the exact amount of elements if the list is in that range. I am wondering if there is a more pythonic way than this:

if 5 <= len(self.content) <= 10:
    lb_height = len(self.content)
elif len(self.content) <= 5:
    lb_height = 5
else:
    lb_height = 10
mnikley
  • 1,625
  • 1
  • 8
  • 21

1 Answers1

1

This answer does something similar: https://stackoverflow.com/a/4092550/11652155

For your case, you'd just change the lower and upper limits:

lb_height = max(5, min(len(self.content), 10))
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
rkechols
  • 558
  • 5
  • 15