Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to remove multiple selected items in listbox in Tkinter?
Next article icon

Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs. 

Note: For more information, refer toPython GUI – tkinter

ListBox widget

The ListBox widget is used to display different types of items. These items must be of the same type of font and having the same font color. The items must also be of Text type. The user can select one or more items from the given list according to the requirement.

Syntax:

listbox = Listbox(root, bg, fg, bd, height, width, font, ..)

Optional parameters

  • root - root window.
  • bg - background colour
  • fg - foreground colour
  • bd - border
  • height - height of the widget.
  • width - width of the widget.
  • font - Font type of the text.
  • highlightcolor - The colour of the list items when focused.
  • yscrollcommand - for scrolling vertically.
  • xscrollcommand - for scrolling horizontally.
  • cursor - The cursor on the widget which can be an arrow, a dot etc.

Common methods

  • yview - allows the widget to be vertically scrollable.
  • xview - allows the widget to be horizontally scrollable.
  • get() - to get the list items in a given range.
  • activate(index) - to select the lines with a specified index.
  • size() - return the number of lines present.
  • delete(start, last) - delete lines in the specified range.
  • nearest(y) - returns the index of the nearest line.
  • curseselection() - returns a tuple for all the line numbers that are being selected.

Example 1: 

Python3
fromtkinterimport*# create a root window.top=Tk()# create listbox objectlistbox=Listbox(top,height=10,width=15,bg="grey",activestyle='dotbox',font="Helvetica",fg="yellow")# Define the size of the window.top.geometry("300x250")# Define a label for the list.label=Label(top,text=" FOOD ITEMS")# insert elements by their# index and names.listbox.insert(1,"Nachos")listbox.insert(2,"Sandwich")listbox.insert(3,"Burger")listbox.insert(4,"Pizza")listbox.insert(5,"Burrito")# pack the widgetslabel.pack()listbox.pack()# Display until User# exits themselves.top.mainloop()

Output:

 python-tkinter-listbox 

Example 2: Let's Delete the elements from the above created listbox 

Python3
# Delete Items from the list#  by specifying the index.listbox.delete(2)

Output:

python-tkinter-listbox


Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp