James the Great
James the Great

Reputation: 951

Python Tkinter: OptionMenu modify dropdown list width

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)

Upvotes: 6

Views: 18702

Answers (6)

wahsandaruwan
wahsandaruwan

Reputation: 689

Just use config()

typeOption.config(width = 50)

Upvotes: 1

ath0rus
ath0rus

Reputation: 93

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.

Upvotes: 6

chrisaramar
chrisaramar

Reputation: 363

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

Upvotes: 1

Sreenidhi D R
Sreenidhi D R

Reputation: 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

Upvotes: -1

Jarad
Jarad

Reputation: 18983

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()

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 386372

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.

Upvotes: 3

Related Questions