Search code examples
pythonuser-interfacetkinterfloating

How to prevent floating widgets in tkinter python


I have the following UI for my tkinter application. My problem is that the OptionsMenu with long text moves all other widgets to the right and eventually out of frame.

Solutions that I can think of: 1) Wrap text to next row.

2) A way that StringVar can truncate the selected text of the dropdown up until certain characters but does not change the original value (selected in dropdown) it stores.

3) Stop making them float and overlap over other widgets.

Here is the image of the erratic behavior.

enter image description here

Here is my code: for the Finding Category dropdown.

    if finding_names != []:
        finding_names.insert(0,'All')
        finding_type_select.set(finding_names[0])
        finding_type_dropdown = OptionMenu(tab3_project_reports,finding_type_select,*finding_names)
        finding_type_dropdown.configure(font='helvetica 12')
        finding_type_dropdown.grid(row=5, column=1,padx=10, pady=10,sticky=W+E+N+S)
        finding_type_dropdown.grid_columnconfigure(0, weight=1)

I have also tried to use the grid_columnconfigure but I did not really see any change.

I would appreciate any help. Thanks in advance.


Solution

  • The only thing I can see here that could be the problem (without seeing more code) is how you are using grid_columnconfig(). You can only apply a column/row config to a container. These would be the root window, a Toplevel() window or a Frame. You are currently using grid_columnconfigure() and that will work but note you can also just do columnconfigure() without the grid_ portion. Same goes for rows.

    Instead of this:

    finding_type_dropdown.grid_columnconfigure(0, weight=1)
    

    Do this:

    tab3_project_reports.grid_columnconfigure(0, weight=1)