Python Tkinter: OptionMenu modify dropdown list width
Asked Answered
W

6

6

I have created an OptionMenu from Tkinter with a columnspan of 2. However, the dropdown list/menu does not match the width, so it does not look good. Any idea on how to match their width?

self.widgetVar = StringVar(self.top)
choices = ['', 'wire', 'register']
typeOption = OptionMenu(self.top, self.widgetVar, *choices)
typeOption.grid(column = 0, columnspan = 2, row = 0, sticky = 'NSWE', padx = 5, pady = 5)
Waligore answered 23/9, 2014 at 16:53 Comment(0)
A
6

doing drop down name.config(width = width) works very well with resizing the drop down box. i managed to get it to work with.

drop1.config(width = 20)

Just letting you know width 20 is quite long.

Aminopyrine answered 30/4, 2020 at 22:29 Comment(0)
B
3

There is no way to change the width of the dropdown.

You might want to consider the ttk.Combobox widget. It has a different look that might be what you're looking for.

Briseno answered 27/10, 2015 at 12:23 Comment(0)
C
1

One idea is to pad the right side (or left, or both) with spaces. Then, when you need the selected value, strip it with str strip. Not great, but better than nothing.

from tkinter import ttk
import tkinter as tk

root = tk.Tk()

def func(selected_item):
  print(repr(selected_item.strip()))

max_len = 38
omvar = tk.StringVar()
choices = ['Default Choice', 'whoa', 'this is a bit longer'] + ['choice'+str(i) for i in range(3)]
padded_choices = [x+' '*(max_len-len(x)) for x in choices]
om = ttk.OptionMenu(root, omvar, 'Default Choice', *padded_choices, command=func)
om.config(width=30)
om.grid(row=0, column=0, padx=20, pady=20, sticky='nsew')

root.mainloop()
Calypso answered 16/9, 2016 at 7:11 Comment(0)
G
1

this answer is a little bit late but I thought in case other people are searching for it, here is my solution:

optionMenu1 = ttk.OptionMenu(btnPane, item_text, item_text.get(), "Choose item!\t\t\t\t\t\t\t\t",
                *list1, *list2(), style='Custom.TMenubutton')

what I am doing here is to only set the default-value with a lot of tabs (\t). The reason for this is that all items of the dropdown are getting the same width. The width of the longest item. In this case its the Default. Now you can get the value of the other items without stripping something. And your width has changed.

How much tabs you need will you see if you test it (depending on the width of your OptionMenu).

Hope it helps someone.

Regards

Gustafsson answered 15/1, 2020 at 9:25 Comment(0)
D
1

Just use config()

typeOption.config(width = 50)
Decrescendo answered 6/7, 2020 at 19:22 Comment(0)
T
-1

We can change the dropdown width by writing as follows:

typesOfSurgeries = ['Chemotherapy','Cataract']
listOfSurgeries = tkinter.OptionMenu(test_frame, variable, *typesofSurgeries)
listOfSurgeries.config(width=20)
listOfSurgeries.grid(row=14,column=1)

listOfSurgeries.config(width=20) sets the width of the OptionMenu

Trilbie answered 7/5, 2019 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.