python - Tkinter OptionMenu: setting the width by the longest menu item in the list -


OptionMenu of the tanker sets its width based on the current active menu item. I'm trying to set my width based on a wide menu item in a way, when the user selects a separate menu item, instead of the currently selected one to prevent the widget from resizing itself.

The suggestion of the answer in the question hides the problem, but it does not resolve (suggestion: there is no duplicate!) Acceptable answer widget to w and / Code> is to be set to be sticky for both. This hides the problem until the suggested size for the column is more comprehensive than all the objects in the box, but does not really solve the problem.

I like to do (more like) a grid layout columns with different widgets, which are dynamically shaped on the width of the widgets, however, I like it The width reported by the OptionMenu widget is that it is the most comprehensive item currently selected, as the width of the change in the whole column, if the user chooses an item, then compare the other items. More in A.

So far I have found that all the recommended methods have been to stick to the widget W and E that 't (automatically ) In my case the problem is solved, and an uncontrolled continuous width which is not related to what I do not want to do. Item

Thanks to two who answered, but neither using a monotype font or just string It is desirable solution to set the width to the length. The latter is also very weak because the width parameter takes the number corresponding to the width of the character "0" in the font given in a particular "text unit". My last solution follows:

  1. Use fonts to fonts.
  2. Use scale to find the width of each string. (Here the pixels are given in.)
  3. 0 Divide the longest width by the width of the character You now have the width in "text units".

    This gives the following code, maybe it can be more pythonic and there may be a class method of subclass of OptionMenu etc., but it works.

      def setMaxWidth (stringList, element): f = tkFont Nametofont (element.cget ("font") zerowidth = f.measure ("0") w = max ([f.measure (I) in the string string)) / zerowidth element.config (width = w)   

    Out of curiosity, I wanted to know that the adjusted width should be in my data from the character length of each string Will be different. It's a simple loop to use with my strings.

      I list the string: take print (i), float (f) (float) / float (xerobath)   

    data:

      4 3.83333333333 24 20.1666666667 22 18.5 11 11666666667 12 11.3333333333 14 12.6666666667   

    left column = string length. Right column = calculation width in other words, a naive lane (max (opts, key = len)) in all cases, a must-have-needed widget (this is definitely expected). I have also repeatedly compared the width of the calculation of the widget using special cases of w , as well as duplicated . And in all cases, these artificial test strings generally agree with the width of "width" by the test widget, which does not have any width manually.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -