Python Forum

Full Version: need help in optionMenu using tkinter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
HI..I need help
I got a code which use optionMenu tkinder
the list is too long..It stretch from the full height of the screen

May i know how to limit the number of items to be displayed?Thanks

import sys
import os
from Tkinter import *
import tkMessageBox
root = Tk()
frame = Frame(root, height="200", width="200", bg="blue")
frame.pack()

label = Label(frame,text="""(Above) Constructed (Year):""")
label.pack(padx=5, pady=10, side=LEFT)

constructed_var = StringVar(root)
constructed_var.set("Any") # initial value

options = [
"Any",
"2016",
"2015",
"2014",
"2013",
"2012",
"2011",
"2010",
"2009",
"2008",
"2007",
"2006",
"2005",
"2004",
"2003",
"2002",
"2001",
"2000",
"1999",
"1998",
"1997",
"1996",
"1995",
"1994",
"1993",
"1992",
"1991",
"1990",
"1989",
"1988",
"1987",
"1986",
"1985",
"1984",
"1983",
"1982",
"1981",
"1980",
"1979",
"1978",
"1977",
"1976",
"1975",
"1974",
"1973",
"1972",
"1971",
"1970"
]

constructed_option = OptionMenu(frame, constructed_var,*options)
constructed_option.pack(padx=5, pady=10, side=LEFT)
constructed_option.configure(width=5)

mainloop()
This will shorten it up a bit:
>>> options = ['Any' ]
>>> for year in range(1970,2016):
...     options.append(str(year))
...
>>> options
['Any', '1970', '1971', '1972', '1973', '1974', '1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015']

>>>
I think OP wants to limit the number of rows to be displayed when click on optionmenu dropdown
the optionMenu widget doesn't have many options (excuse the pun)
Can you use a ttk Treeview instead?
You can control how many rows are exposed and add a scroll bar to scan through.
The fact that it is a tree, means that you can have expandable sublists as well, something
you may want to use in the future.

You can read up on the options available here: Treeview
[attachment=276]
HI

Thanks all for the value suggestions