Python Forum
[Tkinter] need help in optionMenu using tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] need help in optionMenu using tkinter
#1
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()
Reply
#2
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']

>>>
Reply
#3
I think OP wants to limit the number of rows to be displayed when click on optionmenu dropdown
Reply
#4
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
   
Reply
#5
HI

Thanks all for the value suggestions
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter reduce OptionMenu height euras 2 4,569 May-25-2021, 09:14 PM
Last Post: euras
  [Tkinter] optionmenu value write a list mdalireza 0 2,265 Nov-11-2019, 01:00 PM
Last Post: mdalireza
  [Tkinter] Retrieve OptionMenu selection? JP_ROMANO 5 23,588 Mar-13-2019, 10:56 AM
Last Post: vsathya
  [Tkinter] [split] need help in optionMenu using tkinter Rakeshkrtiwari_07 0 2,223 Aug-02-2018, 04:16 PM
Last Post: Rakeshkrtiwari_07
  [Tkinter] Tkinter optionmenu child menu position showing 0,0 thatguy14 2 4,590 Jun-15-2018, 10:42 AM
Last Post: thatguy14

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020